Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 2.57 KB

README.md

File metadata and controls

54 lines (42 loc) · 2.57 KB

goconf

Configuration providers for go inspired by .net core configuration libs. Extensible and easy to use.

GitHub go.mod Go version GitHub tag (latest SemVer) GoDoc Go Report Card Unit-Tests

Supportives:

  • Memory source
  • Environment variables
  • JSON file
  • YAML file
  • INI file

Features:

  • Multi providers support (merge sources)
  • Extensible providers support (new providers can be added easily, even only one function to implement a new provider using file from local filesystem)
  • Get value by full path with key delimiter (e.g. application.bind_addr.port)
  • Cast value to specialized type with TypeConversionFunc
  • Bind configuration section to struct

Usage

  • package: github.com/ah-its-andy/goconf
// initialize on application startup
goconf.Init(func(b goconf.Builder) {
	b.AddSource(physicalfile.Yaml(/*yaml file path, absolute or relative  both supported*/)))
     .AddSource(physicalfile.Json(/*json file path, absolute or relative  both supported*/))
     .AddSource(goconf.EnvironmentVariable(/*prefix for filter environment variables*/))
     .AddSource(goconf.Memory(/*config map*/))
})

// use it anywhere
bindAddr, ok := goconf.GetString("application.bind_addr.addr") //Get string value

bindAddrWithDefault := goconf.GetStringOrDefault("application.bind_addr.addr", "default value") //returns default value when key is not found

castValue, ok := goconf.Cast("application.bind_addr.port", goconf.IntConversion) //cast value to int

castValueWithDefault := goconf.CastOrDefault("application.bind_addr.port", 0 /*default value*/, goconf.IntConversion) //cast value to int, returns default value when key is not found

section:= gocinf.GetSection("application") //get section

var application fakeStruct.Application
err := section.Bind(&application) //bind section to struct

Refer