Merge pull request #61 from moul/dev/jhayotte/getenum

add helper getEnumValue
This commit is contained in:
Manfred Touron 2017-05-15 15:03:02 +02:00 committed by GitHub
commit dc386661ca
7 changed files with 55 additions and 2 deletions

View File

@ -8,6 +8,7 @@ install:
.PHONY: test
test: install
cd examples/enum && make
cd examples/import && make
cd examples/dummy && make
cd examples/flow && make

View File

@ -17,6 +17,7 @@ type GenericTemplateBasedEncoder struct {
templateDir string
service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto
enum []*descriptor.EnumDescriptorProto
debug bool
destinationDir string
}
@ -34,6 +35,7 @@ type Ast struct {
Filename string `json:"filename"`
TemplateDir string `json:"template-dir"`
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) {
@ -43,8 +45,8 @@ func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descript
templateDir: templateDir,
debug: debug,
destinationDir: destinationDir,
enum: file.GetEnumType(),
}
if debug {
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,
file: file,
templateDir: templateDir,
enum: file.GetEnumType(),
debug: debug,
destinationDir: destinationDir,
}
if debug {
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,
Filename: "",
Service: e.service,
Enum: e.enum,
}
buffer := new(bytes.Buffer)
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)

13
examples/enum/Makefile Normal file
View 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

View File

@ -0,0 +1,9 @@
-red
-blue
-black
-yellow
-green
-dark
-white
-gray
-orange

View 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;
}

View File

@ -0,0 +1,2 @@
{{range $m := "colors" | getEnumValue .Enum }}-{{$m.Name}}
{{end}}

View File

@ -65,6 +65,7 @@ var ProtoHelpersFuncMap = template.FuncMap{
},
"snakeCase": xstrings.ToSnakeCase,
"getMessageType": getMessageType,
"getEnumValue": getEnumValue,
"isFieldMessage": isFieldMessage,
"isFieldRepeated": isFieldRepeated,
"goType": goType,
@ -98,6 +99,16 @@ func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.
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 {
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
return true