diff --git a/context.go b/context.go index c596764..e002682 100644 --- a/context.go +++ b/context.go @@ -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) } diff --git a/pipeline_context.go b/pipeline_context.go index 7aa9ab4..493fc0a 100644 --- a/pipeline_context.go +++ b/pipeline_context.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/pipeline_context_test.go b/pipeline_context_test.go index 533de80..bf789bb 100644 --- a/pipeline_context_test.go +++ b/pipeline_context_test.go @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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",