config

package
v0.0.0-...-d36796e Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package config provides a way to load config data from a config file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any](query string) *T

New creates a new instance of T which is initialized with the config data matching the query.

The query is a query string that can be used to find the config data in depth. The query syntax can be found in `Data.Query` in the package github.com/go-shana/core/data.

Note that the returned value is fully initialized after the config file is loaded. Don't use it before that.

Suppose we have a YAML config file like following:

my:
  config:
    name: Shana
    age: 16

Here is a sample to load a struct from the config:

type MyConfig struct {
    Name string
    Age  int
    Role string // Role is absent in config. We'll initialize it later.
}

// myConfig is a global variable.
var myConfig = config.New[MyConfig]("my.config")

We can provide optional initialization and validation functions for the config.

// Validate validates the config automatically.
func (c *MyConfig) Validate(ctx context.Context) {
    errors.Assert(c.Name != "", c.Age > 0)
}

// Init initializes the config automatically.
func (c *MyConfig) Init(ctx context.Context) {
    c.Role = "Warrior"
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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