1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-31 06:35:09 +00:00

Default empty strings

This commit is contained in:
Darren Shepherd
2018-03-01 14:22:08 -07:00
parent 65e6d1cc46
commit f59a6072cf
2 changed files with 49 additions and 1 deletions

View File

@@ -88,7 +88,11 @@ func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{},
}
}
}
result[fieldName] = value
// Don't copy empty strings
if !(value == "" && field.Type == "string") {
result[fieldName] = value
}
if op.IsList() && field.Type == "date" && value != "" {
ts, err := convert.ToTimestamp(value)

View File

@@ -0,0 +1,44 @@
package builder
import (
"testing"
"github.com/rancher/norman/types"
"github.com/stretchr/testify/assert"
)
func TestEmptyStringWithDefault(t *testing.T) {
schema := &types.Schema{
ResourceFields: map[string]types.Field{
"foo": {
Default: "foo",
Type: "string",
Create: true,
},
},
}
schemas := types.NewSchemas()
schemas.AddSchema(*schema)
builder := NewBuilder(&types.APIContext{})
// Test if no field we set to "foo"
result, err := builder.Construct(schema, nil, Create)
if err != nil {
t.Fatal(err)
}
value, ok := result["foo"]
assert.True(t, ok)
assert.Equal(t, "foo", value)
// Test if field is "" we set to "foo"
result, err = builder.Construct(schema, map[string]interface{}{
"foo": "",
}, Create)
if err != nil {
t.Fatal(err)
}
value, ok = result["foo"]
assert.True(t, ok)
assert.Equal(t, "foo", value)
}