From 9938cbdf5aba453c1fecb3a8c071bce6debbc39f Mon Sep 17 00:00:00 2001 From: Guilhem Fanton Date: Mon, 20 Aug 2018 17:41:36 +0200 Subject: [PATCH 1/2] feat(option): Add bool method option --- helpers/helpers.go | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/helpers/helpers.go b/helpers/helpers.go index f558d74..6d96152 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -154,6 +154,7 @@ var ProtoHelpersFuncMap = template.FuncMap{ "leadingDetachedComments": leadingDetachedComments, "stringFieldExtension": stringFieldExtension, "stringMethodOptionsExtension": stringMethodOptionsExtension, + "boolMethodOptionsExtension": boolMethodOptionsExtension, "boolFieldExtension": boolFieldExtension, "isFieldMap": isFieldMap, "fieldMapKeyType": fieldMapKeyType, @@ -351,6 +352,41 @@ func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) str return *str } +func boolMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) bool { + if f == nil { + return false + } + if f.Options == nil { + return false + } + var extendedType *descriptor.MethodOptions + var extensionType *bool + + eds := proto.RegisteredExtensions(f.Options) + if eds[fieldID] == nil { + ed := &proto.ExtensionDesc{ + ExtendedType: extendedType, + ExtensionType: extensionType, + Field: fieldID, + Tag: fmt.Sprintf("bytes,%d", fieldID), + } + proto.RegisterExtension(ed) + eds = proto.RegisteredExtensions(f.Options) + } + + ext, err := proto.GetExtension(f.Options, eds[fieldID]) + if err != nil { + return false + } + + b, ok := ext.(*bool) + if !ok { + return false + } + + return *b +} + func boolFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) bool { if f == nil { return false @@ -378,12 +414,12 @@ func boolFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) bool return false } - str, ok := ext.(*bool) + b, ok := ext.(*bool) if !ok { return false } - return *str + return *b } func init() { From 8a8c54b5b4fd2bfe5841b43fb43e8a9b72b2ed99 Mon Sep 17 00:00:00 2001 From: Guilhem Fanton Date: Mon, 20 Aug 2018 17:41:58 +0200 Subject: [PATCH 2/2] chore(store): Dont panic on get store --- helpers/helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/helpers.go b/helpers/helpers.go index 6d96152..0367dee 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -176,7 +176,7 @@ func getStore(s string) interface{} { return v } - panic(fmt.Sprintf("No key named '%s' found", s)) + return false } func InitPathMap(file *descriptor.FileDescriptorProto) {