configs

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

Adverax/configs

It is a lightweight library for managing application configurations using YAML files. It provides a simple and efficient way to organize, load, and validate configuration data for your projects.

Features

  • Supports JSON and YAML formats.
  • Supports loading from files.
  • Supports loading from environment variables.
  • Supports loading from memory (or from command-line-arguments).
  • Supports migrations from one version to another.
  • Merge multiple configuration sources.
  • Support for nested and hierarchical configuration structures.
  • Support dynamic reloading of sources.
  • Extensible and customizable.

Installation

go get github.com/adverax/configs

Usage

Basic examples
  • see adverax/configs/formats/yaml/example_test.go for more information.
  • see adverax/configs/formats/json/example_test.go for more information.
  • see adverax/configs/formats/mix/example_test.go for more information.
  • see migrations_test.go for more information.
  • see adverax/configs/dynamic/example_test.go for more information.

## License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Example
package main

import (
	"fmt"
	"github.com/adverax/fetchers/maps/maps"
	"time"
)

type MyConfigAddress struct {
	Host string `json:"host"`
	Port int    `json:"port"`
}

type MyConfig struct {
	Address  MyConfigAddress `json:"address"`
	Name     string          `json:"name"`
	Interval time.Duration   `json:"interval"`
}

func DefaultConfig() *MyConfig {
	return &MyConfig{
		Address: MyConfigAddress{
			Host: "unknown",
			Port: 80,
		},
		Name: "unknown",
	}
}

func main() {
	// This example demonstrates how to use loader with migrations.
	//
	// First, create source:
	source := maps.Engine{
		"address": map[string]interface{}{
			"host": "google.com",
			"port": 90,
		},
		"name":     "My App",
		"interval": 10,
	}

	// Then create migrator:
	migrator := NewMigrator()
	migrator.Add(
		"1",
		func(data map[string]interface{}) error {
			if v, ok := data["interval"]; ok {
				data["interval"] = int64(time.Duration(v.(int)) * time.Second)
			}
			return nil
		},
	)

	// Then create loader:
	loader, err := NewBuilder().
		WithSource(NewSourceWithMigration(source, migrator)).
		Build()
	if err != nil {
		panic(err)
	}

	// Then load configuration:
	config := DefaultConfig()
	err = loader.Load(config)
	if err != nil {
		panic(err)
	}

	// Now you can use config.
	// For example, print it:
	fmt.Println(*config)

}
Output:

{{google.com 90} My App 10s}

Index

Examples

Constants

View Source
const MigrationKey = "migration"

Variables

View Source
var (
	ErrFieldFilesIsRequired     = fmt.Errorf("Files are required")
	ErrFieldBuilderIsRequired   = fmt.Errorf("Builder is required")
	ErrFieldConverterIsRequired = fmt.Errorf("Converter is required")
)
View Source
var (
	DefaultConverter = &defaultConverter{}
)
View Source
var (
	ErrDistinct = fmt.Errorf("config without changes")
)
View Source
var (
	ErrFieldSourcesIsRequired = fmt.Errorf("Field sources is required")
)

Functions

func Assign added in v1.0.1

func Assign(ctx context.Context, dst interface{}, src map[string]interface{}) error

Assign assigns values from src to dst.

func Let added in v1.0.1

func Let(ctx context.Context, dst interface{}, src interface{}) error

func RegisterHandler added in v1.0.1

func RegisterHandler(tp reflect.Type, handler TypeHandler)

Types

type BaseConfig added in v1.0.1

type BaseConfig struct {
	sync.RWMutex
}

type Boolean added in v1.0.1

type Boolean interface {
	Get(ctx context.Context) (bool, error)
}

type BooleanTypeHandler added in v1.0.1

type BooleanTypeHandler struct {
}

func (*BooleanTypeHandler) Let added in v1.0.1

