-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (70 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"flag"
. "github.com/dave/jennifer/jen"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)
type Field struct {
Name string
Type string
}
type Struct struct {
Name string
Fields []Field
}
func generateFile(gen *protogen.Plugin, protoFile *protogen.File) *protogen.GeneratedFile {
filename := protoFile.GeneratedFilenamePrefix + ".go"
g := gen.NewGeneratedFile(filename, protoFile.GoImportPath)
// 構造体を元にコードを生成
// fmt.Println(*f.Proto.Package)
file := NewFile(*protoFile.Proto.Package)
// Messageから構造体を作成
var Structs []Struct
for _, m := range protoFile.Messages {
strct := Struct{
Name: m.GoIdent.GoName,
Fields: []Field{},
}
for _, field := range m.Fields {
ff := Field{
Name: field.GoName,
Type: field.Desc.Kind().String(),
}
strct.Fields = append(strct.Fields, ff)
}
Structs = append(Structs, strct)
}
for _, strct := range Structs {
var codes []Code
for _, field := range strct.Fields {
code := Id(field.Name).Id(field.Type)
codes = append(codes, code)
}
// modelのstruct作成
file.Type().Id(strct.Name).Struct(codes...)
}
buf := &bytes.Buffer{}
err := file.Render(buf)
if err != nil {
panic(err)
}
g.P(buf)
return g
}
func main() {
var flags flag.FlagSet
protogen.Options{
ParamFunc: flags.Set,
}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, f := range gen.Files {
if !f.Generate {
continue
}
generateFile(gen, f)
}
return nil
})
}