-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* simplified attributes * fixed/added tests, fixed func calls to NewAttributes() * fixed functions
- Loading branch information
1 parent
97ae895
commit fa01ecb
Showing
14 changed files
with
380 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package bascule | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/xmidt-org/arrange" | ||
) | ||
|
||
var nilTime = time.Time{} | ||
|
||
type BasicAttributes map[string]interface{} | ||
|
||
func (a BasicAttributes) Get(key string) (interface{}, bool) { | ||
v, ok := a[key] | ||
return v, ok | ||
} | ||
|
||
//NewAttributes builds an Attributes instance with | ||
//the given map as datasource. | ||
func NewAttributes(m map[string]interface{}) Attributes { | ||
return BasicAttributes(m) | ||
} | ||
|
||
// GetNestedAttribute uses multiple keys in order to obtain an attribute. | ||
func GetNestedAttribute(attributes Attributes, keys ...string) (interface{}, bool) { | ||
// need at least one key. | ||
if keys == nil || len(keys) == 0 { | ||
return nil, false | ||
} | ||
|
||
var ( | ||
result interface{} | ||
ok bool | ||
) | ||
result = attributes | ||
for _, k := range keys { | ||
var a Attributes | ||
if result == nil { | ||
return nil, false | ||
} | ||
ok = arrange.TryConvert(result, | ||
func(attr Attributes) { a = attr }, | ||
func(m map[string]interface{}) { a = BasicAttributes(m) }, | ||
) | ||
if !ok { | ||
return nil, false | ||
} | ||
result, ok = a.Get(k) | ||
if !ok { | ||
return nil, false | ||
} | ||
} | ||
return result, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package bascule | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
assert := assert.New(t) | ||
attributes := Attributes(attrs) | ||
|
||
val, ok := attributes.Get("testkey") | ||
assert.Equal("testval", val) | ||
assert.True(ok) | ||
|
||
val, ok = attributes.Get("noval") | ||
assert.Empty(val) | ||
assert.False(ok) | ||
|
||
emptyAttributes := NewAttributes(map[string]interface{}{}) | ||
val, ok = emptyAttributes.Get("test") | ||
assert.Nil(val) | ||
assert.False(ok) | ||
} | ||
|
||
func TestGetNestedAttribute(t *testing.T) { | ||
attributes := NewAttributes(map[string]interface{}{ | ||
"a": map[string]interface{}{"b": map[string]interface{}{"c": "answer"}}, | ||
"one level": "yay", | ||
"bad": nil, | ||
}) | ||
tests := []struct { | ||
description string | ||
keys []string | ||
expectedResult interface{} | ||
expectedOK bool | ||
}{ | ||
// Success test is failing. ): getting nil, false | ||
{ | ||
description: "Success", | ||
keys: []string{"a", "b", "c"}, | ||
expectedResult: "answer", | ||
expectedOK: true, | ||
}, | ||
{ | ||
description: "Success single key", | ||
keys: []string{"one level"}, | ||
expectedResult: "yay", | ||
expectedOK: true, | ||
}, | ||
{ | ||
description: "Success nil", | ||
keys: []string{"bad"}, | ||
expectedResult: nil, | ||
expectedOK: true, | ||
}, | ||
{ | ||
description: "Nil Keys Error", | ||
keys: nil, | ||
}, | ||
{ | ||
description: "No Keys Error", | ||
keys: []string{}, | ||
}, | ||
{ | ||
description: "Non Attribute Value Error", | ||
keys: []string{"one level", "test"}, | ||
}, | ||
{ | ||
description: "Nil Attributes Error", | ||
keys: []string{"bad", "more bad"}, | ||
}, | ||
{ | ||
description: "Missing Key Error", | ||
keys: []string{"c", "b", "a"}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
val, ok := GetNestedAttribute(attributes, tc.keys...) | ||
assert.Equal(tc.expectedResult, val) | ||
assert.Equal(tc.expectedOK, ok) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.