hocon

package module
v1.2.19 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: MIT Imports: 11 Imported by: 9

README

HOCON (Human-Optimized Config Object Notation)

Go Report Card codecov Build Status GoDoc Mentioned in Awesome Go

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 { so foo { 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 means foo { 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, so foo { include "bar.json" } merges keys in bar.json into the object foo
  • substitutions foo : ${a.b} sets key foo to the same value as the b field in the a 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.ParseString(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

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

func (Array) String

func (a Array) String() string

String method returns the string representation of the Array

func (Array) Type

func (a Array) Type() Type

Type Array

type Boolean

type Boolean bool

Boolean represents bool value

func (Boolean) String

func (b Boolean) String() string

func (Boolean) Type

func (b Boolean) Type() Type

Type Boolean

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

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

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

func ParseString(input string) (*Config, error)

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

func (c *Config) Get(path string) Value

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

func (c *Config) GetArray(path string) Array

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

func (c *Config) GetBoolean(path string) bool

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) GetConfig added in v1.2.3

func (c *Config) GetConfig(path string) *Config

GetConfig method finds the value at the given path and returns it as a Config, returns nil if the value is not found

func (*Config) GetDuration

func (c *Config) GetDuration(path string) time.Duration

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

func (c *Config) GetFloat32(path string) float32

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

func (c *Config) GetFloat64(path string) float64

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

func (c *Config) GetInt(path string) int

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

func (c *Config) GetIntSlice(path string) []int

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

func (c *Config) GetObject(path string) Object

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) GetRoot added in v1.2.2

func (c *Config) GetRoot() Value

GetRoot method returns the root value of the configuration

func (*Config) GetString

func (c *Config) GetString(path string) string

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

func (c *Config) GetStringMap(path string) map[string]Value

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

func (c *Config) GetStringMapString(path string) map[string]string

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

func (c *Config) GetStringSlice(path string) []string

GetStringSlice method finds the value at the given path and returns it as []string returns nil if the value is not found

func (*Config) String

func (c *Config) String() string

String method returns the string representation of the Config object

func (*Config) WithFallback added in v1.2.0

func (c *Config) WithFallback(fallback *Config) *Config

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 Duration

type Duration time.Duration

Duration represents a duration value

func (Duration) String

func (d Duration) String() string

func (Duration) Type

func (d Duration) Type() Type

Type Duration

type Float32

type Float32 float32

Float32 represents a Float32 value

func (Float32) String

func (f Float32) String() string

func (Float32) Type

func (f Float32) Type() Type

Type Number

type Float64

type Float64 float64

Float64 represents a Float64 value

func (Float64) String

func (f Float64) String() string

func (Float64) Type

func (f Float64) Type() Type

Type Number

type Int

type Int int

Int represents an Integer value

func (Int) String

func (i Int) String() string

func (Int) Type

func (i Int) Type() Type

Type Number

type Null

type Null string

Null represents a null value

func (Null) String

func (n Null) String() string

func (Null) Type

func (n Null) Type() Type

Type Null

type Object

type Object map[string]Value

Object represents an object node in the configuration tree

func (Object) String

func (o Object) String() string

String method returns the string representation of the Object

func (Object) ToConfig added in v1.2.3

func (o Object) ToConfig() *Config

ToConfig method converts object to *Config

func (Object) Type

func (o Object) Type() Type

Type Object

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 String

type String string

String represents a string value

func (String) String

func (s String) String() string

func (String) Type

func (s String) Type() Type

Type 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

func (*Substitution) Type

func (s *Substitution) Type() Type

Type Substitution

type Type

type Type int

Type of an hocon Value

const (
	ObjectType Type = iota
	StringType
	ArrayType
	NumberType
	BooleanType
	NullType
	SubstitutionType
	ConcatenationType
)

Type constants

type Value

type Value interface {
	Type() Type
	String() string
	// contains filtered or unexported methods
}

Value interface represents a value in the configuration tree, all the value types implements this interface

Jump to

Keyboard shortcuts

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