Poonam Jadhav 559c61e6b6
Net-2712/resource hcl parsing (#18250)
* Initial protohcl implementation

Co-authored-by: Matt Keeler <mkeeler@users.noreply.github.com>
Co-authored-by: Daniel Upton <daniel@floppy.co>

* resourcehcl: implement resource decoding on top of protohcl

Co-authored-by: Daniel Upton <daniel@floppy.co>

* fix: resolve ci failures

* test: add additional unmarshalling tests

* refactor: update function test to clean protohcl package imports

---------

Co-authored-by: Matt Keeler <mkeeler@users.noreply.github.com>
Co-authored-by: Daniel Upton <daniel@floppy.co>
2023-08-11 15:52:51 -04:00

36 lines
802 B
Go

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)
}