Skip to content
Snippets Groups Projects
Select Git revision
  • 076807d78d7e26425229805083733cbb2f9b7693
  • main default protected
  • v1.298.0
  • 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
22 results

struct.go

Blame
  • struct.go 3.32 KiB
    package config
    
    import (
    	"regexp"
    
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/string_utils"
    	"gitlab.com/uafrica/go-utils/struct_utils"
    )
    
    var (
    	prefixStructs = map[string]interface{}{}
    )
    
    func LoadEnv(prefix string, configStructPtr interface{}) error {
    	return Load(prefix, configStructPtr, string_utils.EnvironmentKeyReader())
    }
    
    func Load(prefix string, configStructPtr interface{}, keyReader string_utils.KeyReader) error {
    	if !prefixRegex.MatchString(prefix) {
    		return errors.Errorf("config(%s) invalid prefix", prefix)
    	}
    
    	//store before load in case it fails to be still part of docs
    	prefixStructs[prefix] = configStructPtr
    
    	//read os.Getenv() or other reader...
    	nv := struct_utils.NamedValuesFromReader(prefix, keyReader)
    	logger.Debugf("nv: %+v", nv)
    
    	//parse into struct
    	unused, err := struct_utils.UnmarshalNamedValues(nv, configStructPtr)
    	if err != nil {
    		return errors.Wrapf(err, "config(%s) cannot load", prefix)
    	}
    	if len(unused) > 0 {
    		//we still use os.Getenv() elsewhere, so some variables may not be in the struct
    		//e.g. AUDIT_QUEUE_URL is read from queues/sqs/producer which match config(prefix="AUDIT")
    		//so we cannot yet fail here, which we should, because config setting not used is often
    		//a reason for errors, when we try to configure something, then it does not work, and
    		//we cannot figure out why, but the value we did set, might just be misspelled etc.
    		//so, for now - do not fail here, just report the unused values
    		logger.Warnf("Note unused env (might be used elsewhere) for config(%s): %+v", prefix, unused)
    		//return errors.Errorf("config(%s): unknown %+v", prefix, unused)
    	}
    
    	if validator, ok := configStructPtr.(Validator); ok {
    		if err := validator.Validate(); err != nil {
    			return errors.Wrapf(err, "config(%s) is invalid", prefix)
    		}
    	}
    	return nil
    }
    
    func LoadRedis(prefix string, configStructPtr interface{}) error {
    	if !prefixRegex.MatchString(prefix) {
    		return errors.Errorf("config(%s) invalid prefix", prefix)
    	}
    
    	//store before load in case it fails to be still part of docs
    	prefixStructs[prefix] = configStructPtr
    
    	//read os.Getenv()
    	nv := struct_utils.NamedValuesFromEnv(prefix)
    
    	//parse into struct
    	unused, err := struct_utils.UnmarshalNamedValues(nv, configStructPtr)
    	if err != nil {
    		return errors.Wrapf(err, "config(%s) cannot load", prefix)