config

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 8 Imported by: 4

README

go-pkg/config

Go Report Card GoDoc OSCS Status

go-pkg/config is an componentized config plugin.

It provides an easy way to load configuration files.

Install

go get github.com/wwwangxc/go-pkg/config

Quick Start

main.go

package main

import (
        "github.com/wwwangxc/go-pkg/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/go-pkg/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
}

Documentation

Overview

Package go-pkg/config is a componentized config plugin.

It provides an easy way to load configuration files.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnmarshalerNotExist unmarshaler not exist
	ErrUnmarshalerNotExist = fmt.Errorf("%s: unmarshaler not exist", packageName)

	// ErrConfigNotExist config not exist
	ErrConfigNotExist = fmt.Errorf("%s: config not exist", packageName)
)

Functions

This section is empty.

Types

type Configure

type Configure interface {

	// Unmarshal unmarshal config raw data
	Unmarshal(interface{}) error

	// IsExist check the key exist
	IsExist(string) bool

	// Get get value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	Get(string, interface{}) interface{}

	// GetString get string value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetString(string, string) string

	// GetBool get bool value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetBool(string, bool) bool

	// GetInt get int value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetInt(string, int) int

	// GetInt32 get int32 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetInt32(string, int32) int32

	// GetInt64 get int64 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetInt64(string, int64) int64

	// GetUint get uint value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetUint(string, uint) uint

	// GetUint32 get uint32 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetUint32(string, uint32) uint32

	// GetUint64 get uint64 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetUint64(string, uint64) uint64

	// GetFloat32 get float32 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetFloat32(string, float32) float32

	// GetFloat64 get float64 value by key
	//
	// return defaultVal, when key not exist
	// k support key1.key2.key3
	GetFloat64(string, float64) float64
}

Configure ...

func Load

func Load(path string, opts ...LoadOption) (Configure, error)

Load load config

type LoadOption

type LoadOption func(*configureImpl)

LoadOption load option for config

func WithUnmarshaler

func WithUnmarshaler(name string) LoadOption

WithUnmarshaler assign unmarshaler

func WithWatchCallback

func WithWatchCallback(callback func(Configure)) LoadOption

WithWatchCallback with watch callback

Directories

Path Synopsis
example
Package mockconfig is a generated GoMock package.
Package mockconfig is a generated GoMock package.

Jump to

Keyboard shortcuts

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