From 5cdaa646d173e53874ce6f006c306d1f6a3ca050 Mon Sep 17 00:00:00 2001 From: Joshua Pritchard Date: Wed, 12 Jul 2023 17:54:18 -0400 Subject: [PATCH 1/3] use deepcopy for k8s types --- templates/clone/messages.go | 4 ++++ templates/clone/render.go | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/templates/clone/messages.go b/templates/clone/messages.go index 805268c..22f9d31 100644 --- a/templates/clone/messages.go +++ b/templates/clone/messages.go @@ -8,6 +8,10 @@ const messageTpl = ` } ` +const messageGogoTpl = ` + {{ .TargetName }} = {{ .Name }}.DeepCopy() +` + const oneofMessageTpl = ` if h, ok := interface{}({{ .Name }}).(clone.Cloner); ok { target.{{ .OneOfInterface }} = &{{ oneofNonPointer .Field }}{ diff --git a/templates/clone/render.go b/templates/clone/render.go index 3b1f8f0..bc189b0 100644 --- a/templates/clone/render.go +++ b/templates/clone/render.go @@ -3,6 +3,7 @@ package clone import ( "bytes" "errors" + "strings" "text/template" pgs "github.com/lyft/protoc-gen-star" @@ -38,8 +39,12 @@ func (fns goSharedFuncs) render(field pgs.Field) (string, error) { case pgs.StringT: tpl = template.Must(fns.tpl.New("string").Parse(stringTpl)) case pgs.MessageT: - tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) typeName = fns.typeName(field) + if strings.HasPrefix(typeName, "*k8s_io_") { + tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) + } else { + tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) + } default: return "", errors.New("unknown type") } @@ -158,8 +163,12 @@ func (fns goSharedFuncs) simpleRender( case pgs.StringT: tpl = template.Must(fns.tpl.New("string").Parse(stringTpl)) case pgs.MessageT: - tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) typeName = fns.entityTypeName(field, typeElem) + if strings.HasPrefix(typeName, "*k8s_io_") { + tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) + } else { + tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) + } default: return "", errors.New("unknown type") } From bc5fcba3c484b316cd8f605eb30296e4188739dd Mon Sep 17 00:00:00 2001 From: Joshua Pritchard Date: Thu, 13 Jul 2023 14:37:30 -0400 Subject: [PATCH 2/3] use more direct comparison --- templates/clone/functions.go | 21 +++++++++++++++++++++ templates/clone/render.go | 12 +++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/templates/clone/functions.go b/templates/clone/functions.go index 18309c7..0721a46 100644 --- a/templates/clone/functions.go +++ b/templates/clone/functions.go @@ -90,6 +90,18 @@ func (fns goSharedFuncs) importableTypeName(f pgs.Field, e pgs.Entity) string { return fmt.Sprintf("*%s.%s", fns.packageName(e), t) } +func (fns goSharedFuncs) fieldPackageName(field pgs.Field) string { + if field.Type().IsMap() || field.Type().IsRepeated() { + if field.Type().Element().IsEmbed() { + e := field.Type().Element().Embed() + return fns.packageName(e) + } + } + + return fns.packageName(field.Type().Embed()) + +} + func (fns goSharedFuncs) packageName(e pgs.Entity) string { importName := fns.ImportPath(e).String() importName = strings.ReplaceAll(importName, "/", "_") @@ -215,3 +227,12 @@ func (fns goSharedFuncs) externalPackages( func (fns goSharedFuncs) snakeCase(name string) string { return strcase.ToSnake(name) } + +func (fns goSharedFuncs) contains(strings []string, s string) bool { + for _, str := range strings { + if str == s { + return true + } + } + return false +} diff --git a/templates/clone/render.go b/templates/clone/render.go index bc189b0..d7ae018 100644 --- a/templates/clone/render.go +++ b/templates/clone/render.go @@ -3,12 +3,16 @@ package clone import ( "bytes" "errors" - "strings" "text/template" pgs "github.com/lyft/protoc-gen-star" ) +// packages that use k8s/gogo and require DeepCopy because they do not support proto.Clone or Reflect +var gogoPackages = []string{ + "k8s_io_api_core_v1", +} + type Value struct { Name string TargetName string @@ -40,7 +44,8 @@ func (fns goSharedFuncs) render(field pgs.Field) (string, error) { tpl = template.Must(fns.tpl.New("string").Parse(stringTpl)) case pgs.MessageT: typeName = fns.typeName(field) - if strings.HasPrefix(typeName, "*k8s_io_") { + packageName := fns.fieldPackageName(field) + if fns.contains(gogoPackages, packageName) { tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) } else { tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) @@ -164,7 +169,8 @@ func (fns goSharedFuncs) simpleRender( tpl = template.Must(fns.tpl.New("string").Parse(stringTpl)) case pgs.MessageT: typeName = fns.entityTypeName(field, typeElem) - if strings.HasPrefix(typeName, "*k8s_io_") { + packageName := fns.fieldPackageName(field) + if fns.contains(gogoPackages, packageName) { tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) } else { tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) From f95e5b445d560748fc4c541a251f114b61b131f7 Mon Sep 17 00:00:00 2001 From: Joshua Pritchard Date: Thu, 13 Jul 2023 16:07:56 -0400 Subject: [PATCH 3/3] use param --- templates/clone/render.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/templates/clone/render.go b/templates/clone/render.go index d7ae018..437df20 100644 --- a/templates/clone/render.go +++ b/templates/clone/render.go @@ -3,15 +3,14 @@ package clone import ( "bytes" "errors" + "strings" "text/template" pgs "github.com/lyft/protoc-gen-star" ) // packages that use k8s/gogo and require DeepCopy because they do not support proto.Clone or Reflect -var gogoPackages = []string{ - "k8s_io_api_core_v1", -} +var gogoPackagesParamName = "gogo_packages" type Value struct { Name string @@ -45,7 +44,7 @@ func (fns goSharedFuncs) render(field pgs.Field) (string, error) { case pgs.MessageT: typeName = fns.typeName(field) packageName := fns.fieldPackageName(field) - if fns.contains(gogoPackages, packageName) { + if fns.contains(strings.Split(fns.Params().Str(gogoPackagesParamName), "|"), packageName) { tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) } else { tpl = template.Must(fns.tpl.New("message").Parse(messageTpl)) @@ -170,7 +169,7 @@ func (fns goSharedFuncs) simpleRender( case pgs.MessageT: typeName = fns.entityTypeName(field, typeElem) packageName := fns.fieldPackageName(field) - if fns.contains(gogoPackages, packageName) { + if fns.contains(strings.Split(fns.Params().Str(gogoPackagesParamName), "|"), packageName) { tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl)) } else { tpl = template.Must(fns.tpl.New("message").Parse(messageTpl))