Reglue Go

Config

tests linux macos windows

Table Of Contents

Introduction

The config package provides an expressive agnostic interface for working with configuration loaders. You can use third party loaders like viper.

package main

import (
	"github.com/reglue4go/config"

	"github.com/knadh/koanf"
	"github.com/knadh/koanf/parsers/yaml"
	"github.com/knadh/koanf/providers/file"
)

func main() {
	repo := createRepository()

	httpPort := repo.Get("server.ports.http").ToInt()

	jdbcPort := repo.Get("server.ports.jdbc", 1521).ToInt()

	fmt.Printf("%v\n", httpPort) // 8080
	fmt.Printf("%v\n", jdbcPort) // 1521
}

// you can use viper or any other loader
func createRepository() *config.Repository {
	handler := koanf.New(".") // Use "." as the key path delimiter.
	repository := config.New().
		AddLoader(func(_ *config.Repository) {
			handler.Load(file.Provider(target), yaml.Parser())
			// handler.Load(file.Provider(target), toml.Parser())
			// handler.Load(file.Provider(target), json.Parser())
		}).
		AddRetriever(func(key string) (any, bool) {
			if handler.Exists(key) {
				return handler.Get(key), true
			}
			return nil, false
		}).
		Load()
	return repository
}

Available Methods

AddKeysRetriever AddLoader AddRetriever
Get Items Keys
Load Loaders Retrievers

Method Listing

AddKeysRetriever

The AddKeysRetriever method registers a callback for retrieving all configuration keys:

repository.AddKeysRetriever(func() []string {
	return viper.AllKeys()
})

AddLoader

The AddLoader method adds a new configuration loader and returns the Repository instance:

repository.AddLoader(func(_ *config.Repository) {
	viper.AddConfigPath("./config")
	viper.SetConfigFile("./config/app.yaml")
	viper.MergeInConfig()
	viper.WatchConfig()
})

repository.AddLoader(func(repo *config.Repository) {
	repo.Items().
		Put("app.name", "recipes").
		Put("app.url", "https://recipes.io")
})

AddRetriever

The AddRetriever method adds a new configuration retriever and returns the Repository instance:

repository.AddRetriever(func(key string) (any, bool) {
	if viper.IsSet(key) {
		return viper.GetString(key), true
	}
	return nil, false
})

Get

The Get method retrieves the specified configuration as a Fluent value. An optional second value can be provided as fallback in case of a missing key:

value := repository.Get("server.ports.http")

fmt.Printf("%v\n", value.ToInt()) // 8080

fmt.Printf("%v\n", value.ToString()) // "8080"

Items

The Items method returns the underlying Bag(map) of loaded configurations:

bag := repository.Items()

fmt.Printf("%v\n", bag.IsEmpty()) // false

Keys

The Keys method retrieves all loaded configuration keys:

value := repository.Keys()

fmt.Printf("%v\n", value) // ["http_port", "jdbc_port"]

Load

The Load method reads configurations from provided sources like files and databases:

value := repository.Load().Get("app.name", "recipes").String()

fmt.Printf("%v\n", value) // "recipes"

Loaders

The Loaders method optionally adds a new configuration loader and returns the list of registered loaders:

callbacks := repository.Loaders()

Retrievers

The Retrievers method optionally adds a new configuration retriever and returns the list of registered retrievers:

callbacks := repository.Retrievers()

Unit Tests Matrix

This package has been tested on the following platforms.

golang ubuntu macos windows
1.18.x
1.20.x
1.22.x
Go logo