Add stringFileOptionsExtension (#144)

Add stringFileOptionsExtension
This commit is contained in:
Manfred Touron 2019-05-30 20:12:25 +02:00 committed by GitHub
commit 5b5d8bf81e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -146,6 +146,7 @@ var ProtoHelpersFuncMap = template.FuncMap{
"leadingComment": leadingComment, "leadingComment": leadingComment,
"trailingComment": trailingComment, "trailingComment": trailingComment,
"leadingDetachedComments": leadingDetachedComments, "leadingDetachedComments": leadingDetachedComments,
"stringFileOptionsExtension": stringFileOptionsExtension,
"stringMessageExtension": stringMessageExtension, "stringMessageExtension": stringMessageExtension,
"stringFieldExtension": stringFieldExtension, "stringFieldExtension": stringFieldExtension,
"int64FieldExtension": int64FieldExtension, "int64FieldExtension": int64FieldExtension,
@ -348,6 +349,46 @@ func stringMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorP
return *str return *str
} }
// stringFileOptionsExtension extracts file 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 stringFileOptionsExtension(fieldID int32, f *descriptor.FileDescriptorProto) string {
if f == nil {
return ""
}
if f.Options == nil {
return ""
}
var extendedType *descriptor.FileOptions
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 ""