package
1.1.3
Repository: https://github.com/infinitbyte/framework.git
Documentation: pkg.go.dev

# README

Build Status Go Report
Card codecov

ucfg - Universal Configuration

ucfg is a Golang library to handle hjson, json, and yaml configuration files in your Golang project. It was developed for the libbeat framework and used by all beats.

API Documentation

The full API Documentation can be found here.

Examples

A few examples on how ucfg can be used. All examples below assume, that the following packages are imported:

import (
	"infini.sh/framework/lib/go-ucfg"
	"infini.sh/framework/lib/go-ucfg/yaml"
)

Dot notations

ufcg allows you to load yaml configuration files using dots instead of indentation. For example instead of having:

config:
  user: name

with ucfg you can write:

config.user: name

This makes configurations easier and simpler.

To load such a config file in Golang, use the following command:

config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))

Validation and Defaults

ucfg allows to automatically validate fields and set defaults for fields in case they are not defined.

// Defines struct to read config from
type ExampleConfig struct {
    Counter  int 	`config:"counter" validate:"min=0, max=9"`
}

// Defines default config option
var (
    defaultConfig = ExampleConfig{
		    Counter: 4,
    }
)

func main() {
    appConfig := defaultConfig // copy default config so it's not overwritten
    config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    err = config.Unpack(&appConfig)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

The above uses Counter as the config variable. ucfg assures that the value is between 0 and 9 and will return an error if this is not the case. In addition, if the value is not set, it will default to 4.

Requirements

ucfg has the following requirements:

  • Golang 1.10+

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Copyright (c) 2006-2011 Kirill Simonov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

# Functions

No description provided by the author
DefaultParseConfig option sets the default parse config used to parse dyn value if it is not empty.
No description provided by the author
Env option adds another configuration for variable expansion to be used, if the path to look up does not exist in the actual configuration.
MetaData option passes additional metadata (currently only source of the configuration) to be stored internally (e.g.
MustNewFrom creates a new config object normalizing and copying from into the new Config object.
New creates a new empty Config object.
NewFrom creates a new config object normalizing and copying from into the new Config object.
PathSep sets the path separator used to split up names into a tree like hierarchy.
RegisterValidator adds a new validator option to the "validate" struct tag.
Resolve option adds a callback used by variable name expansion.
StructTag option sets the struct tag name to use for looking up field names and options in `Unpack` and `Merge`.
ValidatorTag option sets the struct tag name used to set validators on struct fields in `Unpack`.

# Constants

var ErrMalformed = errors.New("secret string malformed").
var ErrMalformed = errors.New("secret string malformed").

# Variables

AppendValues option configures all merging and unpacking operations to merge dictionaries and append arrays to existing arrays while merging.
Error Reasons.
Error Reasons.
Error Classes.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Classes.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Reasons.
Error Classes.
Error Reasons.
FieldAppendValues option configures all merging and unpacking operations to merge dictionaries and append arrays to existing arrays while merging for the specified field.
FieldMergeValues option configures all merging and unpacking operations to use the default merging behavior for the specified field.
FieldPrependValues option configures all merging and unpacking operations to merge dictionaries and prepend arrays to existing arrays while merging for the specified field.
FieldReplaceValues option configures all merging and unpacking operations to replace old dictionaries and arrays while merging for the specified field.
NoResolve option sets do not to resolve variables.
PrependValues option configures all merging and unpacking operations to merge dictionaries and prepend arrays to existing arrays while merging.
ReplaceValues option configures all merging and unpacking operations to replace old dictionaries and arrays while merging.
ResolveEnv option adds a look up callback looking up values in the available OS environment variables.
ResolveNOOP option add a resolver that will not search the value but instead will return the provided key wrap with the field reference syntax.
ResolveRef option enables support for variable resolve reference from parent config.
VarExp option enables support for variable expansion.

# Structs

Config object to store hierarchical configurations into.
Meta holds additional meta data per config value.

# Interfaces

BoolUnpacker interface specializes the Unpacker interface by casting values to bool when calling Unpack.
ConfigUnpacker interface specializes the Unpacker interface by passing the the *Config object directly instead of transforming the *Config object into map[string]interface{}.
Error type returned by all public functions in go-ucfg.
FloatUnpacker interface specializes the Unpacker interface by casting values to float64 when calling Unpack.
Initializer interface provides initialization of default values support to Unpack.
IntUnpacker interface specializes the Unpacker interface by casting values to int64 when calling Unpack.
StringUnpacker interface specializes the Unpacker interface by casting values to string when calling Unpack.
UintUnpacker interface specializes the Unpacker interface by casting values to uint64 when calling Unpack.
Unpacker type used by Unpack to allow types to implement custom configuration unpacking.
Validator interface provides additional validation support to Unpack.

# Type aliases

Option type implementing additional options to be passed to go-ucfg library functions.
No description provided by the author
ValidatorCallback is the type of optional validator tags to be registered via RegisterValidator.