From 1ff20846ebc55c8f4d3703affc3757949f78c30d Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 18 Jan 2019 19:39:16 +0100 Subject: [PATCH] feat: add int64FieldExtension helper --- .travis.yml | 2 +- README.md | 93 +++++++++++++++++++++++----------------------- go.mod | 12 ++---- go.sum | 22 ++++++----- helpers/helpers.go | 38 ++++++++++++++++++- 5 files changed, 101 insertions(+), 66 deletions(-) diff --git a/.travis.yml b/.travis.yml index 76545aa..8abbc91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ go: 1.11.x go_import_path: moul.io/protoc-gen-gotemplate install: - go get github.com/Masterminds/glide -- wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.4.0 +- wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/v1.5.0/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.4.0 - go get -u github.com/golang/protobuf/protoc-gen-go - go get github.com/securego/gosec/cmd/gosec/... - go get -u github.com/alecthomas/gometalinter diff --git a/README.md b/README.md index d87aff2..eb609da 100644 --- a/README.md +++ b/README.md @@ -76,56 +76,57 @@ This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) libr Non-exhaustive list of new helpers: * **all the functions from [sprig](https://github.com/Masterminds/sprig)** -* `string` -* `json` -* `prettyjson` -* `splitArray` -* `first` -* `last` -* `upperFirst` -* `lowerFirst` -* `camelCase` -* `lowerCamelCase` -* `kebabCase` -* `contains` -* `trimstr` -* `index` -* `snakeCase` -* `getProtoFile` -* `getMessageType` -* `getEnumValue` -* `isFieldMessage` -* `isFieldMessageTimeStamp` -* `isFieldRepeated` -* `haskellType` -* `goType` -* `goZeroValue` -* `goTypeWithPackage` -* `jsType` -* `jsSuffixReserved` -* `namespacedFlowType` -* `httpVerb` -* `httpPath` -* `httpPathsAdditionalBindings` -* `httpBody` -* `shortType` -* `urlHasVarsFromMessage` -* `lowerGoNormalize` -* `goNormalize` -* `leadingComment` -* `trailingComment` -* `leadingDetachedComments` -* `stringFieldExtension` -* `stringMethodOptionsExtension` +* `add` * `boolFieldExtension` -* `isFieldMap` +* `camelCase` +* `contains` +* `divide` * `fieldMapKeyType` * `fieldMapValueType` -* `replaceDict` -* `add` -* `subtract` +* `first` +* `getEnumValue` +* `getMessageType` +* `getProtoFile` +* `goNormalize` +* `goTypeWithPackage` +* `goType` +* `goZeroValue` +* `haskellType` +* `httpBody` +* `httpPath` +* `httpPathsAdditionalBindings` +* `httpVerb` +* `index` +* `int64FieldExtension` +* `isFieldMap` +* `isFieldMessageTimeStamp` +* `isFieldMessage` +* `isFieldRepeated` +* `jsSuffixReserved` +* `jsType` +* `json` +* `kebabCase` +* `last` +* `leadingComment` +* `leadingDetachedComments` +* `lowerCamelCase` +* `lowerFirst` +* `lowerGoNormalize` * `multiply` -* `divide` +* `namespacedFlowType` +* `prettyjson` +* `replaceDict` +* `shortType` +* `snakeCase` +* `splitArray` +* `stringFieldExtension` +* `stringMethodOptionsExtension` +* `string` +* `subtract` +* `trailingComment` +* `trimstr` +* `upperFirst` +* `urlHasVarsFromMessage` See the project helpers for the complete list. diff --git a/go.mod b/go.mod index 52bdf05..cad2ec4 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,6 @@ require ( github.com/Masterminds/semver v1.2.2 // indirect github.com/Masterminds/sprig v2.14.1+incompatible github.com/aokoli/goutils v0.0.0-20170502144750-e57d01ace047 // indirect - github.com/dgrijalva/jwt-go v0.0.0-20160621201154-c9eaceb2896d // indirect - github.com/go-kit/kit v0.0.0-20161109000648-9f5c614cd1e7 // indirect - github.com/go-logfmt/logfmt v0.3.0 // indirect - github.com/go-stack/stack v1.5.3 // indirect github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f // indirect @@ -16,12 +12,10 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.2.2 github.com/huandu/xstrings v0.0.0-20151130125119-3959339b3335 github.com/imdario/mergo v0.0.0-20171009183408-7fe0c75c13ab // indirect - github.com/kr/fs v0.0.0-20131111012553-2788f0dbd169 // indirect - github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect github.com/satori/go.uuid v1.1.0 // indirect + github.com/stretchr/testify v1.3.0 // indirect golang.org/x/crypto v0.0.0-20170516161655-0fe963104e9d // indirect - golang.org/x/net v0.0.0-20170108160505-da2b4fa28524 // indirect - golang.org/x/text v0.0.0-20161216064924-a49bea13b776 // indirect + golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect google.golang.org/genproto v0.0.0-20170517234824-bb3573be0c48 - google.golang.org/grpc v1.0.4 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect ) diff --git a/go.sum b/go.sum index 3c7ed36..f2e6a00 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,8 @@ github.com/Masterminds/sprig v2.14.1+incompatible h1:rTHERm50Xp1Cbb8x7xBCeDp//jM github.com/Masterminds/sprig v2.14.1+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= github.com/aokoli/goutils v0.0.0-20170502144750-e57d01ace047 h1:Bn0iqJ/349f606hR0juIGyheAI6+hyg9XUOLhN9udLo= github.com/aokoli/goutils v0.0.0-20170502144750-e57d01ace047/go.mod h1:SijmP0QR8LtwsmDs8Yii5Z/S4trXFGFC2oO5g9DP+DQ= -github.com/dgrijalva/jwt-go v0.0.0-20160621201154-c9eaceb2896d/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/go-kit/kit v0.0.0-20161109000648-9f5c614cd1e7/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-stack/stack v1.5.3/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b h1:fE/yi9pibxGEc0gSJuEShcsBXE2d5FW3OudsjE9tKzQ= @@ -24,14 +22,20 @@ github.com/huandu/xstrings v0.0.0-20151130125119-3959339b3335 h1:KZOP9q7J/P4eMBi github.com/huandu/xstrings v0.0.0-20151130125119-3959339b3335/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbcucJdbSo= github.com/imdario/mergo v0.0.0-20171009183408-7fe0c75c13ab h1:k/Biv+LJL35wkk0Hveko1nj7as4tSHkHdZaNlzn/gcQ= github.com/imdario/mergo v0.0.0-20171009183408-7fe0c75c13ab/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= -github.com/kr/fs v0.0.0-20131111012553-2788f0dbd169/go.mod h1:glhvuHOU9Hy7/8PwwdtnarXqLagOX0b/TbZx2zLMqEg= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.1.0 h1:B9KXyj+GzIpJbV7gmr873NsY6zpbxNy24CBtGrk7jHo= github.com/satori/go.uuid v1.1.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/crypto v0.0.0-20170516161655-0fe963104e9d h1:qjfFh1YMn6m60QTGoG+IlwUMhNlXJbDyCa6EkHM/N2w= golang.org/x/crypto v0.0.0-20170516161655-0fe963104e9d/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/net v0.0.0-20170108160505-da2b4fa28524/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/text v0.0.0-20161216064924-a49bea13b776/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= google.golang.org/genproto v0.0.0-20170517234824-bb3573be0c48 h1:xfoW+Di7qQQUnjptcGb4/rpc701e3RvfNmaywfMxQVI= google.golang.org/genproto v0.0.0-20170517234824-bb3573be0c48/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/grpc v1.0.4/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/helpers/helpers.go b/helpers/helpers.go index fd6ea99..a45571b 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -147,6 +147,7 @@ var ProtoHelpersFuncMap = template.FuncMap{ "trailingComment": trailingComment, "leadingDetachedComments": leadingDetachedComments, "stringFieldExtension": stringFieldExtension, + "int64FieldExtension": int64FieldExtension, "stringMethodOptionsExtension": stringMethodOptionsExtension, "boolMethodOptionsExtension": boolMethodOptionsExtension, "boolFieldExtension": boolFieldExtension, @@ -377,6 +378,41 @@ func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) str return *str } +func int64FieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) int64 { + if f == nil { + return 0 + } + if f.Options == nil { + return 0 + } + var extendedType *descriptor.FieldOptions + var extensionType *string + + 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 0 + } + + i, ok := ext.(*int64) + if !ok { + return 0 + } + + return *i +} + func boolMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) bool { if f == nil { return false @@ -393,7 +429,7 @@ func boolMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorPro ExtendedType: extendedType, ExtensionType: extensionType, Field: fieldID, - Tag: fmt.Sprintf("bytes,%d", fieldID), + Tag: fmt.Sprintf("varint,%d", fieldID), } proto.RegisterExtension(ed) eds = proto.RegisteredExtensions(f.Options)