Add TypeLowerCaseString (#4683)

This commit is contained in:
Jim Kalafut
2018-06-01 18:30:59 -07:00
committed by GitHub
parent dce21be314
commit 5d973885b4
4 changed files with 70 additions and 5 deletions

View File

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

View File

@@ -35,7 +35,7 @@ func (d *FieldData) Validate() error {
} }
switch schema.Type { switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, TypeLowerCaseString,
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice: TypeKVPairs, TypeCommaIntSlice:
_, _, err := d.getPrimitive(field, schema) _, _, err := d.getPrimitive(field, schema)
@@ -111,7 +111,7 @@ func (d *FieldData) GetOkErr(k string) (interface{}, bool, error) {
} }
switch schema.Type { switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, TypeLowerCaseString,
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice: TypeKVPairs, TypeCommaIntSlice:
return d.getPrimitive(k, schema) return d.getPrimitive(k, schema)
@@ -149,6 +149,13 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
} }
return result, true, nil return result, true, nil
case TypeLowerCaseString:
var result string
if err := mapstructure.WeakDecode(raw, &result); err != nil {
return nil, true, err
}
return strings.ToLower(result), true, nil
case TypeNameString: case TypeNameString:
var result string var result string
if err := mapstructure.WeakDecode(raw, &result); err != nil { if err := mapstructure.WeakDecode(raw, &result); err != nil {

View File

@@ -55,6 +55,60 @@ func TestFieldDataGet(t *testing.T) {
"bar", "bar",
}, },
"lowercase string type, lowercase string value": {
map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeLowerCaseString},
},
map[string]interface{}{
"foo": "bar",
},
"foo",
"bar",
},
"lowercase string type, mixed-case string value": {
map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeLowerCaseString},
},
map[string]interface{}{
"foo": "BaR",
},
"foo",
"bar",
},
"lowercase string type, int value": {
map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeLowerCaseString},
},
map[string]interface{}{
"foo": 42,
},
"foo",
"42",
},
"lowercase string type, unset value": {
map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeLowerCaseString},
},
map[string]interface{}{},
"foo",
"",
},
"lowercase string type, unset value with lowercase default": {
map[string]*FieldSchema{
"foo": &FieldSchema{
Type: TypeLowerCaseString,
Default: "bar",
},
},
map[string]interface{}{},
"foo",
"bar",
},
"int type, int value": { "int type, int value": {
map[string]*FieldSchema{ map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeInt}, "foo": &FieldSchema{Type: TypeInt},

View File

@@ -26,6 +26,10 @@ const (
// a string field // a string field
TypeCommaStringSlice TypeCommaStringSlice
// TypeLowerCaseString is a helper for TypeString that returns a lowercase
// version of the provided string
TypeLowerCaseString
// TypeNameString represents a name that is URI safe and follows specific // TypeNameString represents a name that is URI safe and follows specific
// rules. These rules include start and end with an alphanumeric // rules. These rules include start and end with an alphanumeric
// character and characters in the middle can be alphanumeric or . or -. // character and characters in the middle can be alphanumeric or . or -.
@@ -44,6 +48,8 @@ func (t FieldType) String() string {
switch t { switch t {
case TypeString: case TypeString:
return "string" return "string"
case TypeLowerCaseString:
return "lowercase string"
case TypeNameString: case TypeNameString:
return "name string" return "name string"
case TypeInt: case TypeInt: