Renames for readability in conversion-gen

This commit is contained in:
Tim Hockin 2016-09-26 00:34:25 -07:00
parent 210a634bcc
commit 3023decd00

View File

@ -693,68 +693,68 @@ func (g *genConversion) doSlice(inType, outType *types.Type, sw *generator.Snipp
} }
func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.SnippetWriter) { func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.SnippetWriter) {
for _, m := range inType.Members { for _, inMember := range inType.Members {
// Check if this member is excluded from conversion // Check if this member is excluded from conversion
if tagvals := extractTag(m.CommentLines); tagvals != nil && tagvals[0] == "false" { if tagvals := extractTag(inMember.CommentLines); tagvals != nil && tagvals[0] == "false" {
continue continue
} }
outMember, isOutMember := findMember(outType, m.Name) outMember, found := findMember(outType, inMember.Name)
if !isOutMember { if !found {
// Since this object wasn't filtered out, this means that // Since this object wasn't filtered out, this means that
// this field has "+k8s:conversion-gen=false" comment to ignore it. // this field has "+k8s:conversion-gen=false" comment to ignore it.
continue continue
} }
t, outT := m.Type, outMember.Type inMemberType, outMemberType := inMember.Type, outMember.Type
// create a copy of both underlying types but give them the top level alias name (since aliases // create a copy of both underlying types but give them the top level alias name (since aliases
// are assignable) // are assignable)
if underlying := unwrapAlias(t); underlying != t { if underlying := unwrapAlias(inMemberType); underlying != inMemberType {
copied := *underlying copied := *underlying
copied.Name = t.Name copied.Name = inMemberType.Name
t = &copied inMemberType = &copied
} }
if underlying := unwrapAlias(outT); underlying != outT { if underlying := unwrapAlias(outMemberType); underlying != outMemberType {
copied := *underlying copied := *underlying
copied.Name = outT.Name copied.Name = outMemberType.Name
outT = &copied outMemberType = &copied
} }
args := map[string]interface{}{ args := map[string]interface{}{
"inType": t, "inType": inMemberType,
"outType": outT, "outType": outMemberType,
"name": m.Name, "name": inMember.Name,
} }
// check based on the top level name, not the underlying names // check based on the top level name, not the underlying names
if function, ok := g.preexists(m.Type, outMember.Type); ok { if function, ok := g.preexists(inMember.Type, outMember.Type); ok {
args["function"] = function args["function"] = function
sw.Do("if err := $.function|raw$(&in.$.name$, &out.$.name$, s); err != nil {\n", args) sw.Do("if err := $.function|raw$(&in.$.name$, &out.$.name$, s); err != nil {\n", args)
sw.Do("return err\n", nil) sw.Do("return err\n", nil)
sw.Do("}\n", nil) sw.Do("}\n", nil)
continue continue
} }
switch t.Kind { switch inMemberType.Kind {
case types.Builtin: case types.Builtin:
if t == outT { if inMemberType == outMemberType {
sw.Do("out.$.name$ = in.$.name$\n", args) sw.Do("out.$.name$ = in.$.name$\n", args)
} else { } else {
sw.Do("out.$.name$ = $.outType|raw$(in.$.name$)\n", args) sw.Do("out.$.name$ = $.outType|raw$(in.$.name$)\n", args)
} }
case types.Map, types.Slice, types.Pointer: case types.Map, types.Slice, types.Pointer:
if g.isDirectlyAssignable(t, outT) { if g.isDirectlyAssignable(inMemberType, outMemberType) {
sw.Do("out.$.name$ = in.$.name$\n", args) sw.Do("out.$.name$ = in.$.name$\n", args)
continue continue
} }
sw.Do("if in.$.name$ != nil {\n", args) sw.Do("if in.$.name$ != nil {\n", args)
sw.Do("in, out := &in.$.name$, &out.$.name$\n", args) sw.Do("in, out := &in.$.name$, &out.$.name$\n", args)
g.generateFor(t, outT, sw) g.generateFor(inMemberType, outMemberType, sw)
sw.Do("} else {\n", nil) sw.Do("} else {\n", nil)
sw.Do("out.$.name$ = nil\n", args) sw.Do("out.$.name$ = nil\n", args)
sw.Do("}\n", nil) sw.Do("}\n", nil)
case types.Struct: case types.Struct:
if g.isDirectlyAssignable(t, outT) { if g.isDirectlyAssignable(inMemberType, outMemberType) {
sw.Do("out.$.name$ = in.$.name$\n", args) sw.Do("out.$.name$ = in.$.name$\n", args)
continue continue
} }
if g.convertibleOnlyWithinPackage(t, outT) { if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) {
sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args)
} else { } else {
sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil)
@ -763,14 +763,14 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
sw.Do("return err\n", nil) sw.Do("return err\n", nil)
sw.Do("}\n", nil) sw.Do("}\n", nil)
case types.Alias: case types.Alias:
if isDirectlyAssignable(t, outT) { if isDirectlyAssignable(inMemberType, outMemberType) {
if t == outT { if inMemberType == outMemberType {
sw.Do("out.$.name$ = in.$.name$\n", args) sw.Do("out.$.name$ = in.$.name$\n", args)
} else { } else {
sw.Do("out.$.name$ = $.outType|raw$(in.$.name$)\n", args) sw.Do("out.$.name$ = $.outType|raw$(in.$.name$)\n", args)
} }
} else { } else {
if g.convertibleOnlyWithinPackage(t, outT) { if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) {
sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args)
} else { } else {
sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil)
@ -780,7 +780,7 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
sw.Do("}\n", nil) sw.Do("}\n", nil)
} }
default: default:
if g.convertibleOnlyWithinPackage(t, outT) { if g.convertibleOnlyWithinPackage(inMemberType, outMemberType) {
sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args) sw.Do("if err := "+nameTmpl+"(&in.$.name$, &out.$.name$, s); err != nil {\n", args)
} else { } else {
sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil) sw.Do("// TODO: Inefficient conversion - can we improve it?\n", nil)