Handle 'not supplied' case for field type TypeNameString (#3546)

* Fix panic if value is not supplied for variables of TypeNameString

* Add tests for 'not supplied' case of all field types
This commit is contained in:
Vishal Nayak
2017-11-07 10:59:57 -05:00
committed by GitHub
parent 1cf3414352
commit 2994b26194
2 changed files with 83 additions and 0 deletions

View File

@@ -604,6 +604,8 @@ func (s *FieldSchema) DefaultOrZero() interface{} {
// Zero returns the correct zero-value for a specific FieldType
func (t FieldType) Zero() interface{} {
switch t {
case TypeNameString:
return ""
case TypeString:
return ""
case TypeInt:

View File

@@ -278,6 +278,87 @@ func TestFieldDataGet(t *testing.T) {
"foo",
"bar.baz-bay123",
},
"name string type, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeNameString},
},
map[string]interface{}{},
"foo",
"",
},
"string type, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeString},
},
map[string]interface{}{},
"foo",
"",
},
"type int, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeInt},
},
map[string]interface{}{},
"foo",
0,
},
"type bool, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeBool},
},
map[string]interface{}{},
"foo",
false,
},
"type map, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeMap},
},
map[string]interface{}{},
"foo",
map[string]interface{}{},
},
"type duration second, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeDurationSecond},
},
map[string]interface{}{},
"foo",
0,
},
"type slice, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeSlice},
},
map[string]interface{}{},
"foo",
[]interface{}{},
},
"type string slice, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeStringSlice},
},
map[string]interface{}{},
"foo",
[]string{},
},
"type comma string slice, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeCommaStringSlice},
},
map[string]interface{}{},
"foo",
[]string{},
},
}
for name, tc := range cases {