Fix rendering nil interfaces as "<nil>" string
This commit is contained in:
parent
79151d3fc7
commit
85c7126bbd
2 changed files with 42 additions and 1 deletions
36
util/decoder_test.go
Normal file
36
util/decoder_test.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDecodeNil(t *testing.T) {
|
||||
var dst struct {
|
||||
User, Password string
|
||||
}
|
||||
|
||||
decoderConfig := &mapstructure.DecoderConfig{
|
||||
Result: &dst,
|
||||
ErrorUnused: true,
|
||||
WeaklyTypedInput: true,
|
||||
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.TextUnmarshallerHookFunc(),
|
||||
),
|
||||
}
|
||||
|
||||
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = decoder.Decode(map[string]any{
|
||||
"user": nil,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, struct {
|
||||
User, Password string
|
||||
}{}, dst)
|
||||
}
|
||||
|
|
@ -323,7 +323,12 @@ func (t *Template) RenderResult(renderMode string, other map[string]interface{})
|
|||
|
||||
default:
|
||||
if res[out] == nil || res[out].(string) == "" {
|
||||
res[out] = yamlQuote(fmt.Sprintf("%v", val))
|
||||
// prevent rendering nil interfaces as "<nil>" string
|
||||
var s string
|
||||
if val != nil {
|
||||
s = yamlQuote(fmt.Sprintf("%v", val))
|
||||
}
|
||||
res[out] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue