@@ -33,10 +33,34 @@ func Version() string {
33
33
return "devel"
34
34
}
35
35
36
+ // JSONName returns the default JSON name for a field with the given name.
37
+ // This mirrors the algorithm in protoc:
38
+ //
39
+ // https://github.com/protocolbuffers/protobuf/blob/v21.3/src/google/protobuf/descriptor.cc#L95
40
+ func JSONName (name string ) string {
41
+ var jsonName []rune
42
+ nextUpper := false
43
+ for _ , chr := range name {
44
+ if chr == '_' {
45
+ nextUpper = true
46
+ continue
47
+ }
48
+ if nextUpper {
49
+ nextUpper = false
50
+ jsonName = append (jsonName , unicode .ToUpper (chr ))
51
+ } else {
52
+ jsonName = append (jsonName , chr )
53
+ }
54
+ }
55
+ return string (jsonName )
56
+ }
57
+
58
+ // GetFieldSchema returns the schema.FieldSchema options for the given field.
36
59
func GetFieldSchema (field protoreflect.FieldDescriptor ) (* schema.FieldSchema , error ) {
37
60
return getExt [* schema.FieldSchema ](field .Options (), schema .E_Field )
38
61
}
39
62
63
+ // GetFieldAliases returns the name and JSON name aliases for the given field.
40
64
func GetFieldAliases (fieldSchema * schema.FieldSchema ) ([]protoreflect.Name , []string , error ) {
41
65
aliases , err := getFieldAliases (fieldSchema )
42
66
if err != nil {
@@ -90,25 +114,3 @@ func getExt[T proto.Message](options proto.Message, extType protoreflect.Extensi
90
114
}
91
115
return extProto , proto .Unmarshal (extData , extProto )
92
116
}
93
-
94
- // JSONName returns the default JSON name for a field with the given name.
95
- // This mirrors the algorithm in protoc:
96
- //
97
- // https://github.com/protocolbuffers/protobuf/blob/v21.3/src/google/protobuf/descriptor.cc#L95
98
- func JSONName (name string ) string {
99
- var jsonName []rune
100
- nextUpper := false
101
- for _ , chr := range name {
102
- if chr == '_' {
103
- nextUpper = true
104
- continue
105
- }
106
- if nextUpper {
107
- nextUpper = false
108
- jsonName = append (jsonName , unicode .ToUpper (chr ))
109
- } else {
110
- jsonName = append (jsonName , chr )
111
- }
112
- }
113
- return string (jsonName )
114
- }
0 commit comments