sdk/framework: add TypeSignedDurationSecond FieldType (#6989)

* Refactor table driven tests to use subtests

* sdk/framework: add TypeSignedDurationSecond FieldType

Adds the TypeSignedDurationSecond FieldType which accepts positive and
negative durations. The existing TypeDurationSecond FieldType does not
accept negative durations.

* Add tests for 0 for TypeDurationSecond and TypeSignedDurationSecond
This commit is contained in:
Michael Gaffney
2019-06-26 13:15:36 -04:00
committed by GitHub
parent 7977a0d516
commit 0d9cdc6811
6 changed files with 265 additions and 34 deletions

View File

@@ -38,8 +38,8 @@ func (d *FieldData) Validate() error {
}
switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, TypeLowerCaseString,
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString,
TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice, TypeHeader:
_, _, err := d.getPrimitive(field, schema)
if err != nil {
@@ -131,8 +131,8 @@ func (d *FieldData) GetOkErr(k string) (interface{}, bool, error) {
}
switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeString, TypeLowerCaseString,
TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString,
TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice, TypeHeader:
return d.getPrimitive(k, schema)
default:
@@ -147,7 +147,7 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
return nil, false, nil
}
switch schema.Type {
switch t := schema.Type; t {
case TypeBool:
var result bool
if err := mapstructure.WeakDecode(raw, &result); err != nil {
@@ -197,7 +197,7 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
}
return result, true, nil
case TypeDurationSecond:
case TypeDurationSecond, TypeSignedDurationSecond:
var result int
switch inp := raw.(type) {
case nil:
@@ -209,7 +209,7 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
}
result = int(dur.Seconds())
}
if result < 0 {
if t == TypeDurationSecond && result < 0 {
return nil, false, fmt.Errorf("cannot provide negative value '%d'", result)
}
return result, true, nil