ljconf

package module
v0.0.0-...-31f5766 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2017 License: BSD-2-Clause-Views Imports: 15 Imported by: 17

README

go-ljson-conf

A powerful configuration package for go using Loose JSON as the format. (godoc)

Features

Data format

The data format is the Loose JSON, which is full compatible with JSON format but more easier for writing by hand. Visit Loose JSON project for more details.

Lines starting with // or ; (excluding the leading spaces) are considered as comments and thus ignored.

Dot-separated hierarchical key

For configure:

{
	// http settings
	http: {
		addr: "www.example.com"
		ports: [80, 8080]
	}
}

You can fetch values by different ways:

code value type
String("http.addr") "www.example.com" string
IntList("http.ports") [80, 8080] []int
Int("http.ports[1]") 8080 int
Object("http") map[addr:www...] map[string]interface{}

Include function

main.conf

{
	http: {
		#include#: "addr.conf"
	}
}

addr.conf

{
	addr: "www.example.com"
	ports: [80, 8080]
}

we got:

{
	http: {
		addr: "www.example.com"
		ports: [80, 8080]
	}
}

LICENSE

BSD license.

Documentation

Overview

A configuration package using Loose JSON (https://github.com/daviddengcn/ljson) as the format.

Main features include:

1) Loose JSON as the format

2) Commenting

3) Dot-separated key

4) Include

A typical conf file:

{
	// http settings
	http: {
		addr: "www.example.com"
		ports: [80, 8080]
	}
	#include#: "others.conf"
}

Visit the project page for more details: https://github.com/daviddengcn/go-ljson-conf

Index

Constants

View Source
const INCLUDE_KEY_TAG = "#include#"

Variables

This section is empty.

Functions

func WithZkSvr

func WithZkSvr(svr string) option

WithZkSvr accepts svr as comma separated zookeeper address and use zookeeper as underlying configuration repo storage.

Types

type Conf

type Conf struct {
	// contains filtered or unexported fields
}

func Load

func Load(path string, options ...option) (conf *Conf, err error)

func (*Conf) Bool

func (c *Conf) Bool(key string, def bool) bool

Bool retrieves a value as a bool of the key. def is returned if the value does not exist or is not a bool. A string will be converted using strconv.ParseBool.

func (*Conf) ConfPath

func (c *Conf) ConfPath() villa.Path

func (*Conf) Content

func (c *Conf) Content() map[string]interface{}

func (*Conf) Decode

func (c *Conf) Decode(key string, val interface{}) error

Decode section to struct object val

func (*Conf) Duration

func (c *Conf) Duration(key string, def time.Duration) time.Duration

Duration retrieves a value as a time.Duration. See comments of time.ParseDuration for supported formats.

func (*Conf) Float

func (c *Conf) Float(key string, def float64) float64

Float retrieves a value as a float64 of the key. def is returned if the value does not exist or is not a number. A string will be converted using strconv.ParseFloat.

func (*Conf) Int

func (c *Conf) Int(key string, def int) int

Int retrieves a value as a string of the key. def is returned if the value does not exist or is not a number. A float number will be round up to the closest interger. A string will be converted using strconv.ParseInt.

func (*Conf) IntList

func (c *Conf) IntList(key string, def []int) []int

IntList retrieves a value as a slice of int of the key. def is returned if the value does not exist or is not an array. Elements of the array are converted to int. Zero is used when converting failed.

func (*Conf) Interface

func (c *Conf) Interface(key string, def interface{}) interface{}

Interface retrieves a value as an interface{} of the key. def is returned if the value does not exist.

func (*Conf) List

func (c *Conf) List(key string, def []interface{}) []interface{}

List retrieves a value as a slice of interface{} of the key. def is returned if the value does not exist or is not an array.

func (*Conf) Object

func (c *Conf) Object(key string, def map[string]interface{}) map[string]interface{}

Object retrieves a value as a map[string]interface{} of the key. def is returned if the value does not exist or is not an object.

func (*Conf) Path

func (c *Conf) Path(key string, def villa.Path) villa.Path

func (*Conf) Section

func (c *Conf) Section(key string) (conf *Conf, err error)

func (*Conf) String

func (c *Conf) String(key, def string) string

String retrieves a value as a string of the key. def is returned if the value does not exist or cannot be converted to a string(e.g. is an object).

func (*Conf) StringList

func (c *Conf) StringList(key string, def []string) []string

StringList retrieves a value as a slice of string of the key. def is returned if the value does not exist or is not an array. Elements of the array are converted to strings using fmt.Sprint.

func (*Conf) Time

func (c *Conf) Time(key, layout string, def time.Time) time.Time

Duration retrieves a value as a time.Time. See comments of time.Parse for layout definition.

func (*Conf) Watch

func (c *Conf) Watch(interval time.Duration, stopper <-chan struct{}, ch chan *Conf) error

Watch periodically checks the configure file in curConf with the specified interval. If the configuration file changes, it's reloaded and sent to the specified channel as a *Conf.

Jump to

Keyboard shortcuts

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