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:
@@ -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)
|
||||
|
44
parse/builder/builder_test.go
Normal file
44
parse/builder/builder_test.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user