tinyconf

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 5 Imported by: 0

README

codecov build Goreport GoDoc

tinyconf

tinyconf - a simple and universal library for parsing configs.

Installation

Install via go get. Note that Go 1.19 or newer is required.

go get github.com/insei/tinyconf@latest

Description

tinyconf parses the config and returns the config structure. When initializing tinyconf.Manager, you can add a logger to it, as well as drivers (for priority in parsing the config, i.e. it will check them starting from the last one you specified). You can also add your own driver.

tinyconf.Manager has methods:

Register(conf any) error
Parse(conf any) error 

where:
Register(conf any) error - registers map[strings]fmap.Field for the config.
Parse(conf any) error - parses config from registered drivers.

Example

package main

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/insei/tinyconf"
	"tinyconf/drivers/env"
	"tinyconf/drivers/tag"
	"tinyconf/drivers/yaml"
	"tinyconf/logger"
)

type Embedded struct {
	Test string `initial:"Shakalaka"`
}

type DefConf struct {
	Embedded Embedded
	Test     string    `initial:"123" yaml:"test" env:"DEFAULT_TEST"`
	Test2    int       `initial:"123"  yaml:"test2"`
	Test3    int32     `initial:"2" yaml:"test3,omitempty"`
	Test4    *int32    `initial:"3" yaml:"test4,omitempty"`
	Test5    *string   `initial:"*string" yaml:"test5,omitempty" env:"DEFAULT_TEST"`
	Test6    string    `yaml:"test6,omitempty"`
	Test7    uuid.UUID `initial:"f9a49892-860e-48ec-b927-73f9d2560eec"`
	Password string    `initial:"Qwerty" yaml:"password" hidden:"true"`
}

func main() {
	yamlDriver, err := yaml.New("config.yaml")
	if err != nil {
		return
	}
	envDriver, err := env.New()
	if err != nil {
		return
	}
	tagDriver, err := tag.New("initial")
	if err != nil {
		return
	}
	config, err := tinyconf.New(
		tinyconf.WithLogger(logger.NewFmtLogger(logger.TRACE)),
		tinyconf.WithDriver(tagDriver),
		tinyconf.WithDriver(yamlDriver),
		tinyconf.WithDriver(envDriver),
	)
	if err != nil {
		return
	}
	c := DefConf{
		Embedded: Embedded{},
		Test:     "Tests",
		Test2:    22,
		Test3:    0,
		Test4:    nil,
		Test5:    nil,
		Test6:    "",
		Test7:    uuid.UUID{},
		Password: "",
	}
	err = config.Register(&c)
	if err != nil {
		panic(err)
	}
	fmt.Println(config.GenDoc("yaml"))
	err = config.Parse(&c)
	if err != nil {
		panic(err)
	}
	fmt.Print(c)
}

More examples in parser_test.go and others test files.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotRegisteredConfig  = fmt.Errorf("config is not registered")
	ErrValueNotFound        = fmt.Errorf("value was not found")
	ErrIncorrectTagSettings = fmt.Errorf("incorrect tag settings")
)

Functions

This section is empty.

Types

type Driver

type Driver interface {
	GenDoc(...*Registered) string
	GetName() string
	GetValue(field fmap.Field) (*Value, error)
}

type Field

type Field struct {
	Key   string
	Value interface{}
}

func LogField

func LogField(key string, value interface{}) Field

type Logger

type Logger interface {
	Debug(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	With(fields ...Field) Logger
}

type Manager

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

func New

func New(opts ...Option) (*Manager, error)

func (*Manager) GenDoc

func (c *Manager) GenDoc(driverName string) string

func (*Manager) Parse

func (c *Manager) Parse(conf any) (err error)

func (*Manager) Register

func (c *Manager) Register(conf any) error

type Option

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

func WithDriver

func WithDriver(driver Driver) Option

func WithLogger

func WithLogger(logger Logger) Option

type Registered

type Registered struct {
	Storage fmap.Storage
	Config  any
}

type Value

type Value struct {
	Source string
	Value  interface{}
}

Directories

Path Synopsis
drivers
env
tag
Package slices118 defines various functions useful with slices118 of any type.
Package slices118 defines various functions useful with slices118 of any type.

Jump to

Keyboard shortcuts

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