Skip to content
Snippets Groups Projects
Select Git revision
  • d81b543fb702c694ab535f2ef10099b5f4f42bbc
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

context.go

Blame
  • context.go 6.99 KiB
    package service
    
    import (
    	"context"
    	"reflect"
    	"regexp"
    	"time"
    
    	"gitlab.com/uafrica/go-utils/audit"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/string_utils"
    )
    
    // Ctx stores lambda-wide context e.g. claims, request ID etc.
    var Ctx Context
    
    type Context interface {
    	context.Context
    	logger.Logger
    	Producer
    	audit.Auditor
    
    	RequestID() string
    	MillisecondsSinceStart() int64
    	StartTime() time.Time
    
    	//set claim values - things that cannot change - typically UserID, AccountID, Username, ...
    	//the fieldName must be public, i.e. start with uppercase A-Z, no underscores, its not a tag name :-)
    	//once set, it will override any field in params/body struct recursively with this Golang field name (before validation)
    	//Set will fail if the value is already set on this context, i.e. the value cannot change
    	//Call this in your app Start(), before params/body is extracted
    	//value may only be of type int64 or string for now...
    	ClaimSet(fieldName string, value interface{}) error
    
    	//Get can retrieve any claim value
    	ClaimGet(fieldName string) (interface{}, bool)
    
    	//Claim() return all values so you can iterate over them, but not change them...
    	//note: a context.Context also support its own set of values, which you can use as you like
    	//but the idea is that you use this instead, which we apply to params/body
    	Claim() map[string]interface{}
    
    	//context data (names must be snake_case)
    	//unlike claim, any type of value may be stored
    	//but like claims, value can never change
    	WithValue(name string, value interface{}) Context
    	Set(name string, value interface{}) error
    	Get(name string) (interface{}, bool)
    	//Value(name string) interface{} //return nil if not set, inherited from context.Context and overloaded to retrieve local first, just like Get()
    	ValueOrDefault(name string, defaultValue interface{}) interface{}
    	Data() map[string]interface{}
    
    	//write an audit event
    	AuditChange(eventType string, orgValue, newValue interface{})
    }
    
    //values: are added to context and logger
    //these values are logged for every log event in this context
    //values can be added later using with value, but won't be logged
    //	they are just for retrieval between unrelated packages, e.g.
    //	authentication may set the user_id etc... and other package may retrieve it but not change it
    type valueKey string
    
    func (s service) NewContext(base context.Context, requestID string, values map[string]interface{}) (Context, error) {
    	if values == nil {
    		values = map[string]interface{}{}
    	}
    	values["request-id"] = requestID