conf

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package conf reads configuration from many format file, such as Java properties, yaml, toml, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindValue

func BindValue(p *Properties, v reflect.Value, param BindParam) error

BindValue binds properties to a value.

func IsPrimitiveValueType

func IsPrimitiveValueType(t reflect.Type) bool

IsPrimitiveValueType returns whether the input type is the primitive value type which only is int, unit, float, bool, string and complex.

func IsValueType

func IsValueType(t reflect.Type) bool

IsValueType returns whether the input type is the value type which is the primitive value type and their one dimensional composite type including array, slice, map and struct, such as [3]string, []string, []int, map[int]int, etc.

func RegisterConverter

func RegisterConverter(fn Converter)

RegisterConverter registers Converter for non-primitive type such as time.Time, time.Duration, or other user-defined value type.

func RegisterReader

func RegisterReader(r Reader, ext ...string)

RegisterReader registers Reader for some file extensions.

func RegisterSplitter

func RegisterSplitter(name string, fn Splitter)

RegisterSplitter registers Splitter and named it.

func SplitPath

func SplitPath(key string) ([]string, error)

SplitPath splits the key into individual parts.

Types

type BindOption

type BindOption func(arg *bindArg)

func Key

func Key(key string) BindOption

Key binds properties using one key.

func Tag

func Tag(tag string) BindOption

Tag binds properties using one tag.

type BindParam

type BindParam struct {
	Type reflect.Type // reflection type of binding value
	Key  string       // full property key
	Path string       // binding path
	// contains filtered or unexported fields
}

func (*BindParam) BindTag

func (param *BindParam) BindTag(tag string) error

func (*BindParam) Tag

func (param *BindParam) Tag() ParsedTag

type Converter

type Converter interface{}

Converter converts string value into user-defined value. It should be function type, and its prototype is func(string)(type,error).

type GetOption

type GetOption func(arg *getArg)

func Def

func Def(v string) GetOption

Def returns v when key not exits.

type ParsedTag

type ParsedTag struct {
	Key      string // short property key
	Def      string // default value
	HasDef   bool   // has default value
	Splitter string // splitter's name
}

ParsedTag a value tag includes at most three parts: required key, optional default value, and optional splitter, the syntax is ${key:=value}||splitter.

func ParseTag

func ParseTag(tag string) (ret ParsedTag, err error)

ParseTag parses a value tag, returns its key, and default value, and splitter.

type Properties

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

Properties stores properties with map[string]string and keys are case-sensitive, you can get one of them by its key, or bind some of them to a value. There are too many formats of configuration files, and too many conflicts between them. Each format of configuration file provides its special characteristics, but usually they are not all necessary, and complementary. For example, conf disabled Java properties' expansion when reading file, but also provides similar function when get or bind properties. A good rule of thumb is that treating application configuration as a tree, but not all formats of configuration files designed like this or not ideal, such as Java properties which not strictly verified. Although configuration can store as a tree, but it costs more CPU time when getting properties because it reads property node by node. So conf uses a tree to strictly verify and a flat map to store.

func Bytes

func Bytes(b []byte, ext string) (*Properties, error)

Bytes creates *Properties from []byte, ext is the file name extension.

func Load

func Load(file string) (*Properties, error)

Load creates *Properties from file.

func Map

func Map(m map[string]interface{}) (*Properties, error)

Map creates *Properties from map.

func New

func New() *Properties

New creates empty *Properties.

func Read

func Read(r io.Reader, ext string) (*Properties, error)

Read creates *Properties from io.Reader, ext is the file name extension.

func (*Properties) Bind

func (p *Properties) Bind(i interface{}, opts ...BindOption) error

Bind binds properties to a value, the bind value can be primitive type, map, slice, struct. When binding to struct, the tag 'value' indicates which properties should be bind. The 'value' tags are defined by value:"${a:=b|splitter}", 'a' is the key, 'b' is the default value, 'splitter' is the Splitter's name when you want split string value into []string value.

func (*Properties) Bytes

func (p *Properties) Bytes(b []byte, ext string) error

Bytes loads properties from []byte, ext is the file name extension.

func (*Properties) Get

func (p *Properties) Get(key string, opts ...GetOption) string

Get returns key's value, using Def to return a default value.

func (*Properties) Has

func (p *Properties) Has(key string) bool

Has returns whether key exists.

func (*Properties) Keys

func (p *Properties) Keys() []string

Keys returns all sorted keys.

func (*Properties) Load

func (p *Properties) Load(file string) error

Load loads properties from file.

func (*Properties) Resolve

func (p *Properties) Resolve(s string) (string, error)

Resolve resolves string value that contains references to other properties, the references are defined by ${key:=def}.

func (*Properties) Set

func (p *Properties) Set(key string, val interface{}) error

Set sets key's value to be a primitive type as int or string, or a slice or map nested with primitive type elements. One thing you should know is Set actions as overlap but not replace, that means when you set a slice or a map, an existing path will remain when it doesn't exist in the slice or map even they share a same prefix path.

type Reader

type Reader func(b []byte) (map[string]interface{}, error)

Reader parses []byte value into nested map[string]interface{}.

type Splitter

type Splitter func(string) ([]string, error)

Splitter splits string value into []string value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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