config

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DBConfigManager = "config.manager.db"
)

Variables

View Source
var (
	// ErrorNotDefined ...
	ErrorNotDefined = errors.New("configure item is not defined in metadata")
	// ErrorTypeNotMatch ...
	ErrorTypeNotMatch = errors.New("the required value doesn't matched with metadata defined")
	// ErrorInvalidData ...
	ErrorInvalidData = errors.New("the data provided is invalid")
	// ErrorValueNotSet ...
	ErrorValueNotSet = errors.New("the configure value is not set")
	// ErrorStringValueIsEmpty ...
	ErrorStringValueIsEmpty = errors.New("the configure value can not be empty")
)
View Source
var (
	// DefaultCfgManager the default change manager, default is DBCfgManager.
	// If InMemoryConfigManager is used, need to set to InMemoryCfgManager in
	// test code.
	DefaultConfigManager = DBConfigManager
)

Functions

func Load

func Load(ctx context.Context) error

func NewContext

func NewContext(ctx context.Context, m Manager) context.Context

NewContext returns context with CfgManager

func Register

func Register(name string, mgr Manager)

Register register the config manager

Types

type BoolType

type BoolType struct {
}

BoolType ...

type ConfigMetaData

type ConfigMetaData struct {
	// contains filtered or unexported fields
}

ConfigMetaData ...

func Instance

func Instance() *ConfigMetaData

Instance - Get Instance, make it singleton because there is only one copy of metadata in an env

func (*ConfigMetaData) GetAll

func (c *ConfigMetaData) GetAll() []Item

GetAll - Get all metadata in current env

func (*ConfigMetaData) GetByName

func (c *ConfigMetaData) GetByName(name string) (*Item, bool)

GetByName - Get current metadata of current name, if not defined, return false in second params

func (*ConfigMetaData) InitFromArray

func (c *ConfigMetaData) InitFromArray(items []Item)

initFromArray - Initial metadata from an array

func (*ConfigMetaData) Register

func (c *ConfigMetaData) Register(name string, item Item)

Registe - append the item to metadata

type ConfigureValue

type ConfigureValue struct {
	Name  string `json:"name,omitempty"`
	Value string `json:"value,omitempty"`
}

func NewConfigureValue

func NewConfigureValue(name, value string) (*ConfigureValue, error)

func (*ConfigureValue) GetAnyType

func (c *ConfigureValue) GetAnyType() (interface{}, error)

GetAnyType get the interface{} of current value

func (*ConfigureValue) GetBool

func (c *ConfigureValue) GetBool() bool

GetBool - return the bool value of current setting

func (*ConfigureValue) GetFloat64

func (c *ConfigureValue) GetFloat64() float64

GetFloat64 - return the float64 value of current value

func (*ConfigureValue) GetInt

func (c *ConfigureValue) GetInt() int

GetInt - return the int value of current value

func (*ConfigureValue) GetInt64

func (c *ConfigureValue) GetInt64() int64

GetInt64 - return the int64 value of current value

func (*ConfigureValue) GetName

func (c *ConfigureValue) GetName() string

func (*ConfigureValue) GetPassword

func (c *ConfigureValue) GetPassword() string

GetPassword ...

func (*ConfigureValue) GetString

func (c *ConfigureValue) GetString() string

func (*ConfigureValue) GetStringToStringMap

func (c *ConfigureValue) GetStringToStringMap() map[string]string

GetStringToStringMap - return the string to string map of current value

func (*ConfigureValue) Set

func (c *ConfigureValue) Set(name, value string) error

Set - set this configure item to configure store

func (*ConfigureValue) Validate

func (c *ConfigureValue) Validate() error

Validate - to validate configure items, if passed, return nil, else return error

type Float64Type

type Float64Type struct{}

type Int64Type

type Int64Type struct {
}

Int64Type ...

type IntType

type IntType struct {
}

IntType ..

type Item

type Item struct {
	// The Scope of this configuration item: eg: SystemScope, UserScope
	Scope string `json:"scope,omitempty"`
	// email, ldapbasic, ldapgroup, uaa settings, used to retieve configure items by group
	Group string `json:"group,omitempty"`
	// environment key to retrieves this value when initialize, for example: POSTGRESQL_HOST, only used for system settings, for user settings no EnvKey
	EnvironmentKey string `json:"environment_key,omitempty"`
	// The default string value for this key
	DefaultValue string `json:"default_value,omitempty"`
	// The key for current configure settings in database or rest api
	Name string `json:"name,omitempty"`
	// It can be &IntType{}, &StringType{}, &BoolType{}, &PasswordType{}, &MapType{} etc, any type interface implementation
	ItemType Type
	// Editable means it can updated by configure api, For system configure, the editable is always false, for user configure, it may depends
	Editable bool `json:"editable,omitempty"`
	// Description - Describle the usage of the configure item
	Description string
}

Item - Configure item include default value, type, env name

type Manager

type Manager interface {
	Load(ctx context.Context) error
	Set(ctx context.Context, key string, value interface{})
	Save(ctx context.Context) error
	Get(ctx context.Context, key string) *ConfigureValue
	UpdateConfig(ctx context.Context, cfgs map[string]interface{}) error
	GetUserConfigs(ctx context.Context) map[string]interface{}
	ValidateConfig(ctx context.Context, cfgs map[string]interface{}) error
	GetAll(ctx context.Context) map[string]interface{}
}

Manager defines the operation for config

func DefaultManager

func DefaultManager() Manager

DefaultMgr get default config manager

func FromContext

func FromContext(ctx context.Context) (Manager, bool)

FromContext returns CfgManager from context

func GetConfigManager

func GetConfigManager(ctx context.Context) Manager

func GetManager

func GetManager(name string) (Manager, error)

GetManager get the configure manager by name

type MapType

type MapType struct {
}

MapType ...

type NonEmptyStringType

type NonEmptyStringType struct {
	StringType
}

NonEmptyStringType ...

type PasswordType

type PasswordType struct {
}

PasswordType ...

type PortType

type PortType struct {
	IntType
}

PortType ...

type QuotaType

type QuotaType struct {
	Int64Type
}

QuotaType ...

type StringToStringMapType

type StringToStringMapType struct {
}

StringToStringMapType ...

type StringType

type StringType struct {
}

StringType ...

type Type

type Type interface {
	// contains filtered or unexported methods
}

Type - Use this interface to define and encapsulate the behavior of validation and transformation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL