conf

package
v1.2.0-rc Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist      = errors.New("not exist")
	ErrInvalidSyntax = errors.New("invalid syntax")
)

Functions

func BindValue

func BindValue(p ReadOnlyProperties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) (RetErr error)

BindValue binds properties to a value.

func RegisterConverter

func RegisterConverter[T any](fn Converter[T])

RegisterConverter registers its 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 its Reader for some kind of file extension.

func RegisterSplitter

func RegisterSplitter(name string, fn Splitter)

RegisterSplitter registers a Splitter and named it.

func RegisterValidateFunc added in v1.2.0

func RegisterValidateFunc[T any](name string, fn ValidateFunc[T])

RegisterValidateFunc registers a validation function with a specific name. The function can then be used in validation expressions.

Types

type BindParam

type BindParam struct {
	Key      string            // full key
	Path     string            // full path
	Tag      ParsedTag         // parsed tag
	Validate reflect.StructTag // full field tag
}

func (*BindParam) BindTag

func (param *BindParam) BindTag(tag string, validate reflect.StructTag) error

type Converter

type Converter[T any] func(string) (T, error)

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

type Filter added in v1.1.2

type Filter interface {
	Do(i interface{}, param BindParam) (bool, error)
}

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.

func (ParsedTag) String added in v1.2.0

func (tag ParsedTag) String() string

type Properties

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

Properties stores the data with map[string]string and the 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 getting or binding properties. A good rule of thumb is that treating application configuration as a tree, but not all formats of configuration files designed as a tree or not ideal, for instance Java properties isn't 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 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 (*Properties) Bind

func (p *Properties) Bind(i interface{}, tag ...string) 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' tag 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) CopyTo added in v1.2.0

func (p *Properties) CopyTo(out *Properties) error

CopyTo copies properties into another by override.

func (*Properties) Data added in v1.2.0

func (p *Properties) Data() map[string]string

func (*Properties) Get

func (p *Properties) Get(key string, def ...string) 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) Merge

func (p *Properties) Merge(m map[string]interface{}) error

Merge flattens the map and sets all keys and values.

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.

func (*Properties) SubKeys added in v1.2.0

func (p *Properties) SubKeys(key string) ([]string, error)

SubKeys returns the sorted sub keys of the key.

type ReadOnlyProperties

type ReadOnlyProperties interface {
	// Data returns key-value pairs of the properties.
	Data() map[string]string
	// Keys returns keys of the properties.
	Keys() []string
	// Has returns whether the key exists.
	Has(key string) bool
	// SubKeys returns the sorted sub keys of the key.
	SubKeys(key string) ([]string, error)
	// Get returns key's value.
	Get(key string, def ...string) string
	// Resolve resolves string that contains references.
	Resolve(s string) (string, error)
	// Bind binds properties into a value.
	Bind(i interface{}, tag ...string) error
	// CopyTo copies properties into another by override.
	CopyTo(out *Properties) error
}

ReadOnlyProperties is the interface for read-only properties.

type Reader

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

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

type Splitter

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

Splitter splits string into []string by some characters.

type ValidateFunc added in v1.2.0

type ValidateFunc[T any] func(T) bool

ValidateFunc defines a type for validation functions, which accept a value of type T and return a boolean result.

Directories

Path Synopsis
reader

Jump to

Keyboard shortcuts

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