ljconf

package module
v0.0.0-...-92689ea Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2015 License: BSD-2-Clause-Views Imports: 13 Imported by: 14

README

go-ljson-conf GoSearch

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-seperated 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]
	}
}

Multi-path searching We calling Load function, it searches the following path in order:

  1. For absolute path, it is directly used,
  2. Current directory,
  3. Directory of the executable, and
  4. User's home directory

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-seperated 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 Watch

func Watch(curConf *Conf, interval time.Duration, 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. TODO use inotify mechanism instead of poll.

Types

type Conf

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

func Load

func Load(fn string) (conf *Conf, err error)

Load reads configurations from a speicified file. If some error found during reading, it will be return, but the conf is still available. If the given path is not absolute, Load tries to find the configure file in the following order: 1. Current directory 2. Same folder the executable, and 3. User's home folder.

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) 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.

Jump to

Keyboard shortcuts

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