goconf

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

README

goconf

Configuration providers for go inspired by .net core configuration libs.

Supportives:

  • Memory source
  • Environment variables
  • JSON file
  • YAML file
  • INI file

Features:

  • Get value by full path with key delimiter (e.g. application.bind_addr.port)
  • Cast value to specialized type with TypeConversionFunc
  • Bind configuration section to struct

Usage

  • package: github.com/ah-its-andy/goconf
// initialize on application startup
goconf.Init(func(b goconf.Builder) {
	b.AddSource(physicalfile.Yaml(/*yaml file path, absolute or relative  both supported*/)))
     .AddSource(physicalfile.Json(/*json file path, absolute or relative  both supported*/))
     .AddSource(goconf.EnvironmentVariable(/*prefix for filter environment variables*/))
     .AddSource(goconf.Memory(/*config map*/))
})

// use it anywhere
bindAddr, ok := goconf.GetString("application.bind_addr.addr") //Get string value

bindAddrWithDefault := goconf.GetStringOrDefault("application.bind_addr.addr", "default value") //returns default value when key is not found

castValue, ok := goconf.Cast("application.bind_addr.port", goconf.IntConversion) //cast value to int

castValueWithDefault := goconf.CastOrDefault("application.bind_addr.port", 0 /*default value*/, goconf.IntConversion) //cast value to int, returns default value when key is not found

section:= gocinf.GetSection("application") //get section

var application fakeStruct.Application
err := section.Bind(&application) //bind section to struct

Refer

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KeyDelimiter = "."

Functions

func Cast

func Cast(name string, fn TypeConversionFunc) (interface{}, bool)

func CastOrDefault

func CastOrDefault(name string, defaultValue interface{}, fn TypeConversionFunc) interface{}

func CombinePath

func CombinePath(pathSegments ...string) string

Combine paths to one path with KeyDelimiter

func ExtractMap

func ExtractMap(data interface{}, path string) map[string]*ExtractedValue

func ExtractStructToMap

func ExtractStructToMap(data interface{}, path string) map[string]*ExtractedValue

func GetConfiguration

func GetConfiguration(providers []Provider, name string) (string, bool)

func GetParentPath

func GetParentPath(path string) (string, bool)

Get parent path

func GetSectionKey

func GetSectionKey(path string) string

Get last path segment

func GetString

func GetString(name string) (string, bool)

func GetStringOrDefault

func GetStringOrDefault(name string, defaultValue string) string

func Init

func Init(fn func(Builder))

func Segment

func Segment(key string, prefixLength int) string

Types

type Builder

type Builder interface {
	Properties() map[string]interface{}
	Sources() []Source
	AddSource(source Source) Builder
	BuildRoot() Root
}

func NewBuilder

func NewBuilder() Builder

type Configuration

type Configuration interface {
	GetString(name string) (string, bool)
	GetExtracted(name string) (*ExtractedValue, bool)
	GetSection(name string) Section
	GetChildren() []Section
}

type ConfigurationBuilder

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

func (*ConfigurationBuilder) AddSource

func (builder *ConfigurationBuilder) AddSource(source Source) Builder

func (*ConfigurationBuilder) BuildRoot

func (builder *ConfigurationBuilder) BuildRoot() Root

func (*ConfigurationBuilder) Properties

func (builder *ConfigurationBuilder) Properties() map[string]interface{}

func (*ConfigurationBuilder) Sources

func (builder *ConfigurationBuilder) Sources() []Source

type ConfigurationProvider

type ConfigurationProvider struct {
	Data map[string]*ExtractedValue
}

func NewConfigurationProvider

func NewConfigurationProvider() *ConfigurationProvider

func (*ConfigurationProvider) GetChildKeys

func (provider *ConfigurationProvider) GetChildKeys(path string, earlierKeys ...string) []string

基于此返回给定父路径的直接后代配置键

func (*ConfigurationProvider) GetExtracted

func (provider *ConfigurationProvider) GetExtracted(name string) (*ExtractedValue, bool)

func (*ConfigurationProvider) GetString

func (provider *ConfigurationProvider) GetString(name string) (string, bool)

尝试获取一个配置key的值

func (*ConfigurationProvider) Load

func (provider *ConfigurationProvider) Load() error

type ConfigurationRoot

type ConfigurationRoot struct {
	Providers []Provider
}

func NewRoot

func NewRoot(providers []Provider) *ConfigurationRoot

