A configure parser library for go.
Conf can work with many conf syntax. Developer can write SyntaxHandler for their own configure syntax. This library provided a SyntaxHandler for Hdf configure format, SyntaxHandlerHdf.
func Read(h SyntaxHandler, rd io.Reader) (cf *Conf, err error) // read from reader with SyntaxHandler h
func Parse(h SyntaxHandler, b []byte) (cf *Conf, err error) // parse from []byte with SyntaxHandler h
func Load(h SyntaxHandler, filename string) (cf *Conf, err error) // load a single file with SyntaxHandler h
func LoadDir(h SyntaxHandler, confRoot string) (cf *Conf, err error) // load whole directory with SyntaxHandler h
No Syntax defined in Conf, all things about configure file syntax are defined in SyntaxHandler. Data structure must math the operation of Conf.
- The root of data can only be a
map[string]interface{}. It is similar to json, but root cannot be an array. - A map cannot contain an array nor a map directly. Each map or array must have a key name.
- In an array, data type must be the same.
- Values can only be string, map or array(slice). Key can only be string.
{
"name": "test",
"location": "china"
}
If we want the value of name.
s := cf.Find("name").String()
s would be a string test.
{
"server": {
"name": "localhost",
"listen": ":80"
},
"log": "/var/log/hello.log"
}
If we want the value of name.
s := cf.Find("server.name").String()
or
s := cf.Find("server", "name").String()
s would be the string localhost.
{
"rewrite": [
{
"from": "/",
"to": "/index.php"
},
{
"from": "/s",
"to": "/search.php"
}
]
}
If we want to access each rewrite rule.
c := cf.Find("rewrite")
for _, i := range c.Array() {
fr := i.Find("from").String()
/* other processing code */
}
A SyntaxHandler must implement the following interface.
type SyntaxHandler interface {
Read(rd io.Reader) (interface{}, error)
Load(filename string) (interface{}, error)
Parse(b []byte) (interface{}, error)
LoadDir(dirname string) (interface{}, error)
Write(wr io.Writer, data interface{}) (int, error)
Dump(data interface{}) ([]byte, error)
}
The first time I know hdf is when I do some work on HHVM. They does not recommand using this configure file format now, but I like it.
key = value
The value contains all things after the equal mark, include quotation-marks, comment marks.
name = "Jim" # person's name
In this case, the value is string "Jim" # person's name, not just Jim.
A block is between a pair of {}.
log {
file_path: /var/log/hello.log
auto_rotate: false
}
In json format is:
{
"log": {
"file_path": "/var/log/hello.log",
"auto_rotate": "false"
}
}
In hhvm, { can be in indivadual line, but it looks very strange that the key-value holds the whole line but block can be in two line. So in the default SyntaxHandlerHdf, this is not allowed. For the same reason, to close a block, } must be in a individual line.
So this lib may not parse all hdf file from hhvm.
No mark for array defined in hdf. If a key appears twice or more, it would be an array.
server {
port = 80
}
In json:
{
"server": {
"port": "80"
}
}
and if server appears twice:
server {
port = 80
}
server {
port = 443
}
in json:
{
"server": [
{
"port": "80"
},
{
"port": "443"
}
]
}