Skip to content
Snippets Groups Projects
Select Git revision
  • 8787211cbcc22ade5309ab6425884641bd2582f4
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

Data.php

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