Add TypeFloat to field data (#8923)

* Add TypeFloat to field data

* Handle zero value case

* Address review feedback
This commit is contained in:
Vishal Nayak
2020-05-04 18:22:28 -04:00
committed by GitHub
parent 8a4dce5bf0
commit 206ab86823
4 changed files with 159 additions and 8 deletions

View File

@@ -623,6 +623,8 @@ func (t FieldType) Zero() interface{} {
return []int{} return []int{}
case TypeHeader: case TypeHeader:
return http.Header{} return http.Header{}
case TypeFloat:
return 0.0
default: default:
panic("unknown type: " + t.String()) panic("unknown type: " + t.String())
} }

View File

@@ -40,7 +40,7 @@ func (d *FieldData) Validate() error {
switch schema.Type { switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString, case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString,
TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice, TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice, TypeHeader: TypeKVPairs, TypeCommaIntSlice, TypeHeader, TypeFloat:
_, _, err := d.getPrimitive(field, schema) _, _, err := d.getPrimitive(field, schema)
if err != nil { if err != nil {
return errwrap.Wrapf(fmt.Sprintf("error converting input %v for field %q: {{err}}", value, field), err) return errwrap.Wrapf(fmt.Sprintf("error converting input %v for field %q: {{err}}", value, field), err)
@@ -133,7 +133,7 @@ func (d *FieldData) GetOkErr(k string) (interface{}, bool, error) {
switch schema.Type { switch schema.Type {
case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString, case TypeBool, TypeInt, TypeMap, TypeDurationSecond, TypeSignedDurationSecond, TypeString,
TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice, TypeLowerCaseString, TypeNameString, TypeSlice, TypeStringSlice, TypeCommaStringSlice,
TypeKVPairs, TypeCommaIntSlice, TypeHeader: TypeKVPairs, TypeCommaIntSlice, TypeHeader, TypeFloat:
return d.getPrimitive(k, schema) return d.getPrimitive(k, schema)
default: default:
return nil, false, return nil, false,
@@ -162,6 +162,13 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
} }
return result, true, nil return result, true, nil
case TypeFloat:
var result float64
if err := mapstructure.WeakDecode(raw, &result); err != nil {
return nil, false, err
}
return result, true, nil
case TypeString: case TypeString:
var result string var result string
if err := mapstructure.WeakDecode(raw, &result); err != nil { if err := mapstructure.WeakDecode(raw, &result); err != nil {

View File

@@ -8,10 +8,11 @@ import (
func TestFieldDataGet(t *testing.T) { func TestFieldDataGet(t *testing.T) {
cases := map[string]struct { cases := map[string]struct {
Schema map[string]*FieldSchema Schema map[string]*FieldSchema
Raw map[string]interface{} Raw map[string]interface{}
Key string Key string
Value interface{} Value interface{}
ExpectError bool
}{ }{
"string type, string value": { "string type, string value": {
map[string]*FieldSchema{ map[string]*FieldSchema{
@@ -22,6 +23,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"bar", "bar",
false,
}, },
"string type, int value": { "string type, int value": {
@@ -33,6 +35,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"42", "42",
false,
}, },
"string type, unset value": { "string type, unset value": {
@@ -42,6 +45,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"", "",
false,
}, },
"string type, unset value with default": { "string type, unset value with default": {
@@ -54,6 +58,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"bar", "bar",
false,
}, },
"lowercase string type, lowercase string value": { "lowercase string type, lowercase string value": {
@@ -65,6 +70,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"bar", "bar",
false,
}, },
"lowercase string type, mixed-case string value": { "lowercase string type, mixed-case string value": {
@@ -76,6 +82,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"bar", "bar",
false,
}, },
"lowercase string type, int value": { "lowercase string type, int value": {
@@ -87,6 +94,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"42", "42",
false,
}, },
"lowercase string type, unset value": { "lowercase string type, unset value": {
@@ -96,6 +104,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"", "",
false,
}, },
"lowercase string type, unset value with lowercase default": { "lowercase string type, unset value with lowercase default": {
@@ -108,6 +117,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"bar", "bar",
false,
}, },
"int type, int value": { "int type, int value": {
@@ -119,6 +129,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"bool type, bool value": { "bool type, bool value": {
@@ -130,6 +141,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
false, false,
false,
}, },
"map type, map value": { "map type, map value": {
@@ -145,6 +157,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"child": true, "child": true,
}, },
false,
}, },
"duration type, string value": { "duration type, string value": {
@@ -156,6 +169,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"duration type, string duration value": { "duration type, string duration value": {
@@ -167,6 +181,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
2520, 2520,
false,
}, },
"duration type, int value": { "duration type, int value": {
@@ -178,6 +193,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"duration type, float value": { "duration type, float value": {
@@ -189,6 +205,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"duration type, nil value": { "duration type, nil value": {
@@ -200,6 +217,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
0, 0,
false,
}, },
"duration type, 0 value": { "duration type, 0 value": {
@@ -211,6 +229,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
0, 0,
false,
}, },
"signed duration type, positive string value": { "signed duration type, positive string value": {
@@ -222,6 +241,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"signed duration type, positive string duration value": { "signed duration type, positive string duration value": {
@@ -233,6 +253,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
2520, 2520,
false,
}, },
"signed duration type, positive int value": { "signed duration type, positive int value": {
@@ -244,6 +265,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"signed duration type, positive float value": { "signed duration type, positive float value": {
@@ -255,6 +277,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
42, 42,
false,
}, },
"signed duration type, negative string value": { "signed duration type, negative string value": {
@@ -266,6 +289,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
-42, -42,
false,
}, },
"signed duration type, negative string duration value": { "signed duration type, negative string duration value": {
@@ -277,6 +301,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
-2520, -2520,
false,
}, },
"signed duration type, negative int value": { "signed duration type, negative int value": {
@@ -288,6 +313,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
-42, -42,
false,
}, },
"signed duration type, negative float value": { "signed duration type, negative float value": {
@@ -299,6 +325,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
-42, -42,
false,
}, },
"signed duration type, nil value": { "signed duration type, nil value": {
@@ -310,6 +337,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
0, 0,
false,
}, },
"signed duration type, 0 value": { "signed duration type, 0 value": {
@@ -321,6 +349,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
0, 0,
false,
}, },
"slice type, empty slice": { "slice type, empty slice": {
@@ -332,6 +361,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]interface{}{}, []interface{}{},
false,
}, },
"slice type, filled, mixed slice": { "slice type, filled, mixed slice": {
@@ -343,6 +373,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]interface{}{123, "abc"}, []interface{}{123, "abc"},
false,
}, },
"string slice type, filled slice": { "string slice type, filled slice": {
@@ -354,6 +385,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"123", "abc"}, []string{"123", "abc"},
false,
}, },
"string slice type, single value": { "string slice type, single value": {
@@ -365,6 +397,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"abc"}, []string{"abc"},
false,
}, },
"string slice type, empty string": { "string slice type, empty string": {
@@ -376,6 +409,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{}, []string{},
false,
}, },
"comma string slice type, empty string": { "comma string slice type, empty string": {
@@ -387,6 +421,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{}, []string{},
false,
}, },
"comma string slice type, comma string with one value": { "comma string slice type, comma string with one value": {
@@ -398,6 +433,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"value1"}, []string{"value1"},
false,
}, },
"comma string slice type, comma string with multi value": { "comma string slice type, comma string with multi value": {
@@ -409,6 +445,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"value1", "value2", "value3"}, []string{"value1", "value2", "value3"},
false,
}, },
"comma string slice type, nil string slice value": { "comma string slice type, nil string slice value": {
@@ -420,6 +457,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{}, []string{},
false,
}, },
"comma string slice type, string slice with one value": { "comma string slice type, string slice with one value": {
@@ -431,6 +469,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"value1"}, []string{"value1"},
false,
}, },
"comma string slice type, string slice with multi value": { "comma string slice type, string slice with multi value": {
@@ -442,6 +481,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{"value1", "value2", "value3"}, []string{"value1", "value2", "value3"},
false,
}, },
"comma string slice type, empty string slice value": { "comma string slice type, empty string slice value": {
@@ -453,6 +493,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]string{}, []string{},
false,
}, },
"comma int slice type, comma int with one value": { "comma int slice type, comma int with one value": {
@@ -464,6 +505,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1}, []int{1},
false,
}, },
"comma int slice type, comma int with multi value slice": { "comma int slice type, comma int with multi value slice": {
@@ -475,6 +517,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1, 2, 3}, []int{1, 2, 3},
false,
}, },
"comma int slice type, comma int with multi value": { "comma int slice type, comma int with multi value": {
@@ -486,6 +529,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1, 2, 3}, []int{1, 2, 3},
false,
}, },
"comma int slice type, nil int slice value": { "comma int slice type, nil int slice value": {
@@ -497,6 +541,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{}, []int{},
false,
}, },
"comma int slice type, int slice with one value": { "comma int slice type, int slice with one value": {
@@ -508,6 +553,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1}, []int{1},
false,
}, },
"comma int slice type, int slice with multi value strings": { "comma int slice type, int slice with multi value strings": {
@@ -519,6 +565,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1, 2, 3}, []int{1, 2, 3},
false,
}, },
"comma int slice type, int slice with multi value": { "comma int slice type, int slice with multi value": {
@@ -530,6 +577,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{1, 2, 3}, []int{1, 2, 3},
false,
}, },
"comma int slice type, empty int slice value": { "comma int slice type, empty int slice value": {
@@ -541,6 +589,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
[]int{}, []int{},
false,
}, },
"name string type, valid string": { "name string type, valid string": {
map[string]*FieldSchema{ map[string]*FieldSchema{
@@ -551,6 +600,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"bar", "bar",
false,
}, },
"name string type, valid value with special characters": { "name string type, valid value with special characters": {
@@ -562,6 +612,7 @@ func TestFieldDataGet(t *testing.T) {
}, },
"foo", "foo",
"bar.baz-bay123", "bar.baz-bay123",
false,
}, },
"keypair type, valid value map type": { "keypair type, valid value map type": {
@@ -581,6 +632,7 @@ func TestFieldDataGet(t *testing.T) {
"key2": "value2", "key2": "value2",
"key3": "1", "key3": "1",
}, },
false,
}, },
"keypair type, list of equal sign delim key pairs type": { "keypair type, list of equal sign delim key pairs type": {
@@ -596,6 +648,7 @@ func TestFieldDataGet(t *testing.T) {
"key2": "value2", "key2": "value2",
"key3": "1", "key3": "1",
}, },
false,
}, },
"keypair type, single equal sign delim value": { "keypair type, single equal sign delim value": {
@@ -609,6 +662,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]string{ map[string]string{
"key1": "value1", "key1": "value1",
}, },
false,
}, },
"type header, keypair string array": { "type header, keypair string array": {
@@ -624,6 +678,7 @@ func TestFieldDataGet(t *testing.T) {
"Key2": []string{"value2"}, "Key2": []string{"value2"},
"Key3": []string{"1"}, "Key3": []string{"1"},
}, },
false,
}, },
"type header, b64 string": { "type header, b64 string": {
@@ -643,6 +698,7 @@ func TestFieldDataGet(t *testing.T) {
"Authorization": []string{"AWS4-HMAC-SHA256 Credential=foo/20160930/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-vault-server, Signature=a69fd750a3445c4e553e1b3e79d3da90eef54047f1eb4efe8ffbc9c428c2655b"}, "Authorization": []string{"AWS4-HMAC-SHA256 Credential=foo/20160930/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-vault-server, Signature=a69fd750a3445c4e553e1b3e79d3da90eef54047f1eb4efe8ffbc9c428c2655b"},
"Foo": []string{"42"}, "Foo": []string{"42"},
}, },
false,
}, },
"type header, json string": { "type header, json string": {
@@ -659,6 +715,7 @@ func TestFieldDataGet(t *testing.T) {
"Guten Tag": []string{"42"}, "Guten Tag": []string{"42"},
"你好": []string{"10", "20", "3.14"}, "你好": []string{"10", "20", "3.14"},
}, },
false,
}, },
"type header, keypair string array with dupe key": { "type header, keypair string array with dupe key": {
@@ -674,6 +731,7 @@ func TestFieldDataGet(t *testing.T) {
"Key2": []string{"value2"}, "Key2": []string{"value2"},
"Key3": []string{"1", "true"}, "Key3": []string{"1", "true"},
}, },
false,
}, },
"type header, map string slice": { "type header, map string slice": {
@@ -693,6 +751,7 @@ func TestFieldDataGet(t *testing.T) {
"Key2": []string{"value2"}, "Key2": []string{"value2"},
"Key3": []string{"1"}, "Key3": []string{"1"},
}, },
false,
}, },
"name string type, not supplied": { "name string type, not supplied": {
@@ -702,6 +761,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"", "",
false,
}, },
"string type, not supplied": { "string type, not supplied": {
@@ -711,6 +771,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
"", "",
false,
}, },
"type int, not supplied": { "type int, not supplied": {
@@ -720,6 +781,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
0, 0,
false,
}, },
"type bool, not supplied": { "type bool, not supplied": {
@@ -729,6 +791,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
false, false,
false,
}, },
"type map, not supplied": { "type map, not supplied": {
@@ -738,6 +801,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
map[string]interface{}{}, map[string]interface{}{},
false,
}, },
"type duration second, not supplied": { "type duration second, not supplied": {
@@ -747,6 +811,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
0, 0,
false,
}, },
"type signed duration second, not supplied": { "type signed duration second, not supplied": {
@@ -756,6 +821,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
0, 0,
false,
}, },
"type slice, not supplied": { "type slice, not supplied": {
@@ -765,6 +831,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
[]interface{}{}, []interface{}{},
false,
}, },
"type string slice, not supplied": { "type string slice, not supplied": {
@@ -774,6 +841,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
[]string{}, []string{},
false,
}, },
"type comma string slice, not supplied": { "type comma string slice, not supplied": {
@@ -783,6 +851,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
[]string{}, []string{},
false,
}, },
"type kv pair, not supplied": { "type kv pair, not supplied": {
@@ -792,6 +861,7 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
map[string]string{}, map[string]string{},
false,
}, },
"type header, not supplied": { "type header, not supplied": {
@@ -801,6 +871,65 @@ func TestFieldDataGet(t *testing.T) {
map[string]interface{}{}, map[string]interface{}{},
"foo", "foo",
http.Header{}, http.Header{},
false,
},
"float type, positive with decimals, as string": {
map[string]*FieldSchema{
"foo": {Type: TypeFloat},
},
map[string]interface{}{
"foo": "1234567.891234567",
},
"foo",
1234567.891234567,
false,
},
"float type, negative with decimals, as string": {
map[string]*FieldSchema{
"foo": {Type: TypeFloat},
},
map[string]interface{}{
"foo": "-1234567.891234567",
},
"foo",
-1234567.891234567,
false,
},
"float type, positive without decimals": {
map[string]*FieldSchema{
"foo": {Type: TypeFloat},
},
map[string]interface{}{
"foo": 1234567,
},
"foo",
1234567.0,
false,
},
"type float, not supplied": {
map[string]*FieldSchema{
"foo": {Type: TypeFloat},
},
map[string]interface{}{},
"foo",
0.0,
false,
},
"type float, invalid value": {
map[string]*FieldSchema{
"foo": {Type: TypeFloat},
},
map[string]interface{}{
"foo": "invalid0.0",
},
"foo",
0.0,
true,
}, },
} }
@@ -813,8 +942,16 @@ func TestFieldDataGet(t *testing.T) {
Schema: tc.Schema, Schema: tc.Schema,
} }
if err := data.Validate(); err != nil { err := data.Validate()
t.Fatalf("bad: %s", err) switch {
case tc.ExpectError && err == nil:
t.Fatalf("expected error")
case tc.ExpectError && err != nil:
return
case !tc.ExpectError && err != nil:
t.Fatal(err)
default:
// Continue if !tc.ExpectError && err == nil
} }
actual := data.Get(tc.Key) actual := data.Get(tc.Key)

View File

@@ -53,6 +53,9 @@ const (
// benevolent MITM for a request, and the headers are sent through and // benevolent MITM for a request, and the headers are sent through and
// parsed. // parsed.
TypeHeader TypeHeader
// TypeFloat parses both float32 and float64 values
TypeFloat
) )
func (t FieldType) String() string { func (t FieldType) String() string {
@@ -77,6 +80,8 @@ func (t FieldType) String() string {
return "slice" return "slice"
case TypeHeader: case TypeHeader:
return "header" return "header"
case TypeFloat:
return "float"
default: default:
return "unknown type" return "unknown type"
} }