Skip to content

Commit

Permalink
Add an option to edit the fullPackageName value for the hashing logic (
Browse files Browse the repository at this point in the history
…#23)

* add an option to edit the fullPackageName value for the hashing logic

* simplify implementation
  • Loading branch information
nikolasmatt authored Jun 12, 2024
1 parent fe1999e commit b495c50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion templates/hash/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions tests/api/hello.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b495c50

Please sign in to comment.