gofigure

package module
v0.0.0-...-a766b49 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 7 Imported by: 0

README

GoFigure - a simple yet powerful and extensible configuration library for Golang

Lint Status Test Status Coverage Status Go Reference

Example

config/app.yaml

env: dev
port: 8080
host: localhost
listen: !tpl |
  {{ config "app.host" }}:{{ config "app.port" }}
db_host: !ref storage.db.host
database: !tpl |
  mysql://{{ config "storage.db.user" }}:{{ config "storage.db.password" }}@{{ config "storage.db.host" }}:{{ config "storage.db.port" }}
external: !include
  file:
    path: external/test.yaml
    parse: true
    key: value

config/storage/db.yaml

host: localhost
port: 3306
user: root

config/prod/app.yaml

env: prod
port: 80

config/prod/storage/db.yaml

host: remote-address
password: supersecret

config/external/test.yaml

value: hello world

main.go

var defaultYaml []byte // config/app.yaml
var envYaml []byte // config/prod/app.yaml
var defaultDbYaml []byte // config/storage/db.yaml
var envDbYaml []byte // config/prod/storage/db.yaml

loader := gofigure.New().WithFeatures(
	feature.Reference(),
    feature.Template()/*.WithFuncs(template.Funcs{}).WithValeus(map[stirng]any{}) */,
	feature.Include(os.DirFS("./config")),
)
_ = loader.Load("app.yaml", defaultYaml)
_ = loader.Load("storage/db.yaml", defaultDbYaml)
_ = loader.Load("app.yaml", envYaml)
_ = loader.Load("storage/db.yaml", envDbYaml)
var app struct {
    Env      string `yaml:"env"`
    Listen   string `yaml:"listen"`
    Database string `yaml:"database"`
    External string `yaml:"external"`
}
_ = loader.Get(context.Background(), "app", &app)
fmt.Println(app.Env) // prod
fmt.Println(app.Listen) // localhost:80
fmt.Println(app.Database) // mysql://root:supersecret@remote-address:3306
fmt.Println(app.External) // hello world 

Introduction

GoFigure is a tool to allow maximum flexibility in configuration loading and parsing. It is designed to be simple to use, yet powerful and extensible. It comes with default features like include other files, render a go template and reference other values, etc.

You can easily extend GoFigure with your own features with ease, please check feature for examples.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPathNotFound     = errors.New("path not found")
	ErrConfigParseError = errors.New("config parse error")
	ErrInvalidPath      = errors.New("invalid path")
)

Functions

func IsNodeError

func IsNodeError(err error) bool

func NewNodeError

func NewNodeError(node *Node, err error) error

Types

type Feature

type Feature interface {
	Name() string
	Resolve(ctx context.Context, loader *Loader, node *Node) (*Node, error)
}

func FeatureFunc

func FeatureFunc(name string, resolve ResolveFunc) Feature

type Loader

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

func New

func New() *Loader

func (*Loader) Get

func (l *Loader) Get(ctx context.Context, path string, target any) error

func (*Loader) GetNode

func (l *Loader) GetNode(ctx context.Context, path string) (*Node, error)

func (*Loader) Load

func (l *Loader) Load(name string, contents []byte) error

func (*Loader) WithFeatures

func (l *Loader) WithFeatures(features ...Feature) *Loader

type Node

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

func MergeNodes

func MergeNodes(nodes ...*Node) (*Node, error)

func NewMappingNode

func NewMappingNode(m map[string]*Node, options ...NodeOption) *Node

func NewNode

func NewNode(node *yaml.Node, options ...NodeOption) *Node

func NewScalarNode

func NewScalarNode(value string, options ...NodeOption) *Node

func NewSequenceNode

func NewSequenceNode(values []*Node, options ...NodeOption) *Node

func PackNodeInNestedKeys

func PackNodeInNestedKeys(node *Node, keys ...string) *Node

func (*Node) Anchor

func (n *Node) Anchor() string

func (*Node) BoolValue

func (n *Node) BoolValue() (bool, error)

func (*Node) Column

func (n *Node) Column() int

func (*Node) Filepath

func (n *Node) Filepath() string

func (*Node) FootComment

func (n *Node) FootComment() string

func (*Node) GetDeep

func (n *Node) GetDeep(path string) (*Node, error)

func (*Node) GetMappingChild

func (n *Node) GetMappingChild(key string) (*Node, error)

func (*Node) GetSequenceChild

func (n *Node) GetSequenceChild(index int) (*Node, error)

func (*Node) HeadComment

func (n *Node) HeadComment() string

func (*Node) Keypath

func (n *Node) Keypath() string

func (*Node) Kind

func (n *Node) Kind() yaml.Kind

func (*Node) Line

func (n *Node) Line() int

func (*Node) LineComment

func (n *Node) LineComment() string

func (*Node) MarshalYAML

func (n *Node) MarshalYAML() (interface{}, error)

func (*Node) RawValue

func (n *Node) RawValue() string

func (*Node) Style

func (n *Node) Style() yaml.Style

func (*Node) Tag

func (n *Node) Tag() string

func (*Node) ToYAMLNode

func (n *Node) ToYAMLNode() *yaml.Node

func (*Node) UnmarshalYAML

func (n *Node) UnmarshalYAML(value *yaml.Node) error

func (*Node) Value

func (n *Node) Value() string

type NodeOption

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

func NodeFilepath

func NodeFilepath(path string) NodeOption

func NodeMappingKey

func NodeMappingKey(key string) NodeOption

func NodeParent

func NodeParent(parent *Node) NodeOption

func NodeSequenceIndex

func NodeSequenceIndex(index int) NodeOption

type ResolveFunc

type ResolveFunc func(ctx context.Context, loader *Loader, node *Node) (*Node, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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