Skip to content

Commit

Permalink
Handle wrong context retrieval types as missing
Browse files Browse the repository at this point in the history
  • Loading branch information
saantiaguilera committed Feb 10, 2020
1 parent 2cfdad5 commit 916a41b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
34 changes: 17 additions & 17 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,65 @@ type Context interface {
Set(key Tag, value interface{})

// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
// If the value does not exists it returns (nil, false) or the default ("", false)
Get(key Tag) (value interface{}, exists bool)

// Delete the key and any value assigned to it
Delete(key Tag)

// GetString returns the value associated with the key as a string 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetString(key Tag) (s string, exists bool)

// GetBool returns the value associated with the key as a boolean 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetBool(key Tag) (b bool, exists bool)

// GetInt 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
// GetInt returns the value associated with the key as an int 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 / default and won't exist
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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
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 returns the value associated with the key as an int64 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 / default and won't exist
GetInt64(key Tag) (i64 int64, exists bool)

// GetFloat64 returns the value associated with the key as a float64 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetFloat64(key Tag) (f64 float64, exists bool)

// GetTime returns the value associated with the key as time 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetTime(key Tag) (t time.Time, exists bool)

// GetDuration returns the value associated with the key as a duration 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetDuration(key Tag) (d time.Duration, exists bool)

// GetByteSlice returns the value associated with the key as a slice of bytes 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetByteSlice(key Tag) (ss []byte, exists bool)

// GetStringSlice returns the value associated with the key as a slice of strings 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetStringSlice(key Tag) (ss []string, exists bool)

// GetStringMap returns the value associated with the key as a map of interfaces 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetStringMap(key Tag) (sm map[string]interface{}, exists bool)

// GetStringMapString returns the value associated with the key as a map of strings 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetStringMapString(key Tag) (sms map[string]string, exists bool)

// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings 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
// Note that if the type stored is different from the expected, the value will be nil / default and won't exist
GetStringMapStringSlice(key Tag) (smss map[string][]string, exists bool)
}
28 changes: 14 additions & 14 deletions pipeline_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *memMapContext) Delete(key Tag) {
func (c *memMapContext) GetString(key Tag) (s string, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
s, _ = val.(string)
s, exists = val.(string)
}
return
}
Expand All @@ -58,7 +58,7 @@ func (c *memMapContext) GetString(key Tag) (s string, exists bool) {
func (c *memMapContext) GetBool(key Tag) (b bool, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
b, _ = val.(bool)
b, exists = val.(bool)
}
return
}
Expand All @@ -68,7 +68,7 @@ func (c *memMapContext) GetBool(key Tag) (b bool, exists bool) {
func (c *memMapContext) GetInt(key Tag) (i int, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
i, _ = val.(int)
i, exists = val.(int)
}
return
}
Expand All @@ -78,7 +78,7 @@ func (c *memMapContext) GetInt(key Tag) (i int, exists bool) {
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)
i, exists = val.(uint)
}
return
}
Expand All @@ -88,7 +88,7 @@ func (c *memMapContext) GetUInt(key Tag) (i uint, exists bool) {
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)
i, exists = val.(uint64)
}
return
}
Expand All @@ -98,7 +98,7 @@ func (c *memMapContext) GetUInt64(key Tag) (i uint64, exists bool) {
func (c *memMapContext) GetInt64(key Tag) (i64 int64, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
i64, _ = val.(int64)
i64, exists = val.(int64)
}
return
}
Expand All @@ -108,7 +108,7 @@ func (c *memMapContext) GetInt64(key Tag) (i64 int64, exists bool) {
func (c *memMapContext) GetFloat64(key Tag) (f64 float64, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
f64, _ = val.(float64)
f64, exists = val.(float64)
}
return
}
Expand All @@ -118,7 +118,7 @@ func (c *memMapContext) GetFloat64(key Tag) (f64 float64, exists bool) {
func (c *memMapContext) GetTime(key Tag) (t time.Time, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
t, _ = val.(time.Time)
t, exists = val.(time.Time)
}
return
}
Expand All @@ -128,7 +128,7 @@ func (c *memMapContext) GetTime(key Tag) (t time.Time, exists bool) {
func (c *memMapContext) GetDuration(key Tag) (d time.Duration, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
d, _ = val.(time.Duration)
d, exists = val.(time.Duration)
}
return
}
Expand All @@ -138,7 +138,7 @@ func (c *memMapContext) GetDuration(key Tag) (d time.Duration, exists bool) {
func (c *memMapContext) GetByteSlice(key Tag) (ss []byte, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
ss, _ = val.([]byte)
ss, exists = val.([]byte)
}
return
}
Expand All @@ -148,7 +148,7 @@ func (c *memMapContext) GetByteSlice(key Tag) (ss []byte, exists bool) {
func (c *memMapContext) GetStringSlice(key Tag) (ss []string, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
ss, _ = val.([]string)
ss, exists = val.([]string)
}
return
}
Expand All @@ -158,7 +158,7 @@ func (c *memMapContext) GetStringSlice(key Tag) (ss []string, exists bool) {
func (c *memMapContext) GetStringMap(key Tag) (sm map[string]interface{}, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
sm, _ = val.(map[string]interface{})
sm, exists = val.(map[string]interface{})
}
return
}
Expand All @@ -168,7 +168,7 @@ func (c *memMapContext) GetStringMap(key Tag) (sm map[string]interface{}, exists
func (c *memMapContext) GetStringMapString(key Tag) (sms map[string]string, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
sms, _ = val.(map[string]string)
sms, exists = val.(map[string]string)
}
return
}
Expand All @@ -178,7 +178,7 @@ func (c *memMapContext) GetStringMapString(key Tag) (sms map[string]string, exis
func (c *memMapContext) GetStringMapStringSlice(key Tag) (smss map[string][]string, exists bool) {
var val interface{}
if val, exists = c.Get(key); exists && val != nil {
smss, _ = val.(map[string][]string)
smss, exists = val.(map[string][]string)
}
return
}
28 changes: 14 additions & 14 deletions pipeline_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestPipelineContext_GetString_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: "", exists: true},
}{value: "", exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestPipelineContext_GetBool_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: false, exists: true},
}{value: false, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestPipelineContext_GetInt_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: 0, exists: true},
}{value: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestPipelineContext_GetUInt_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: uint(0), exists: true},
}{value: uint(0), exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -450,7 +450,7 @@ func TestPipelineContext_GetUInt64_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: uint64(0), exists: true},
}{value: uint64(0), exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestPipelineContext_GetInt64_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: int64(0), exists: true},
}{value: int64(0), exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestPipelineContext_GetFloat64_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: float64(0), exists: true},
}{value: float64(0), exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -669,7 +669,7 @@ func TestPipelineContext_GetTime_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: time.Time{}, exists: true},
}{value: time.Time{}, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -742,7 +742,7 @@ func TestPipelineContext_GetDuration_Suite(t *testing.T) {
want: struct {
value interface{}
exists bool
}{value: time.Duration(0), exists: true},
}{value: time.Duration(0), exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -817,7 +817,7 @@ func TestPipelineContext_GetByteSlice_Suite(t *testing.T) {
want: struct {
valuelen int
exists bool
}{valuelen: 0, exists: true},
}{valuelen: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -892,7 +892,7 @@ func TestPipelineContext_GetStringSlice_Suite(t *testing.T) {
want: struct {
valuelen int
exists bool
}{valuelen: 0, exists: true},
}{valuelen: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -967,7 +967,7 @@ func TestPipelineContext_GetStringMap_Suite(t *testing.T) {
want: struct {
valuelen int
exists bool
}{valuelen: 0, exists: true},
}{valuelen: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func TestPipelineContext_GetStringMapString_Suite(t *testing.T) {
want: struct {
valuelen int
exists bool
}{valuelen: 0, exists: true},
}{valuelen: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func TestPipelineContext_GetStringMapStringSlice_Suite(t *testing.T) {
want: struct {
valuelen int
exists bool
}{valuelen: 0, exists: true},
}{valuelen: 0, exists: false},
},
{
name: "given a stored nil value, when retrieving it, then it exist and it's default value",
Expand Down

0 comments on commit 916a41b

Please sign in to comment.