Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPointerArgumentRequired ... ErrPointerArgumentRequired = errors.New("value type should be a pointer to struct") )
Functions ¶
func WithOverwrittenVariables ¶
func WithOverwrittenVariables(ctx context.Context, overwrittenVariables map[string]interface{}) context.Context
WithOverwrittenVariables returns a new context.Context that holds a reference to the given overwritten variables.
Note: This implementation creates a shallow copy of the input map to hold a reference to all of its key-value pairs. The expectation is that clients must only use the lib to **get** overwritten values from the context. If the value clients receive is a pointer or a complex struct containing some pointers or a pointer-like object such as an array or a map, they should NOT update this value since the context is most likely passed into many go-routines running in parallel. As a consequence, clients may run into a race condition if things goes wrong.
Types ¶
type IOverwritingOps ¶
type IOverwritingOps interface { // ExtractOverwritingStorage returns the Storage currently associated with ctx, or // nil if no such Storage could be found. ExtractOverwritingStorage(ctx context.Context) Storage // GetOverwrittenValue returns the Value of the variable under this name if it was overwritten GetOverwrittenValue(ctx context.Context, name string) Value }
IOverwritingOps ...
var Ops IOverwritingOps = overwritingOps{}
Ops provides a wrapper around all overwriting-related functions provided by the library. It can be mocked to help write tests more fluently.
type MockIOverwritingOps ¶
MockIOverwritingOps is an autogenerated mock type for the IOverwritingOps type
func MockOps ¶
func MockOps() (*MockIOverwritingOps, func())
MockOps can be used in tests to perform monkey-patching on Ops
func (*MockIOverwritingOps) ExtractOverwritingStorage ¶
func (_m *MockIOverwritingOps) ExtractOverwritingStorage(ctx context.Context) Storage
ExtractOverwritingStorage provides a mock function with given fields: ctx
func (*MockIOverwritingOps) GetOverwrittenValue ¶
func (_m *MockIOverwritingOps) GetOverwrittenValue(ctx context.Context, name string) Value
GetOverwrittenValue provides a mock function with given fields: ctx, name
type MockStorage ¶
MockStorage is an autogenerated mock type for the Storage type
func (*MockStorage) Get ¶
func (_m *MockStorage) Get(name string) Value
Get provides a mock function with given fields: name
type MockValue ¶
MockValue is an autogenerated mock type for the Value type
func (*MockValue) AsIs ¶
func (_m *MockValue) AsIs() interface{}
AsIs provides a mock function with given fields:
type Storage ¶
type Storage interface { // Get returns the Value of the variable under this name if it was overwritten Get(name string) Value }
Storage is the container of all overwritten variables
func ExtractOverwritingStorage ¶
ExtractOverwritingStorage returns the Storage currently associated with ctx, or nil if no such Storage could be found.
type Value ¶
type Value interface { // AsIs returns the wrapped value as-is. AsIs() interface{} // AsString typecast to string. Returns zero value if not possible to cast. AsString() string // AsBool typecast to bool. Returns zero value if not possible to cast. AsBool() bool // AsFloat typecast to float64. Returns zero value if not possible to cast. // Note: Try not to use a raw value of type float32 if possible. // https://stackoverflow.com/questions/67145364/golang-losing-precision-while-converting-float32-to-float64 AsFloat() float64 // AsInt typecast to int64. Returns zero value if not possible to cast. // NOTE: JSON by default unmarshal to numbers which are treated as float. // Using this method, your float will lose precision as an int64. AsInt() int64 }
Value wraps a raw interface{} value