Skip to content

Commit b495c50

Browse files
authored
Add an option to edit the fullPackageName value for the hashing logic (#23)
* add an option to edit the fullPackageName value for the hashing logic * simplify implementation
1 parent fe1999e commit b495c50

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

templates/hash/functions.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
1212
)
1313

14+
const TRANSFORM_PACKAGE_FOR_HASH_PARAMETER_NAME = "transform_package_for_hash"
15+
1416
func register(tpl *template.Template, params pgs.Parameters) {
1517
fns := goSharedFuncs{
1618
Context: pgsgo.InitContext(params),
@@ -52,8 +54,24 @@ func (fns goSharedFuncs) fieldName(field pgs.Field) string {
5254
return fmt.Sprintf("%s", fns.Name(field))
5355
}
5456

57+
// fullPackageName constructs and returns the full package name of a message as a string.
58+
// If the parameter transform_package_for_hash is set, using the format "old=new", the function will
59+
// replace all instances of "old" with "new" in the full package name before returning the new value.
60+
// If the parameter transform_package_for_hash does not have a empty or valid value, the function will panic.
5561
func (fns goSharedFuncs) fullPackageName(msg pgs.Message) string {
56-
return fmt.Sprintf("%s.%s.%s", msg.Package().ProtoName(), fns.ImportPath(msg), fns.Name(msg))
62+
fullPackageName := fmt.Sprintf("%s.%s.%s", msg.Package().ProtoName(), fns.ImportPath(msg), fns.Name(msg))
63+
transformPackageForHash := fns.Params().Str(TRANSFORM_PACKAGE_FOR_HASH_PARAMETER_NAME)
64+
if transformPackageForHash == "" {
65+
return fullPackageName
66+
}
67+
68+
replaceParts := strings.Split(transformPackageForHash, "=")
69+
if len(replaceParts) != 2 {
70+
// If the valud in the parameter is not in the format "old=new", the process should fail
71+
panic(fmt.Sprintf("Invalid transform string: %s", transformPackageForHash))
72+
}
73+
74+
return strings.ReplaceAll(fullPackageName, replaceParts[0], replaceParts[1])
5775
}
5876

5977
func (fns goSharedFuncs) lookup(f pgs.Field, name string) string {

tests/api/hello.pb.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)