Skip to content

codemodus/config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages