config

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2018 License: MIT Imports: 7 Imported by: 1

README

config

go get "github.com/codemodus/config"

Package config provides an interface and initialization function for handling configuration values stored as JSON. The JSON structure is defined by a user configurable struct which implements Configurator. Nested Configurator instances by calling Init within the parent's InitPost.

Usage

func Init(c Configurator, file string) (err error)
type Config
    func (c *Config) InitPost() error
type Configurator
Setup
import (
    "fmt"

    "github.com/codemodus/config"
)

func main() {
    myConf := &struct {
        *config.Config

        SampleText string
        TestText   string
    }{}

    if err := config.Init(myConf, "test_dir/config.json"); err != nil {
        fmt.Println(err)
    }

    fmt.Println(myConf.SampleText) // "sampled"
    fmt.Println(myConf.TestText)   // "tested"
}
JSON Example
{
  "SampleText": "sampled",
  "TestText": "tested"
}
Nested Configurator Objects
type testConf struct {
    // Not needed because InitPost() is defined.
    // *config.Config
    
    SampleText string
    TestText   string

    *embeddedConf
}

func (tc *testConf) InitPost() error {
    emConf := &embeddedConf{}
    if err := config.Init(emConf, "test_dir/config2.json"); err != nil {
        return err
    }
    tc.embeddedConf = emConf
    return nil
}

type embeddedConf struct {
    *config.Config

    SampleText2 string
    TestText2   string
}

More Info

N/A

Documentation

View the GoDoc

Benchmarks

N/A

Documentation

Overview

Package config provides an interface and initialization function for handling configuration values stored as JSON. The JSON structure is defined by a user configurable struct which implements Configurator. Nest Configurator instances by calling Init within the parent's InitPost.

Example
package main

import (
	"fmt"

	"github.com/codemodus/config"
)

func main() {
	myConf := &struct {
		*config.Config

		SampleText string
		TestText   string
	}{}

	if err := config.Init(myConf, "testdata/config.json"); err != nil {
		fmt.Println(err)
	}

	fmt.Println(myConf.SampleText)
	fmt.Println(myConf.TestText)

}
Output:

sampled
tested

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultConfDir is the configuration directory fallback.
	DefaultConfDir = defaultConfDir()
	// DefaultFilename is the configuration filename fallback.
	DefaultFilename = "config.cnf"
	// DefaultLibDir is the lib directory fallback.
	DefaultLibDir = defaultLibDir()
	// ErrBadData indicates that the relevant data is not valid JSON or TOML.
	ErrBadData = errors.New("provided data is not valid JSON or TOML")
)

Functions

func Init

func Init(c Configurator, file string) (err error)

Init decodes the provided JSON file into the provided Configurator.

Types

type Config

type Config struct{}

Config is provided for embedding default methods needed to satisfy the Configurator interface.

func (*Config) InitPost

func (c *Config) InitPost() error

InitPost runs post config initialization processing.

type Configurator

type Configurator interface {
	// InitPost should contain any post initialization logic to be applied to
	// the Configurator, and return any processing errors.
	InitPost() error
}

Configurator defines the basic functionality required to work with config.

Jump to

Keyboard shortcuts

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