README ¶
HOCON (Human-Optimized Config Object Notation)
Configuration library for working with the Lightbend's HOCON format. HOCON is a human-friendly JSON superset
Features of HOCON
- Comments, with
#
or//
- Allow omitting the
{}
around a root object - Allow
=
as a synonym for:
- Allow omitting the
=
or:
before a{
sofoo { a : 42 }
- Allow omitting commas as long as there's a newline
- Allow trailing commas after last element in objects and arrays
- Allow unquoted strings for keys and values
- Unquoted keys can use dot-notation for nested objects,
foo.bar=42
meansfoo { bar : 42 }
- Duplicate keys are allowed; later values override earlier, except for object-valued keys where the two objects are merged recursively
include
feature merges root object in another file into current object, sofoo { include "bar.json" }
merges keys inbar.json
into the objectfoo
- substitutions
foo : ${a.b}
sets keyfoo
to the same value as theb
field in thea
object - substitutions concatenate into unquoted strings,
foo : the quick ${colors.fox} jumped
- substitutions fall back to environment variables if they don't
resolve in the config itself, so
${HOME}
would work as you expect. - substitutions normally cause an error if unresolved, but
there is a syntax
${?a.b}
to permit them to be missing. +=
syntax to append elements to arrays,path += "/bin"
- multi-line strings with triple quotes as in Python or Scala
see the documentation for more details about the HOCON https://github.com/lightbend/config/blob/master/HOCON.md
Installation
go get -u github.com/gurkankaymak/hocon
Usage
package main
import (
"fmt"
"log"
"github.com/gurkankaymak/hocon"
)
func main() {
hoconString := `
booleans {
trueVal: true
trueValAgain: ${booleans.trueVal}
trueWithYes: yes
falseWithNo: no
}
// this is a comment
# this is also a comment
numbers {
intVal: 3
floatVal: 1.0
}
strings {
a: "a"
b: "b"
c: "c"
}
arrays {
empty: []
ofInt: [1, 2, 3]
ofString: [${strings.a}, ${strings.b}, ${strings.c}]
ofDuration: [1 second, 2h, 3 days]
}
durations {
second: 1s
halfSecond: 0.5 second
minutes: 5 minutes
hours: 2hours
day: 1d
}
objects {
valueObject {
mandatoryValue: "mandatoryValue"
arrayValue: ${arrays.ofInt}
nullValue: null
}
}`
conf, err := hocon.ParseResource(hoconString)
if err != nil {
log.Fatal("error while parsing configuration: ", err)
}
objectValue := conf.GetObject("objects.valueObject")
arrayValue := conf.GetArray("arrays.ofInt")
stringValue := conf.GetString("strings.a")
intValue := conf.GetInt("numbers.intVal")
floatValue := conf.GetFloat64("numbers.floatVal")
durationValue := conf.GetDuration("durations.second")
fmt.Println("objectValue:", objectValue) // {mandatoryValue:mandatoryValue, arrayValue:[1,2,3], nullValue:null}
fmt.Println("arrayValue:", arrayValue) // [1,2,3]
fmt.Println("stringValue:", stringValue) // a
fmt.Println("intValue:", intValue) // 3
fmt.Println("floatValue:", floatValue) // 1.0
fmt.Println("durationValue:", durationValue) // 1s
fmt.Println("all configuration:", conf)
}
Documentation ¶
Index ¶
- type Array
- type Boolean
- type Config
- func (c *Config) Get(path string) Value
- func (c *Config) GetArray(path string) Array
- func (c *Config) GetBoolean(path string) bool
- func (c *Config) GetDuration(path string) time.Duration
- func (c *Config) GetFloat32(path string) float32
- func (c *Config) GetFloat64(path string) float64
- func (c *Config) GetInt(path string) int
- func (c *Config) GetIntSlice(path string) []int
- func (c *Config) GetObject(path string) Object
- func (c *Config) GetString(path string) string
- func (c *Config) GetStringMap(path string) map[string]Value
- func (c *Config) GetStringMapString(path string) map[string]string
- func (c *Config) GetStringSlice(path string) []string
- func (c *Config) String() string
- func (c *Config) WithFallback(fallback *Config) *Config
- type Duration
- type Float32
- type Float64
- type Int
- type Null
- type Object
- type ParseError
- type String
- type Substitution
- type Type
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array []Value
Array represents an array node in the configuration tree
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config stores the root of the configuration tree and provides an API to retrieve configuration values with the path expressions
func ParseResource ¶
ParseResource parses the resource at the given path, creates the configuration tree and returns a pointer to the Config, returns the error if any error occurs while parsing
func ParseString ¶
ParseString function parses the given hocon string, creates the configuration tree and returns a pointer to the Config, returns a ParseError if any error occurs while parsing
func (*Config) Get ¶
Get method finds the value at the given path and returns it without casting to any type returns nil if the value is not found
func (*Config) GetArray ¶
GetArray method finds the value at the given path and returns it as an Array, returns nil if the value is not found
func (*Config) GetBoolean ¶
GetBoolean method finds the value at the given path and returns it as a Boolean returns false if the value is not found
func (*Config) GetDuration ¶
GetDuration method finds the value at the given path and returns it as a time.Duration returns 0 if the value is not found
func (*Config) GetFloat32 ¶
GetFloat32 method finds the value at the given path and returns it as a Float32 returns float32(0.0) if the value is not found
func (*Config) GetFloat64 ¶
GetFloat64 method finds the value at the given path and returns it as a Float64 returns 0.0 if the value is not found
func (*Config) GetInt ¶
GetInt method finds the value at the given path and returns it as an Int, returns zero if the value is not found
func (*Config) GetIntSlice ¶ added in v1.1.0
GetIntSlice method finds the value at the given path and returns it as []int, returns nil if the value is not found
func (*Config) GetObject ¶
GetObject method finds the value at the given path and returns it as an Object, returns nil if the value is not found
func (*Config) GetString ¶
GetString method finds the value at the given path and returns it as a String returns empty string if the value is not found
func (*Config) GetStringMap ¶ added in v1.1.0
GetStringMap method finds the value at the given path and returns it as a map[string]Value returns nil if the value is not found
func (*Config) GetStringMapString ¶ added in v1.1.0
GetStringMapString method finds the value at the given path and returns it as a map[string]string returns nil if the value is not found
func (*Config) GetStringSlice ¶ added in v1.1.0
GetStringSlice method finds the value at the given path and returns it as []string returns nil if the value is not found
func (*Config) WithFallback ¶ added in v1.2.0
WithFallback method returns a new *Config (or the current config, if the given fallback doesn't get used) 1. merges the values of the current and fallback *Configs, if the root of both of them are of type Object for the same keys current values overrides the fallback values 2. if any of the *Configs has non-object root then returns the current *Config ignoring the fallback parameter
type Object ¶
Object represents an object node in the configuration tree
type ParseError ¶
type ParseError struct {
// contains filtered or unexported fields
}
ParseError represents an error occurred while parsing a resource or string to a hocon configuration
func (*ParseError) Error ¶
func (p *ParseError) Error() string
type Substitution ¶
type Substitution struct {
// contains filtered or unexported fields
}
Substitution refers to another value in the configuration tree
func (*Substitution) String ¶
func (s *Substitution) String() string
String method returns the string representation of the Substitution