goconf

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2018 License: MIT Imports: 9 Imported by: 1

README

GJSON
Build Status codecov Go Report Card
GoDoc

Gjson-based configuration file for golang

GoConf is a Go package that quickly retrieves JSON configuration files based on GJSON

Getting Started

Installing

To start using GoConf, install Go and run go get:

$ go get -u github.com/fifsky/goconf

This will retrieve the library.

Load multiple configs

You only need to declare the Tag conf:"filename" on the struct

type log struct {
    LogName string `json:"log_name"`
    LogPath string `json:"log_path"`
}

type db struct {
    Name string `json:"name"`
    Host string `json:"host"`
    Port string `json:"port"`
}

type config struct {
    Log log `conf:"log"`
    DB  db  `conf:"db"`
}

conf, err := NewConfig("./testdata/")
if err != nil {
    fmt.Fatal(err)
}

app := &config{}
err = conf.Load(app)

fmt.Printf("%#v",app)

Get config a value

Config file: testdata/dev.json

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44},
    {"first": "Roger", "last": "Craig", "age": 68},
    {"first": "Jane", "last": "Murphy", "age": 47}
  ]
}
package main

import "github.com/fifsky/goconf"

func main() {
    conf, _ := goconf.NewConfig("./testdata/")
    ret, _ := conf.Get("dev.name.last")

    println(ret.String())
}

This will print:

Anderson

You can also use the Must function to simplify this

package main

import "github.com/fifsky/goconf"

func main() {
    conf, _ := goconf.NewConfig("./testdata/")
    println(conf.MustGet("dev.name.last").String())
}

first key dev is filename

Path Syntax

For more Path Syntax, see the GJSON document

Default value

If you use the Must function to access a non-existent configuration file or keys, GoConf returns zero value, which is consistent with gjson.result

conf.MustGet("dev.notfound").String() //value is empty string
conf.MustGet("dev2.not").Int() //value is 0 int

Unmarshal to Struct

type Database struct {
	Driver string `json:"driver"`
	Host   string `json:"host"`
	Port   int    `json:"port"`
}

type ConfigDemo struct {
	Database Database `json:"database"`
}

conf, _ := goconf.NewConfig("./testdata/")
app := &ConfigDemo{}
err = conf.Unmarshal("json5", app)
if err != nil {
    fmt.Println(err)
}
fmt.Println(app)
//&{{mysql localhost 3306}}

Unmarshal also supports Xpath

database := &Database{}
conf.Unmarshl("json5.database",database)

Contact

Xudong Cai @fifsky

License

GoConf source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Path string
	Ext  string
	Tag  string
	// contains filtered or unexported fields
}

config file path and ext,Ext default .json

func NewConfig

func NewConfig(path string) (*Config, error)

func (*Config) Get

func (c *Config) Get(key string) (*gjson.Result, error)

if key does not exist, return error key: dev.user.name dev is filename in config path

func (*Config) Load

func (c *Config) Load(v interface{}) error

func (*Config) MustGet

func (c *Config) MustGet(key string) *gjson.Result

Ignore the error and return a zero value when it does not exist

func (*Config) Unmarshal

func (c *Config) Unmarshal(keys string, v interface{}) error

Unmarshal is json5 unmarshal to struct and support xpath

Jump to

Keyboard shortcuts

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