func (*ConfigurationRoot) GetChildren

func (root *ConfigurationRoot) GetChildren() []Section

func (*ConfigurationRoot) GetExtracted

func (root *ConfigurationRoot) GetExtracted(name string) (*ExtractedValue, bool)

func (*ConfigurationRoot) GetProviders

func (root *ConfigurationRoot) GetProviders() []Provider

func (*ConfigurationRoot) GetSection

func (root *ConfigurationRoot) GetSection(name string) Section

func (*ConfigurationRoot) GetString

func (root *ConfigurationRoot) GetString(name string) (string, bool)

func (*ConfigurationRoot) Reload

func (root *ConfigurationRoot) Reload() error

type ConfigurationSection

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

func NewSection

func NewSection(root Root, path string) *ConfigurationSection

func (*ConfigurationSection) Bind

func (section *ConfigurationSection) Bind(recv interface{}) error

func (*ConfigurationSection) GetChildren

func (section *ConfigurationSection) GetChildren() []Section

func (*ConfigurationSection) GetExtracted

func (section *ConfigurationSection) GetExtracted(name string) (*ExtractedValue, bool)

func (*ConfigurationSection) GetName

func (section *ConfigurationSection) GetName() string

func (*ConfigurationSection) GetPath

func (section *ConfigurationSection) GetPath() string

func (*ConfigurationSection) GetRaw

func (section *ConfigurationSection) GetRaw() (interface{}, bool)

func (*ConfigurationSection) GetSection

func (section *ConfigurationSection) GetSection(name string) Section

func (*ConfigurationSection) GetString

func (section *ConfigurationSection) GetString(name string) (string, bool)

func (*ConfigurationSection) GetValue

func (section *ConfigurationSection) GetValue() (string, bool)

type ExtractedValue

type ExtractedValue struct {
	Name  string
	Value string
	Raw   interface{}
}

func GetConfigExtractedValue

func GetConfigExtractedValue(providers []Provider, name string) (*ExtractedValue, bool)

func NewExtractedValue

func NewExtractedValue(name string, value string, raw interface{}) *ExtractedValue

type MemoryProvider

type MemoryProvider struct {
	ConfigurationProvider
	// contains filtered or unexported fields
}

func NewMemoryProvider

func NewMemoryProvider(source *MemorySource) *MemoryProvider

func (*MemoryProvider) Add

func (provider *MemoryProvider) Add(key, value string)

type MemorySource

type MemorySource struct {
	InitialData map[string]string
}

func EnvironmentVariable

func EnvironmentVariable(prefix string) *MemorySource

func Memory

func Memory(initialData map[string]string) *MemorySource

func (*MemorySource) BuildProvider

func (source *MemorySource) BuildProvider(builder Builder) Provider

type Provider

type Provider interface {
	Load() error
	// 尝试获取一个配置key的值
	GetString(name string) (string, bool)
	GetExtracted(name string) (*ExtractedValue, bool)
	// 基于此返回给定父路径的直接后代配置键
	GetChildKeys(path string, earlierKeys ...string) []string
}

type Root

type Root interface {
	Configuration

	Reload() error
	GetProviders() []Provider
}

func ConfigRoot

func ConfigRoot() Root

type Section

type Section interface {
	Configuration

	GetName() string
	GetPath() string
	GetValue() (string, bool)
	GetRaw() (interface{}, bool)
	Bind(recv interface{}) error
}

func GetChildrenFromRoot

func GetChildrenFromRoot(root Root, path string) []Section

func GetSection

func GetSection(name string) Section

type Source

type Source interface {
	BuildProvider(builder Builder) Provider
}

type TypeConversionFunc

type TypeConversionFunc func(string) (interface{}, error)
var (
	IntConversion TypeConversionFunc = func(s string) (interface{}, error) {
		v, err := strconv.ParseFloat(s, 64)
		if err != nil {
			return nil, err
		}
		return int(v), nil
	}

	FloatConversion TypeConversionFunc = func(s string) (interface{}, error) {
		v, err := strconv.ParseFloat(s, 64)
		if err != nil {
			return nil, err
		}
		return v, nil
	}

	BooleanConversion TypeConversionFunc = func(s string) (interface{}, error) {
		v, err := strconv.ParseFloat(s, 64)
		if err != nil {
			return strings.ToLower(s) == "true", nil
		}
		return v > 0, nil
	}
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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