func (that *BooleanTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Build

func (that *Builder) Build() (*Loader, error)

func (*Builder) WithConverter

func (that *Builder) WithConverter(converter Converter) *Builder

func (*Builder) WithDistinct added in v1.0.1

func (that *Builder) WithDistinct(distinct bool) *Builder

func (*Builder) WithSource

func (that *Builder) WithSource(sources ...Source) *Builder

type Config added in v1.0.1

type Config interface {
	Lock()
	Unlock()
	RLock()
	RUnlock()
}

type Converter

type Converter interface {
	Convert(dst interface{}, src map[string]interface{}) error
}

type Duration added in v1.0.1

type Duration interface {
	Get(ctx context.Context) (time.Duration, error)
}

type DurationTypeHandler added in v1.0.1

type DurationTypeHandler struct {
}

func (*DurationTypeHandler) Let added in v1.0.1

func (that *DurationTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Fetcher

type Fetcher interface {
	Fetch() ([]byte, error)
}

type FileLoaderBuilder

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

func NewFileLoaderBuilder

func NewFileLoaderBuilder() *FileLoaderBuilder

func (*FileLoaderBuilder) Build

func (that *FileLoaderBuilder) Build() (*Loader, error)

func (*FileLoaderBuilder) WithConverter

func (that *FileLoaderBuilder) WithConverter(converter Converter) *FileLoaderBuilder

func (*FileLoaderBuilder) WithFile

func (that *FileLoaderBuilder) WithFile(file string, mustExists bool) *FileLoaderBuilder

func (*FileLoaderBuilder) WithSource

func (that *FileLoaderBuilder) WithSource(sources ...Source) *FileLoaderBuilder

func (*FileLoaderBuilder) WithSourceBuilder

func (that *FileLoaderBuilder) WithSourceBuilder(builder SourceBuilder) *FileLoaderBuilder

type Float added in v1.0.1

type Float interface {
	Get(ctx context.Context) (float64, error)
}

type FloatTypeHandler added in v1.0.1

type FloatTypeHandler struct {
}

func (*FloatTypeHandler) Let added in v1.0.1

func (that *FloatTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Getter added in v1.0.1

type Getter[T any] interface {
	Get(ctx context.Context) (T, error)
}

type Importer added in v1.0.1

type Importer interface {
	Import(ctx context.Context, value interface{}) error
}

type Integer added in v1.0.1

type Integer interface {
	Get(ctx context.Context) (int64, error)
}

type IntegerTypeHandler added in v1.0.1

type IntegerTypeHandler struct {
}

func (*IntegerTypeHandler) Let added in v1.0.1

func (that *IntegerTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Letter added in v1.0.1

type Letter[T any] interface {
	Let(ctx context.Context, value T) error
}

type Loader

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

func (*Loader) Load

func (that *Loader) Load(config interface{}) error

type Migration added in v1.0.1

type Migration func(data map[string]interface{}) error

type Migrator added in v1.0.1

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

func NewMigrator added in v1.0.1

func NewMigrator() *Migrator

func (*Migrator) Add added in v1.0.1

func (that *Migrator) Add(id string, m Migration)

func (*Migrator) Migrate added in v1.0.1

func (that *Migrator) Migrate(data map[string]interface{}) error

type Registry added in v1.0.1

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

func NewRegistry added in v1.0.1

func NewRegistry() *Registry

func (*Registry) Get added in v1.0.1

func (that *Registry) Get(tp reflect.Type) TypeHandler

func (*Registry) Register added in v1.0.1

func (that *Registry) Register(tp reflect.Type, handler TypeHandler) *Registry

type Source

type Source interface {
	Fetch() (map[string]interface{}, error)
}

type SourceBuilder

type SourceBuilder func(Fetcher) Source

type SourceMigrator added in v1.0.1

type SourceMigrator interface {
	Migrate(data map[string]interface{}) error
}

type SourceWithMigration added in v1.0.1

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

func NewSourceWithMigration added in v1.0.1

func NewSourceWithMigration(source Source, migrator SourceMigrator) *SourceWithMigration

func (*SourceWithMigration) Fetch added in v1.0.1

func (that *SourceWithMigration) Fetch() (map[string]interface{}, error)

type String added in v1.0.1

type String interface {
	Get(ctx context.Context) (string, error)
}

type StringTypeHandler added in v1.0.1

type StringTypeHandler struct {
}

func (*StringTypeHandler) Let added in v1.0.1

func (that *StringTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Strings added in v1.0.1

type Strings interface {
	Get(ctx context.Context) ([]string, error)
}

type StringsTypeHandler added in v1.0.1

type StringsTypeHandler struct {
}

func (*StringsTypeHandler) Let added in v1.0.1

func (that *StringsTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type Time added in v1.0.1

type Time interface {
	Get(ctx context.Context) (time.Time, error)
}

type TimeTypeHandler added in v1.0.1

type TimeTypeHandler struct {
}

func (*TimeTypeHandler) Let added in v1.0.1

func (that *TimeTypeHandler) Let(ctx context.Context, dst interface{}, src interface{}) error

type TypeHandler added in v1.0.1

type TypeHandler interface {
	Let(ctx context.Context, dst interface{}, src interface{}) error
}

func HandlerOf added in v1.0.1

func HandlerOf(tp reflect.Type) TypeHandler

type Writer added in v1.0.1

type Writer interface {
	Save(data map[string]interface{}) error
}

Directories

Path Synopsis
formats

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