Skip to content

Commit

Permalink
fix: seaql use annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Dec 26, 2023
1 parent 654829c commit 9bd2ec8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 48 deletions.
58 changes: 16 additions & 42 deletions codegen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,32 @@ func (g *CodeGen) GenMapper() *CodeGen {
}

g.Println(`import "protoc-gen-openapiv2/options/annotations.proto";`)
g.Println(`import "protosaber/seaql/seaql.proto";`)
g.Println(`// import "protosaber/enumerate/enumerate.proto";`)
g.Println()

for _, et := range g.entities {
structName := utils.CamelCase(et.Name)
commaOrEmpty := func(r int) string {
if r == 0 {
return ""
}
return ","
}

g.Printf("// %s %s\n", structName, trimStructComment(et.Comment, "\n", "\n// "))
g.Printf("message %s {\n", structName)
if (et.Table != nil && et.Table.PrimaryKey() != nil) ||
len(et.Indexes) > 0 || len(et.ForeignKeys) > 0 {
g.Println("option (things_go.seaql.options) = {")
if (et.Table != nil && et.Table.PrimaryKey() != nil) || len(et.Indexes) > 0 {
g.Println("index: [")
remain := len(et.Indexes)
if et.Table != nil && et.Table.PrimaryKey() != nil {
ending := commaOrEmpty(remain)
g.Printf("'%s'%s\n", et.Table.PrimaryKey().Definition(), ending)
}
for _, index := range et.Indexes {
remain--
if et.Table != nil &&
et.Table.PrimaryKey() != nil &&
et.Table.PrimaryKey().Index().Name == index.Name {
continue
}
ending := commaOrEmpty(remain)
g.Printf("'%s'%s\n", index.Index.Definition(), ending)
}
}
g.Println("],")
if remain := len(et.ForeignKeys); remain > 0 {
g.Println("foreign_key: [")
for _, fk := range et.ForeignKeys {
remain--
ending := commaOrEmpty(remain)
g.Printf("'%s'%s\n", fk.ForeignKey.Definition(), ending)
}
g.Println("],")

g.Printf("// #[seaql]\n")
if et.Table != nil && et.Table.PrimaryKey() != nil {
g.Printf("// #[seaql(index=\"%s\")]\n", et.Table.PrimaryKey().Definition())
}
for _, index := range et.Indexes {
if et.Table != nil &&
et.Table.PrimaryKey() != nil &&
et.Table.PrimaryKey().Index().Name == index.Name {
continue
}
g.Println("};")
g.Printf("// #[seaql(index=\"%s\")]\n", index.Index.Definition())
}
g.Println()
for _, fk := range et.ForeignKeys {
g.Printf("// #[seaql(foreign_key=\"%s\")]\n", fk.ForeignKey.Definition())
}
g.Printf("message %s {\n", structName)
for i, m := range et.ProtoMessage {
if m.Comment != "" {
g.Printf("// %s\n", m.Comment)
g.Printf("%s\n", m.Comment)
}
g.Println(genMapperMessageField(i+1, m))
}
Expand Down
8 changes: 4 additions & 4 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func (t EntityDescriptorSlice) Less(i, j int) bool { return t[i].Name < t[j].Nam
func (t EntityDescriptorSlice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }

func BuildEntity(m MixinEntity, opt *Option) *EntityDescriptor {
enableGogo, enableSea := false, false
if opt != nil {
enableGogo, enableSea = opt.EnableGogo, opt.EnableSea
}
fielders := m.Fields()
fields := make([]*FieldDescriptor, 0, len(fielders))
protoMessages := make([]*ProtoMessage, 0, len(fielders))
for _, fb := range fielders {
field := fb.Build(opt)
fields = append(fields, field)
enableGogo, enableSea := false, false
if opt != nil {
enableGogo, enableSea = opt.EnableGogo, opt.EnableSea
}
protoMessages = append(protoMessages, buildProtoMessage(field, enableGogo, enableSea))
}
indexers := m.Indexes()
Expand Down
13 changes: 11 additions & 2 deletions protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ func buildProtoMessage(field *FieldDescriptor, enableGogo, enableSea bool) *Prot
annotations = append(annotations, `(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = { type: [ INTEGER ] }`)
}
}
comment := field.Comment
if comment != "" {
comment = "// " + comment
}
if field.Column != nil && enableSea {
annotations = append(annotations, fmt.Sprintf(`(things_go.seaql.field) = { type: "%s" }`, field.Column.Definition()))
l := fmt.Sprintf(`// #[seaql(type="%s")]`, field.Column.Definition())
if comment != "" {
comment = comment + "\n" + l
} else {
comment = l
}
}
return &ProtoMessage{
DataType: dataType,
Name: field.Name,
Comment: field.Comment,
Comment: comment,
Annotations: annotations,
}
}

0 comments on commit 9bd2ec8

Please sign in to comment.