diff --git a/templates/hash/functions.go b/templates/hash/functions.go index 6b6d4d2..5bff0b3 100644 --- a/templates/hash/functions.go +++ b/templates/hash/functions.go @@ -11,6 +11,8 @@ import ( pgsgo "github.com/lyft/protoc-gen-star/lang/go" ) +const TRANSFORM_PACKAGE_FOR_HASH_PARAMETER_NAME = "transform_package_for_hash" + func register(tpl *template.Template, params pgs.Parameters) { fns := goSharedFuncs{ Context: pgsgo.InitContext(params), @@ -52,8 +54,24 @@ func (fns goSharedFuncs) fieldName(field pgs.Field) string { return fmt.Sprintf("%s", fns.Name(field)) } +// fullPackageName constructs and returns the full package name of a message as a string. +// If the parameter transform_package_for_hash is set, using the format "old=new", the function will +// replace all instances of "old" with "new" in the full package name before returning the new value. +// If the parameter transform_package_for_hash does not have a empty or valid value, the function will panic. func (fns goSharedFuncs) fullPackageName(msg pgs.Message) string { - return fmt.Sprintf("%s.%s.%s", msg.Package().ProtoName(), fns.ImportPath(msg), fns.Name(msg)) + fullPackageName := fmt.Sprintf("%s.%s.%s", msg.Package().ProtoName(), fns.ImportPath(msg), fns.Name(msg)) + transformPackageForHash := fns.Params().Str(TRANSFORM_PACKAGE_FOR_HASH_PARAMETER_NAME) + if transformPackageForHash == "" { + return fullPackageName + } + + replaceParts := strings.Split(transformPackageForHash, "=") + if len(replaceParts) != 2 { + // If the valud in the parameter is not in the format "old=new", the process should fail + panic(fmt.Sprintf("Invalid transform string: %s", transformPackageForHash)) + } + + return strings.ReplaceAll(fullPackageName, replaceParts[0], replaceParts[1]) } func (fns goSharedFuncs) lookup(f pgs.Field, name string) string { diff --git a/tests/api/hello.pb.go b/tests/api/hello.pb.go index 1b2ecad..a13b885 100644 --- a/tests/api/hello.pb.go +++ b/tests/api/hello.pb.go @@ -391,6 +391,7 @@ type Nested struct { Initial map[string]*Simple `protobuf:"bytes,9,rep,name=initial,proto3" json:"initial,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` SimpleMap map[string]string `protobuf:"bytes,10,rep,name=simple_map,json=simpleMap,proto3" json:"simple_map,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // Types that are assignable to TestOneOf: + // // *Nested_EmptyOneOf // *Nested_NestedOneOf // *Nested_PrimitiveOneOf