add helper getEnumValue
This commit is contained in:
parent
c750f5de81
commit
e1486970f7
1
Makefile
1
Makefile
@ -8,6 +8,7 @@ install:
|
|||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: install
|
test: install
|
||||||
|
cd examples/enum && make
|
||||||
cd examples/import && make
|
cd examples/import && make
|
||||||
cd examples/dummy && make
|
cd examples/dummy && make
|
||||||
cd examples/flow && make
|
cd examples/flow && make
|
||||||
|
@ -17,6 +17,7 @@ type GenericTemplateBasedEncoder struct {
|
|||||||
templateDir string
|
templateDir string
|
||||||
service *descriptor.ServiceDescriptorProto
|
service *descriptor.ServiceDescriptorProto
|
||||||
file *descriptor.FileDescriptorProto
|
file *descriptor.FileDescriptorProto
|
||||||
|
enum []*descriptor.EnumDescriptorProto
|
||||||
debug bool
|
debug bool
|
||||||
destinationDir string
|
destinationDir string
|
||||||
}
|
}
|
||||||
@ -34,6 +35,7 @@ type Ast struct {
|
|||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
TemplateDir string `json:"template-dir"`
|
TemplateDir string `json:"template-dir"`
|
||||||
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
||||||
|
Enum []*descriptor.EnumDescriptorProto `json:"enum"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
||||||
@ -43,8 +45,8 @@ func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descript
|
|||||||
templateDir: templateDir,
|
templateDir: templateDir,
|
||||||
debug: debug,
|
debug: debug,
|
||||||
destinationDir: destinationDir,
|
destinationDir: destinationDir,
|
||||||
|
enum: file.GetEnumType(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
||||||
}
|
}
|
||||||
@ -57,10 +59,10 @@ func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDes
|
|||||||
service: nil,
|
service: nil,
|
||||||
file: file,
|
file: file,
|
||||||
templateDir: templateDir,
|
templateDir: templateDir,
|
||||||
|
enum: file.GetEnumType(),
|
||||||
debug: debug,
|
debug: debug,
|
||||||
destinationDir: destinationDir,
|
destinationDir: destinationDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
|
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
|
||||||
}
|
}
|
||||||
@ -117,6 +119,7 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
|
|||||||
RawFilename: templateFilename,
|
RawFilename: templateFilename,
|
||||||
Filename: "",
|
Filename: "",
|
||||||
Service: e.service,
|
Service: e.service,
|
||||||
|
Enum: e.enum,
|
||||||
}
|
}
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
||||||
|
13
examples/enum/Makefile
Normal file
13
examples/enum/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
mkdir -p output
|
||||||
|
protoc -I. --gotemplate_out=template_dir=templates,debug=true,all=true:output proto/*.proto
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: re
|
||||||
|
re: clean build
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf output
|
9
examples/enum/output/enum.txt
Normal file
9
examples/enum/output/enum.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
-red
|
||||||
|
-blue
|
||||||
|
-black
|
||||||
|
-yellow
|
||||||
|
-green
|
||||||
|
-dark
|
||||||
|
-white
|
||||||
|
-gray
|
||||||
|
-orange
|
14
examples/enum/proto/sample.proto
Normal file
14
examples/enum/proto/sample.proto
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package Sample;
|
||||||
|
|
||||||
|
enum Colors {
|
||||||
|
red = 0;
|
||||||
|
blue = 1;
|
||||||
|
black = 2;
|
||||||
|
yellow = 3;
|
||||||
|
green = 4;
|
||||||
|
dark = 5;
|
||||||
|
white = 6;
|
||||||
|
gray = 7;
|
||||||
|
orange = 8;
|
||||||
|
}
|
2
examples/enum/templates/enum.txt.tmpl
Normal file
2
examples/enum/templates/enum.txt.tmpl
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{{range $m := "colors" | getEnumValue .Enum }}-{{$m.Name}}
|
||||||
|
{{end}}
|
11
helpers.go
11
helpers.go
@ -65,6 +65,7 @@ var ProtoHelpersFuncMap = template.FuncMap{
|
|||||||
},
|
},
|
||||||
"snakeCase": xstrings.ToSnakeCase,
|
"snakeCase": xstrings.ToSnakeCase,
|
||||||
"getMessageType": getMessageType,
|
"getMessageType": getMessageType,
|
||||||
|
"getEnumValue": getEnumValue,
|
||||||
"isFieldMessage": isFieldMessage,
|
"isFieldMessage": isFieldMessage,
|
||||||
"isFieldRepeated": isFieldRepeated,
|
"isFieldRepeated": isFieldRepeated,
|
||||||
"goType": goType,
|
"goType": goType,
|
||||||
@ -98,6 +99,16 @@ func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEnumValue(f []*descriptor.EnumDescriptorProto, name string) []*descriptor.EnumValueDescriptorProto {
|
||||||
|
for _, item := range f {
|
||||||
|
if strings.EqualFold(*item.Name, name) {
|
||||||
|
return item.GetValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
||||||
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
||||||
return true
|
return true
|
||||||
|
Loading…
Reference in New Issue
Block a user