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
func (t FieldType) Zero() interface{} {
switch t {
case TypeNameString:
return ""
case TypeString:
case TypeString, TypeNameString, TypeLowerCaseString:
return ""
case TypeInt:
return 0

View File

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

View File

@@ -55,6 +55,60 @@ func TestFieldDataGet(t *testing.T) {
"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": {
map[string]*FieldSchema{
"foo": &FieldSchema{Type: TypeInt},

View File

@@ -26,6 +26,10 @@ const (
// a string field
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
// rules. These rules include start and end with an alphanumeric
// character and characters in the middle can be alphanumeric or . or -.
@@ -44,6 +48,8 @@ func (t FieldType) String() string {
switch t {
case TypeString:
return "string"
case TypeLowerCaseString:
return "lowercase string"
case TypeNameString:
return "name string"
case TypeInt: