Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gotohelm: fix map indexing and zeroOf #359

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/aaacommon/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/astrewrites/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/directives/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/flowcontrol/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/inputs/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/k8s/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
7 changes: 7 additions & 0 deletions pkg/gotohelm/testdata/src/example/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,16 @@ func quantity(dot *helmette.Dot) map[string]any {
value = append(value, q.Value())
}

// Intentionally generate zero values of resource.Quantity to assert that
// zeroOf handles it.
var varZero resource.Quantity
resources := corev1.ResourceList{}

return map[string]any{
"MustParse": quantities,
"Value": value,
"String": strs,
"dictZero": resources[corev1.ResourceCPU],
"varZero": varZero,
}
}
7 changes: 7 additions & 0 deletions pkg/gotohelm/testdata/src/example/k8s/k8s.rewritten.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,16 @@ func quantity(dot *helmette.Dot) map[string]any {
value = append(value, q.Value())
}

// Intentionally generate zero values of resource.Quantity to assert that
// zeroOf handles it.
var varZero resource.Quantity
resources := corev1.ResourceList{}

return map[string]any{
"MustParse": quantities,
"Value": value,
"String": strs,
"dictZero": resources[corev1.ResourceCPU],
"varZero": varZero,
}
}
4 changes: 3 additions & 1 deletion pkg/gotohelm/testdata/src/example/k8s/k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@
{{- if $_is_returning -}}
{{- break -}}
{{- end -}}
{{- $varZero := "0" -}}
{{- $resources := (dict ) -}}
{{- $_is_returning = true -}}
{{- (dict "r" (dict "MustParse" $quantities "Value" $value "String" $strs )) | toJson -}}
{{- (dict "r" (dict "MustParse" $quantities "Value" $value "String" $strs "dictZero" (ternary (index $resources "cpu") "0" (hasKey $resources "cpu")) "varZero" $varZero )) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/labels/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/sprig/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/syntax/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
6 changes: 6 additions & 0 deletions pkg/gotohelm/testdata/src/example/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ func binaryExprs() []any {
s1 + s2,
"one" + s2,
s1 + "two",
// Ensure that indexing into a map with a missing key generates the
// correct zero value.
map[string]int{}["missing"],
map[string]string{}["missing"],
map[string]bool{}["missing"],
map[string]struct{ Foo int }{}["missing"],
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ func binaryExprs() []any {
s1 + s2,
"one" + s2,
s1 + "two",
// Ensure that indexing into a map with a missing key generates the
// correct zero value.
map[string]int{}["missing"],
map[string]string{}["missing"],
map[string]bool{}["missing"],
map[string]struct{ Foo int }{}["missing"],
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/syntax/syntax.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
{{- $s1 := "one " -}}
{{- $s2 := "two" -}}
{{- $_is_returning = true -}}
{{- (dict "r" (list (gt (1 | int) (2 | int)) (lt (1 | int) (2 | int)) (ge (1 | int) (2 | int)) (le (1 | int) (2 | int)) (ge (2 | int) (2 | int)) (le (2 | int) (2 | int)) (printf "%s%s" "string " "concatenation") (printf "%s%s" $s1 $s2) (printf "%s%s" "one" $s2) (printf "%s%s" $s1 "two"))) | toJson -}}
{{- (dict "r" (list (gt (1 | int) (2 | int)) (lt (1 | int) (2 | int)) (ge (1 | int) (2 | int)) (le (1 | int) (2 | int)) (ge (2 | int) (2 | int)) (le (2 | int) (2 | int)) (printf "%s%s" "string " "concatenation") (printf "%s%s" $s1 $s2) (printf "%s%s" "one" $s2) (printf "%s%s" $s1 "two") (ternary (index (dict ) "missing") 0 (hasKey (dict ) "missing")) (ternary (index (dict ) "missing") "" (hasKey (dict ) "missing")) (ternary (index (dict ) "missing") false (hasKey (dict ) "missing")) (ternary (index (dict ) "missing") (dict "Foo" 0 ) (hasKey (dict ) "missing")))) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gotohelm/testdata/src/example/typing/_shims.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
{{- $_ := (fail (printf "invalid Duration: %q" $original)) -}}
{{- end -}}
{{- $repr = (substr ((get (fromJson (include "_shims.len" (dict "a" (list $unit) ))) "r") | int) -1 $repr) -}}
{{- $value = ((add $value (((mul (int64 $n) (index $unitMap $unit)) | int64))) | int64) -}}
{{- $value = ((add $value (((mul (int64 $n) (ternary (index $unitMap $unit) 0 (hasKey $unitMap $unit))) | int64))) | int64) -}}
{{- end -}}
{{- if $_is_returning -}}
{{- break -}}
Expand Down
67 changes: 64 additions & 3 deletions pkg/gotohelm/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,46 @@ func (t *Transpiler) transpileExpr(n ast.Expr) Node {
}

case *ast.IndexExpr:
// IndexExprs handle (n.X)[n.Index] as an expression i.e. map tests (_,
// _ = X[Index]) are NOT handled here (see
// [Transpiler.transpileMVAssignStmt]).

switch typ := t.typeOf(n.X).Underlying().(type) {
case *types.Map:
// if the zero value of elem's type is NOT nil, we need to
// replicate go's behavior of returning zero values on missing
// keys.
elemZero := t.zeroOf(typ.Elem())
if _, ok := elemZero.(*Nil); !ok {
// This behavior is replicated using sprig's ternary function.
// Admittedly, this could cause unexpected behavior if either
// expr or index are particularly complex or have side effects.
// In practice, this, thankfully, has not been the case.
expr := t.transpileExpr(n.X)
index := t.transpileExpr(n.Index)

// X[index] -> (ternary (index X index) (zero) (hasKey X index))
return &BuiltInCall{
// Ternary signature is (trueVal, falseVal, testVal)
FuncName: "ternary",
Arguments: []Node{
&BuiltInCall{
FuncName: "index",
Arguments: []Node{expr, index},
},
elemZero,
&BuiltInCall{
FuncName: "hasKey",
Arguments: []Node{expr, index},
},
},
}
}
}

// Otherwise, text/template's builtin index function handles everything
// for us as it returns nil for missing keys due to all maps being
// `map[string]any`'s.
return &BuiltInCall{
FuncName: "index",
Arguments: []Node{
Expand Down Expand Up @@ -1419,9 +1459,6 @@ func (t *Transpiler) typeOf(expr ast.Expr) types.Type {
}

func (t *Transpiler) zeroOf(typ types.Type) Node {
// TODO need to detect and reject or special case implementors of
// json.Marshaler. Getting a handle to a that interface is... difficult.

// Special cases.
switch typ.String() {
case "k8s.io/apimachinery/pkg/apis/meta/v1.Time":
Expand All @@ -1430,6 +1467,30 @@ func (t *Transpiler) zeroOf(typ types.Type) Node {
// IntOrString's zero value appears to marshal to a 0 though it's
// unclear how correct this is.
return &Literal{Value: "0"}
case "k8s.io/apimachinery/pkg/api/resource.Quantity":
return &Literal{Value: `"0"`}
}

// If encoding/json is in the dependency chain for this package, we'll
// enable some additional checks (because it's other wise very difficult to
// check for implementation of {M,Unm}arshaller...)
if json, ok := t.packages["encoding/json"]; ok {
marshaller := json.Types.Scope().Lookup("Marshaler").Type().Underlying().(*types.Interface)
unmarshaller := json.Types.Scope().Lookup("Unmarshaler").Type().Underlying().(*types.Interface)

ptr := types.NewPointer(typ)

// If we can get a handle to Marshaler and Unmarshaler, we'll error out
// on any types that implement either and aren't special cased as their
// JSON representation likely won't match what we can infer from the go
// type.
switch {
case types.Implements(typ, unmarshaller),
types.Implements(typ, marshaller),
types.Implements(ptr, marshaller),
types.Implements(ptr, unmarshaller):
panic(fmt.Sprintf("unsupported type %q implemented json.Unmarshaler or json.Marshaller and needs to be special cased but isn't currently", typ.String()))
}
}

switch underlying := typ.Underlying().(type) {
Expand Down