Skip to content

Commit

Permalink
Merge pull request #22 from solo-io/support-k8s-protos
Browse files Browse the repository at this point in the history
Support k8s Types
  • Loading branch information
josh-pritchard authored Jul 13, 2023
2 parents cbddbd7 + f95e5b4 commit fe1999e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 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
}
4 changes: 4 additions & 0 deletions templates/clone/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}{
Expand Down
18 changes: 16 additions & 2 deletions templates/clone/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ 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 gogoPackagesParamName = "gogo_packages"

type Value struct {
Name string
TargetName string
Expand Down Expand Up @@ -38,8 +42,13 @@ 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)
packageName := fns.fieldPackageName(field)
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))
}
default:
return "", errors.New("unknown type")
}
Expand Down Expand Up @@ -158,8 +167,13 @@ 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)
packageName := fns.fieldPackageName(field)
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))
}
default:
return "", errors.New("unknown type")
}
Expand Down

0 comments on commit fe1999e

Please sign in to comment.