Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
873 B

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package resourcehcl
import (
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
)
// fieldNamer implements protohcl.FieldNamer to name fields using PascalCase
// with support for acroynms (e.g. ID, TCP).
type fieldNamer struct{ acroynms []string }
func (n fieldNamer) NameField(fd protoreflect.FieldDescriptor) string {
camel := fd.JSONName()
upper := strings.ToUpper(camel)
for _, a := range n.acroynms {
if upper == a {
return a
}
}
return strings.ToUpper(camel[:1]) + camel[1:]
}
func (n fieldNamer) GetField(fds protoreflect.FieldDescriptors, name string) protoreflect.FieldDescriptor {
for _, a := range n.acroynms {
if name == a {
return fds.ByJSONName(strings.ToLower(a))
}
}
camel := strings.ToLower(name[:1]) + name[1:]
return fds.ByJSONName(camel)
}