Skip to content

Commit

Permalink
use more direct comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-pritchard committed Jul 13, 2023
1 parent 5cdaa64 commit bc5fcba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions templates/clone/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/", "_")
Expand Down Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions templates/clone/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit bc5fcba

Please sign in to comment.