Categorygithub.com/wwwangxc/gopkg/config
modulepackage
0.1.1
Repository: https://github.com/wwwangxc/gopkg.git
Documentation: pkg.go.dev

# README

gopkg/config

Go Report Card GoDoc OSCS Status

gopkg/config is an componentized config plugin.

It provides an easy way to load configuration files.

Install

go get github.com/wwwangxc/gopkg/config

Quick Start

main.go

package main

import (
        "github.com/wwwangxc/gopkg/config"
)

func main() {
        // load local config file.
        // config.WithUnmarshaler("yaml"): serialize with yaml.
        // config.WithWatchCallback(watch): watch config file, callback watch funcation when config file changed.
        configure, err := config.Load("./config.yaml", config.WithUnmarshaler("yaml"), config.WithWatchCallback(watch))

        // the default unmarshaler is yaml
        configure, err = config.Load("./config.yaml", config.WithWatchCallback(watch))

        // serialize config file with toml
        configure, err = config.Load("./config.toml", config.WithUnmarshaler("toml"))

        // serialize config file with json
        configure, err = config.Load("./config.json", config.WithUnmarshaler("json"))

        // read string value
        configure.GetString("app.env_name", "default")

        // read bool value
        configure.GetBool("app.debug", false)

        // read uint32 value
        configure.GetUint32("machine_id", 1)

        // unmarshal raw data to Config struct
        c := &Config{}
        err = configure.Unmarshal(c)
}

func watch(configure config.Configure) {
        // do something ...
}

type Config struct {
        MachineID uint32 `yaml:"machine_id" toml:"machine_id" json:"machine_id"`
        APP       struct{
                EnvName string `yaml:"env_name" toml:"env_name" json:"env_name"`
                Debug   bool `yaml:"debug" toml:"debug" json:"debug"`
        } `yaml:"app" toml:"app" json:"app"`
}

config.yaml

machine_id: 1
app:
  env_name: ${ENV}
  debug: true

config.toml

machine_id = 1

[app]
env_name = "${ENV}"
debug = true

config.json

{
  "machine_id": 1,
  "app": {
    "env_name": "${ENV}",
    "debug": true
  }
}

How To Mock

package tests

import (
        "testing"

        "github.com/agiledragon/gomonkey"
        "github.com/golang/mock/gomock"

        "github.com/wwwangxc/gopkg/config/mockconfig"
)

func TestLoad(t *testing.T) {
        ctrl := gomock.NewController(t)
        defer ctrl.Finish()
        
        mockConfigure := mockconfig.NewMockConfigure(ctrl)
        mockConfigure.EXPECT().GetString(gomock.Any(), gomock.Any()).
            Return("mocked value").AnyTimes()
        
        dispatch := gomonkey.ApplyFunc(Load,
       	        func(string, ...LoadOption) (Configure, error) {
       	                return mockConfigure, nil
       	        })
        defer dispatch.Reset()

        // test cases
}

# Packages

No description provided by the author
Package mockconfig is a generated GoMock package.
No description provided by the author

# Functions

Load load config.
WithUnmarshaler assign unmarshaler.
WithWatchCallback with watch callback.

# Variables

ErrConfigNotExist config not exist.
ErrUnmarshalerNotExist unmarshaler not exist.

# Interfaces

Configure ...go:generate mockgen -source=config.go -destination=mockconfig/config_mock.go -package=mockconfig.

# Type aliases

LoadOption load option for config.