Skip to content

Commit 844ee9a

Browse files
committed
some docs
1 parent 4298f88 commit 844ee9a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

internal/protoschema/protoschema.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,34 @@ func Version() string {
3333
return "devel"
3434
}
3535

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.
3659
func GetFieldSchema(field protoreflect.FieldDescriptor) (*schema.FieldSchema, error) {
3760
return getExt[*schema.FieldSchema](field.Options(), schema.E_Field)
3861
}
3962

63+
// GetFieldAliases returns the name and JSON name aliases for the given field.
4064
func GetFieldAliases(fieldSchema *schema.FieldSchema) ([]protoreflect.Name, []string, error) {
4165
aliases, err := getFieldAliases(fieldSchema)
4266
if err != nil {
@@ -90,25 +114,3 @@ func getExt[T proto.Message](options proto.Message, extType protoreflect.Extensi
90114
}
91115
return extProto, proto.Unmarshal(extData, extProto)
92116
}
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

Comments
 (0)