Skip to content

Commit c4967f5

Browse files
committed
bugfix: cast to storage type if defaultValue is a reference
otherwise if you have a `string` field, and provide a `type Value string`-valued default, you get an error trying to assign an alias to a string
1 parent f0d687c commit c4967f5

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

examples/defaulter-gen/generators/defaulter.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ func populateDefaultValue(node *callNode, t *types.Type, tags string, commentLin
565565

566566
node.defaultIsPrimitive = baseT.IsPrimitive()
567567
node.defaultType = baseT.String()
568+
node.defaultTopLevelType = t.Name
568569
node.defaultValue.InlineConstant = defaultString
569570
node.defaultValue.SymbolReference = symbolReference
570571
node.defaultDepth = depth
@@ -812,7 +813,7 @@ func (g *genDefaulter) GenerateType(c *generator.Context, t *types.Type, w io.Wr
812813

813814
}
814815

815-
for i, v := range current.defaultDepth {
816+
resolveTypeName := func(v types.Name) types.Name {
816817
var prefix string = ""
817818

818819
// Check for pointer. If it is a pointer type, resolve the element
@@ -827,15 +828,21 @@ func (g *genDefaulter) GenerateType(c *generator.Context, t *types.Type, w io.Wr
827828

828829
if len(v.Name) > 0 && len(v.Package) > 0 {
829830
if g.isOtherPackage(v.Package) {
830-
current.defaultDepth[i] = types.Name{
831+
return types.Name{
831832
Name: prefix + g.imports.LocalNameOf(v.Package) + "." + v.Name,
832833
}
833834
} else {
834-
current.defaultDepth[i] = types.Name{
835+
return types.Name{
835836
Name: prefix + v.Name,
836837
}
837838
}
838839
}
840+
return v
841+
}
842+
843+
current.defaultTopLevelType = resolveTypeName(current.defaultTopLevelType)
844+
for i, v := range current.defaultDepth {
845+
current.defaultDepth[i] = resolveTypeName(v)
839846
}
840847

841848
if len(current.call) == 0 {
@@ -932,6 +939,10 @@ type callNode struct {
932939
// defaultType is the type of the default value.
933940
// Only populated if defaultIsPrimitive is true
934941
defaultType string
942+
943+
// defaultTopLevelType is the final type the value should resolve to
944+
// This is in constrast with default type, which resolves aliases and pointers.
945+
defaultTopLevelType types.Name
935946
}
936947

937948
type defaultValue struct {
@@ -1102,7 +1113,12 @@ func (n *callNode) writeDefaulter(varName string, index string, isVarPointer boo
11021113
args["defaultZero"] = defaultZero
11031114

11041115
sw.Do(fmt.Sprintf("if %s == $.defaultZero$ {\n", variablePlaceholder), args)
1105-
sw.Do(fmt.Sprintf("%s = $.defaultValue$", variablePlaceholder), args)
1116+
1117+
if len(n.defaultValue.SymbolReference) > 0 {
1118+
sw.Do(fmt.Sprintf("%s = %s($.defaultValue$)", variablePlaceholder, n.defaultTopLevelType), args)
1119+
} else {
1120+
sw.Do(fmt.Sprintf("%s = $.defaultValue$", variablePlaceholder), args)
1121+
}
11061122
}
11071123
} else {
11081124
sw.Do(fmt.Sprintf("if %s == nil {\n", variablePlaceholder), args)

0 commit comments

Comments
 (0)