Skip to content

Commit fe1999e

Browse files
Merge pull request #22 from solo-io/support-k8s-protos
Support k8s Types
2 parents cbddbd7 + f95e5b4 commit fe1999e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

templates/clone/functions.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ func (fns goSharedFuncs) importableTypeName(f pgs.Field, e pgs.Entity) string {
9090
return fmt.Sprintf("*%s.%s", fns.packageName(e), t)
9191
}
9292

93+
func (fns goSharedFuncs) fieldPackageName(field pgs.Field) string {
94+
if field.Type().IsMap() || field.Type().IsRepeated() {
95+
if field.Type().Element().IsEmbed() {
96+
e := field.Type().Element().Embed()
97+
return fns.packageName(e)
98+
}
99+
}
100+
101+
return fns.packageName(field.Type().Embed())
102+
103+
}
104+
93105
func (fns goSharedFuncs) packageName(e pgs.Entity) string {
94106
importName := fns.ImportPath(e).String()
95107
importName = strings.ReplaceAll(importName, "/", "_")
@@ -215,3 +227,12 @@ func (fns goSharedFuncs) externalPackages(
215227
func (fns goSharedFuncs) snakeCase(name string) string {
216228
return strcase.ToSnake(name)
217229
}
230+
231+
func (fns goSharedFuncs) contains(strings []string, s string) bool {
232+
for _, str := range strings {
233+
if str == s {
234+
return true
235+
}
236+
}
237+
return false
238+
}

templates/clone/messages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const messageTpl = `
88
}
99
`
1010

11+
const messageGogoTpl = `
12+
{{ .TargetName }} = {{ .Name }}.DeepCopy()
13+
`
14+
1115
const oneofMessageTpl = `
1216
if h, ok := interface{}({{ .Name }}).(clone.Cloner); ok {
1317
target.{{ .OneOfInterface }} = &{{ oneofNonPointer .Field }}{

templates/clone/render.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ package clone
33
import (
44
"bytes"
55
"errors"
6+
"strings"
67
"text/template"
78

89
pgs "github.com/lyft/protoc-gen-star"
910
)
1011

12+
// packages that use k8s/gogo and require DeepCopy because they do not support proto.Clone or Reflect
13+
var gogoPackagesParamName = "gogo_packages"
14+
1115
type Value struct {
1216
Name string
1317
TargetName string
@@ -38,8 +42,13 @@ func (fns goSharedFuncs) render(field pgs.Field) (string, error) {
3842
case pgs.StringT:
3943
tpl = template.Must(fns.tpl.New("string").Parse(stringTpl))
4044
case pgs.MessageT:
41-
tpl = template.Must(fns.tpl.New("message").Parse(messageTpl))
4245
typeName = fns.typeName(field)
46+
packageName := fns.fieldPackageName(field)
47+
if fns.contains(strings.Split(fns.Params().Str(gogoPackagesParamName), "|"), packageName) {
48+
tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl))
49+
} else {
50+
tpl = template.Must(fns.tpl.New("message").Parse(messageTpl))
51+
}
4352
default:
4453
return "", errors.New("unknown type")
4554
}
@@ -158,8 +167,13 @@ func (fns goSharedFuncs) simpleRender(
158167
case pgs.StringT:
159168
tpl = template.Must(fns.tpl.New("string").Parse(stringTpl))
160169
case pgs.MessageT:
161-
tpl = template.Must(fns.tpl.New("message").Parse(messageTpl))
162170
typeName = fns.entityTypeName(field, typeElem)
171+
packageName := fns.fieldPackageName(field)
172+
if fns.contains(strings.Split(fns.Params().Str(gogoPackagesParamName), "|"), packageName) {
173+
tpl = template.Must(fns.tpl.New("message").Parse(messageGogoTpl))
174+
} else {
175+
tpl = template.Must(fns.tpl.New("message").Parse(messageTpl))
176+
}
163177
default:
164178
return "", errors.New("unknown type")
165179
}

0 commit comments

Comments
 (0)