Add stringMethodOptionsExtension
This helper function can be used on service methods to read options, which extend method options by a string. e.g. It is possible to read "Some string" for a custom `my_method_option.bar` message option extension. See: https://developers.google.com/protocol-buffers/docs/proto#customoptions on how to define a custom extension and referencing is by a fieldID. ``` service MyService { rpc MyMethod(RequestType) returns(ResponseType) { option (my_method_option.bar) = "Some string"; } } ``` If `my_method_option.bar` was defined for `fieldID: 50000` one can reference it using protoc-gen-template like: ``` {{- stringMethodOptionsExtension 50000 .method }} ```
This commit is contained in:
parent
bb3105a862
commit
6765ff04eb
@ -101,37 +101,38 @@ var ProtoHelpersFuncMap = template.FuncMap{
|
|||||||
}
|
}
|
||||||
return slice.Index(int(i)).Interface()
|
return slice.Index(int(i)).Interface()
|
||||||
},
|
},
|
||||||
"snakeCase": xstrings.ToSnakeCase,
|
"snakeCase": xstrings.ToSnakeCase,
|
||||||
"getProtoFile": getProtoFile,
|
"getProtoFile": getProtoFile,
|
||||||
"getMessageType": getMessageType,
|
"getMessageType": getMessageType,
|
||||||
"getEnumValue": getEnumValue,
|
"getEnumValue": getEnumValue,
|
||||||
"isFieldMessage": isFieldMessage,
|
"isFieldMessage": isFieldMessage,
|
||||||
"isFieldMessageTimeStamp": isFieldMessageTimeStamp,
|
"isFieldMessageTimeStamp": isFieldMessageTimeStamp,
|
||||||
"isFieldRepeated": isFieldRepeated,
|
"isFieldRepeated": isFieldRepeated,
|
||||||
"haskellType": haskellType,
|
"haskellType": haskellType,
|
||||||
"goType": goType,
|
"goType": goType,
|
||||||
"goZeroValue": goZeroValue,
|
"goZeroValue": goZeroValue,
|
||||||
"goTypeWithPackage": goTypeWithPackage,
|
"goTypeWithPackage": goTypeWithPackage,
|
||||||
"jsType": jsType,
|
"jsType": jsType,
|
||||||
"jsSuffixReserved": jsSuffixReservedKeyword,
|
"jsSuffixReserved": jsSuffixReservedKeyword,
|
||||||
"namespacedFlowType": namespacedFlowType,
|
"namespacedFlowType": namespacedFlowType,
|
||||||
"httpVerb": httpVerb,
|
"httpVerb": httpVerb,
|
||||||
"httpPath": httpPath,
|
"httpPath": httpPath,
|
||||||
"httpPathsAdditionalBindings": httpPathsAdditionalBindings,
|
"httpPathsAdditionalBindings": httpPathsAdditionalBindings,
|
||||||
"httpBody": httpBody,
|
"httpBody": httpBody,
|
||||||
"shortType": shortType,
|
"shortType": shortType,
|
||||||
"urlHasVarsFromMessage": urlHasVarsFromMessage,
|
"urlHasVarsFromMessage": urlHasVarsFromMessage,
|
||||||
"lowerGoNormalize": lowerGoNormalize,
|
"lowerGoNormalize": lowerGoNormalize,
|
||||||
"goNormalize": goNormalize,
|
"goNormalize": goNormalize,
|
||||||
"leadingComment": leadingComment,
|
"leadingComment": leadingComment,
|
||||||
"trailingComment": trailingComment,
|
"trailingComment": trailingComment,
|
||||||
"leadingDetachedComments": leadingDetachedComments,
|
"leadingDetachedComments": leadingDetachedComments,
|
||||||
"stringFieldExtension": stringFieldExtension,
|
"stringFieldExtension": stringFieldExtension,
|
||||||
"boolFieldExtension": boolFieldExtension,
|
"stringMethodOptionsExtension": stringMethodOptionsExtension,
|
||||||
"isFieldMap": isFieldMap,
|
"boolFieldExtension": boolFieldExtension,
|
||||||
"fieldMapKeyType": fieldMapKeyType,
|
"isFieldMap": isFieldMap,
|
||||||
"fieldMapValueType": fieldMapValueType,
|
"fieldMapKeyType": fieldMapKeyType,
|
||||||
"replaceDict": replaceDict,
|
"fieldMapValueType": fieldMapValueType,
|
||||||
|
"replaceDict": replaceDict,
|
||||||
}
|
}
|
||||||
|
|
||||||
var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location
|
var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location
|
||||||
@ -234,6 +235,46 @@ func leadingDetachedComments(i interface{}) []string {
|
|||||||
return loc.GetLeadingDetachedComments()
|
return loc.GetLeadingDetachedComments()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stringMethodOptionsExtension extracts method options of a string type.
|
||||||
|
// To define your own extensions see:
|
||||||
|
// https://developers.google.com/protocol-buffers/docs/proto#customoptions
|
||||||
|
// Typically the fieldID of private extensions should be in the range:
|
||||||
|
// 50000-99999
|
||||||
|
func stringMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) string {
|
||||||
|
if f == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if f.Options == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var extendedType *descriptor.MethodOptions
|
||||||
|
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 ""
|
||||||
|
}
|
||||||
|
|
||||||
|
str, ok := ext.(*string)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return *str
|
||||||
|
}
|
||||||
|
|
||||||
func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) string {
|
func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) string {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return ""
|
return ""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user