sprig
sprig copied to clipboard
toString should handle `*string`
For an argument of type *string, there is no case defined for the toString function, which causes the default case to be selected. The default case uses fmt.Sprintf("%v", v), which returns the address of the pointer. In my opinion, this is almost never the thing, which a user of toString is expected to get in the case of a Go template.
Instead, the referenced value of the pointer should be returned.
Link to current implementation: https://github.com/Masterminds/sprig/blob/master/strings.go#L174
Suggested implementation:
func strval(v interface{}) string {
switch v := v.(type) {
case string:
return v
case *string:
if v == nil {
return ""
}
return *v
case []byte:
return string(v)
case error:
return v.Error()
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
}