Skip to content

Commit

Permalink
Add support to uint and uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
saantiaguilera committed Feb 5, 2020
1 parent 630010b commit 9f057ca
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
8 changes: 8 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type Context interface {
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
GetInt(key Tag) (i int, exists bool)

// GetUInt returns the value associated with the key as an uint if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
GetUInt(key Tag) (i uint, exists bool)

// GetUInt64 returns the value associated with the key as an uint64 if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
GetUInt64(key Tag) (i uint64, exists bool)

// GetInt64 returns the value associated with the key as an integer if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
GetInt64(key Tag) (i64 int64, exists bool)
Expand Down
10 changes: 10 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func (m *mockContext) GetInt(key pipeline.Tag) (i int, exists bool) {
return args.Int(0), args.Bool(1)
}

func (m *mockContext) GetUInt(key pipeline.Tag) (i uint, exists bool) {
args := m.Called(key)
return args.Get(0).(uint), args.Bool(1)
}

func (m *mockContext) GetUInt64(key pipeline.Tag) (i uint64, exists bool) {
args := m.Called(key)
return args.Get(0).(uint64), args.Bool(1)
}

func (m *mockContext) GetInt64(key pipeline.Tag) (i64 int64, exists bool) {
args := m.Called(key)
return args.Get(0).(int64), args.Bool(1)
Expand Down
20 changes: 20 additions & 0 deletions pipeline_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ func (c *memMapContext) GetInt(key Tag) (i int, exists bool) {
return
}

// GetUInt returns the value associated with the key as an uint if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
func (c *memMapContext) GetUInt(key Tag) (i uint, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
i, _ = val.(uint)
}
return
}

// GetUInt64 returns the value associated with the key as an uint if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
func (c *memMapContext) GetUInt64(key Tag) (i uint64, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
i, _ = val.(uint64)
}
return
}

// GetInt64 returns the value associated with the key as an integer if possible, and if it exists regardless of the type.
// Note that if the type stored is different from the expected, the value will be nil but the exists will be true
func (c *memMapContext) GetInt64(key Tag) (i64 int64, exists bool) {
Expand Down
146 changes: 146 additions & 0 deletions pipeline_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,152 @@ func TestPipelineContext_GetInt_Suite(t *testing.T) {
}
}

func TestPipelineContext_GetUInt_Suite(t *testing.T) {
tests := []struct {
name string
store struct {
key Tag
value interface{}
}
retrieve Tag
want struct {
value interface{}
exists bool
}
}{
{
name: "given a stored value, when retrieving it, then it exists and it's the same",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: uint(12)},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint(12), exists: true},
},
{
name: "given a stored value, when retrieving something else, then it doesnt exists and it's null",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: true},
retrieve: TestKey2Tag,
want: struct {
value interface{}
exists bool
}{value: uint(0), exists: false},
},
{
name: "given a stored value of a different type, when retrieving it, then it exists but is default value",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: "asdf"},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint(0), exists: true},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: nil},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint(0), exists: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := CreateContext()
ctx.Set(tt.store.key, tt.store.value)
if got, exists := ctx.GetUInt(tt.retrieve); got != tt.want.value || exists != tt.want.exists {
t.Errorf("expected - got = {%v %v}, want %v", got, exists, tt.want)
}
})
}
}

func TestPipelineContext_GetUInt64_Suite(t *testing.T) {
tests := []struct {
name string
store struct {
key Tag
value interface{}
}
retrieve Tag
want struct {
value interface{}
exists bool
}
}{
{
name: "given a stored value, when retrieving it, then it exists and it's the same",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: uint64(12)},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint64(12), exists: true},
},
{
name: "given a stored value, when retrieving something else, then it doesnt exists and it's null",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: true},
retrieve: TestKey2Tag,
want: struct {
value interface{}
exists bool
}{value: uint64(0), exists: false},
},
{
name: "given a stored value of a different type, when retrieving it, then it exists but is default value",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: "asdf"},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint64(0), exists: true},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
store: struct {
key Tag
value interface{}
}{key: TestKeyTag, value: nil},
retrieve: TestKeyTag,
want: struct {
value interface{}
exists bool
}{value: uint64(0), exists: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := CreateContext()
ctx.Set(tt.store.key, tt.store.value)
if got, exists := ctx.GetUInt64(tt.retrieve); got != tt.want.value || exists != tt.want.exists {
t.Errorf("expected - got = {%v %v}, want %v", got, exists, tt.want)
}
})
}
}

func TestPipelineContext_GetInt64_Suite(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 9f057ca

Please sign in to comment.