ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - romshark/yamagiconf: The magic YAML configuration framework for Go
The magic YAML configuration framework for Go. Contribute to romshark/yamagiconf development by creating an account on GitHub.
Visit Site

GitHub - romshark/yamagiconf: The magic YAML configuration framework for Go

GitHub - romshark/yamagiconf: The magic YAML configuration framework for Go

yamagiconf

yamagiconf

The heavily opinionated YAML Magic Configuration framework for Go keeps your configs simple and consistent by being more restrictive than your regular YAML parser ๐Ÿšท allowing only a subset of YAML and enforcing some restrictions to the target Go type.

If you hate YAML, and you're afraid of YAML documents from hell, and you can't stand complex, unexplorable and unintuitive configurations then yamagiconf is for you!

๐Ÿช„ It's magic because it uses reflect to find recursively all values of types that implement interface { Validate() error } and calls them reporting an error annotated with line and column in the YAML file if necessary.

(anti-)Features

  • Go restrictions:
    • ๐Ÿšซ Forbids recursive Go types.
    • ๐Ÿšซ Forbids the use of any, int & uint (unspecified width), and other types. Only maps, slices, arrays and deterministic primitives are allowed.
    • โ—๏ธ Requires yaml struct tags on all exported fields.
    • โ—๏ธ Requires env struct tags to be POSIX-style.
    • ๐Ÿšซ Forbids the use of env struct tag on non-primitive fields. Allows only floats, ints, strings, bool and types that implement the encoding.TextUnmarshaler interface.
    • ๐Ÿšซ Forbids the use of env on primitive fields implementing the yaml.Unmarshaler interface.
    • ๐Ÿšซ Forbids the use of yaml and env struct tags within implementations of encoding.TextUnmarshaler and/or yaml.Unmarshaler.
    • ๐Ÿšซ Forbids the use of YAML struct tag option "inline" for non-embedded structs and requires embedded structs to use option "inline".
  • YAML restrictions:
    • ๐Ÿšซ Forbids the use of no, yes, on and off for bool, allows only true and false.
    • ๐Ÿšซ Forbids the use of ~, Null and other variations, allows only null for nilables.
    • ๐Ÿšซ Forbids assigning null to non-nilables (which normally would assign zero value).
    • ๐Ÿšซ Forbids fields in the YAML file that aren't specified by the Go type.
    • ๐Ÿšซ Forbids the use of YAML tags.
    • ๐Ÿšซ Forbids redeclaration of anchors.
    • ๐Ÿšซ Forbids unused anchors.
    • ๐Ÿšซ Forbids anchors with implicit null value (no value) like foo: &bar.
    • โ—๏ธ Requires fields specified in the configuration type to be present in the YAML file.
    • ๐Ÿšซ Forbids assigning non-string values to Go types that implement the encoding.TextUnmarshaler interface.
    • ๐Ÿšซ Forbids empty array items (see rationale).
    • ๐Ÿšซ Forbids multi-document files.
    • ๐Ÿšซ Forbids YAML merge keys.
  • Features:
    • ๐Ÿช„ If any type within your configuration struct implements the Validate interface, then its validation method will be called using reflection (doesn't apply to unexported fields which are invisible to reflect). If it returns an error - the error will be reported. Keeps your validation logic close to your configuration type definitions.
    • Reports errors by line:column when possible.
    • Supports github.com/go-playground/validator validation struct tags.
    • Implements env struct tags to overwrite fields from env vars if provided.
    • Supports encoding.TextUnmarshaler and yaml.Unmarshaler (except for the root struct type).
    • Supports time.Duration.

Example

https://go.dev/play/p/PjV0aG7uIUH

list:
  - foo: valid
    bar: valid
  - foo: valid
    bar: valid
map:
  valid: valid
secret: 'this will be overwritten from env var SECRET'
required: 'this must not be empty'
package main

import (
	"fmt"

	"github.com/romshark/yamagiconf"
)

type Config struct {
	List []Struct                            `yaml:"list"`
	Map  map[ValidatedString]ValidatedString `yaml:"map"`

	// Secret will be overwritten if env var SECRET is set.
	Secret string `yaml:"secret" env:"SECRET"`

	// See https://github.com/go-playground/validator
	// for all available validation tags
	Required string `yaml:"required" validate:"required"`
}

type Struct struct {
	Foo string          `yaml:"foo"`
	Bar ValidatedString `yaml:"bar"`
}

// Validate will automatically be called by yamagiconf
func (v *Struct) Validate() error {
	if v.Foo == "invalid" {
		return fmt.Errorf("invalid foo")
	}
	if v.Bar == "invalid" {
		return fmt.Errorf("invalid bar")
	}
	return nil
}

type ValidatedString string

// Validate will automatically be called by yamagiconf
func (v ValidatedString) Validate() error {
	if v == "invalid" {
		return fmt.Errorf("string is invalid")
	}
	return nil
}

func main() {
	var c Config
	if err := yamagiconf.LoadFile("./config.yaml", &c); err != nil {
		fmt.Println("Whoops, something is wrong with your config!", err)
	}
	fmt.Printf("%#v\n", c)
}

FAQ

Why are empty array items forbidden?

Consider the following YAML array:

array:
  - 
  - ''
  - ""
  - x

Even though this YAML array works as expect with a Go array: [4]string{"", "", "", "x"}, parsing the same YAML into a Go slice will result in the empty item being ommited: []string{"", "", "x"} which is counterintuitive. Therefore, yamagiconf forbids empty array items in general to keep behavior consistent and intuitive independent of the Go target type.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory