Code Monkey home page Code Monkey logo

configuration's Introduction

HOCON (Human-Optimized Config Object Notation)

GoDoc HOCON Docs.

Currently, some features are not implemented, the API might be a little changed in the future.

example.go

package main

import (
  "fmt"
  "github.com/go-akka/configuration"
)

var configText = `
####################################
# Typesafe HOCON                   #
####################################

config {
  # Comment
  version = "0.0.1"
  one-second = 1s
  one-day = 1day
  array = ["one", "two", "three"] #comment
  bar = "bar"
  foo = foo.${config.bar} 
  number = 1
  object {
    a = "a"
    b = "b"
    c = {
            d = ${config.object.a} //comment
        }
    }
}
// fallback
config.object.a="newA"
config.object.c.f="valueF"

// self reference
self-ref=1
self-ref=[${self-ref}][2]

// byte size
byte-size=10MiB

// system envs
home:${HOME}

plus-equal=foo
plus-equal+=bar

plus-equal-array=[foo]
plus-equal-array+=[bar, ${HOME}]
`

func main() {
  conf := configuration.ParseString(configText)

  fmt.Println("config.one-second:", conf.GetTimeDuration("config.one-second"))
  fmt.Println("config.one-day:", conf.GetTimeDuration("config.one-day"))
  fmt.Println("config.array:", conf.GetStringList("config.array"))
  fmt.Println("config.bar:", conf.GetString("config.bar"))
  fmt.Println("config.foo:", conf.GetString("config.foo"))
  fmt.Println("config.number:", conf.GetInt64("config.number"))
  fmt.Println("config.object.a:", conf.GetString("config.object.a"))
  fmt.Println("config.object.c.d:", conf.GetString("config.object.c.d"))
  fmt.Println("config.object.c.f:", conf.GetString("config.object.c.f"))
  fmt.Println("self-ref:", conf.GetInt64List("self-ref"))
  fmt.Println("byte-size:", conf.GetByteSize("byte-size"))
  fmt.Println("home:", conf.GetString("home"))
  fmt.Println("default:", conf.GetString("none", "default-value"))
  fmt.Println("plus-equal:", conf.GetString("plus-equal"))
  fmt.Println("plus-equal-array:", conf.GetStringList("plus-equal-array"))
}
> go run example.go
config.one-second: 1s
config.one-day: 24h0m0s
config.array: [one two three]
config.bar: bar
config.foo: foo.bar
config.number: 1
config.object.a: newA
config.object.c.d: a
config.object.c.f: valueF
self-ref: [1 2]
byte-size: 10485760
home: /Users/zeal
default: default-value
plus-equal: foobar
plus-equal-array: [foo bar /Users/zeal]

configuration's People

Contributors

artemkaxboy avatar bsideup avatar btnguyen2k avatar dlintw avatar xujinzheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

configuration's Issues

Missing value when optional substitution is unresolved

Expected

When trying to overwrite a variable with an optional substitution, if the substitution cannot be resolved the last value of the variable should be kept.

Got

The variable's value is overwritten by the empty string.

Example

With the DATABASE_URL environment variable not being set:

package main

import (
	"fmt"

	"github.com/go-akka/configuration"
)

var cfg = `
url = "root:@tcp(127.0.0.1:3306)/table?parseTime=True&loc=Local"
url = ${?DATABASE_URL}
`

func main() {
	conf := configuration.ParseString(cfg)
	fmt.Println("url =", conf.GetString("url"))
	// Output: url = 
	// Expected: url = root:@tcp(127.0.0.1:3306)/table?parseTime=True&loc=Local
}

I tried to inspect the code and I think the problem appears because GetOrCreateKey() (https://github.com/go-akka/configuration/blob/master/hocon/parser.go#L90) moves the values into the oldValue variable which is never used when handling optional substitution.

I would be happy to start a PR if someone could point me in the right direction :)

Object concatenation seems to be recursive

Hi!

Trying your library. Looks like there is a bug with object concatenation

I'm getting:

fatal error: stack overflow

runtime stack:
runtime.throw(0x4e3ece, 0xe)
	/usr/local/go/src/runtime/panic.go:566 +0x95
runtime.newstack()
	/usr/local/go/src/runtime/stack.go:1061 +0x416
runtime.morestack()
	/usr/local/go/src/runtime/asm_amd64.s:366 +0x7f

goroutine 1 [running]:
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/substitution.go:12 fp=0xc44008c2c0 sp=0xc44008c2b8
github.com/go-akka/configuration/hocon.(*HoconValue).IsString(0xc42000e340, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/value.go:45 +0x5b fp=0xc44008c300 sp=0xc44008c2c0
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/substitution.go:13 +0x2f fp=0xc44008c320 sp=0xc44008c300
github.com/go-akka/configuration/hocon.(*HoconValue).IsString(0xc42000e340, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/value.go:45 +0x5b fp=0xc44008c360 sp=0xc44008c320
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/substitution.go:13 +0x2f fp=0xc44008c380 sp=0xc44008c360
github.com/go-akka/configuration/hocon.(*HoconValue).IsString(0xc42000e340, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/value.go:45 +0x5b fp=0xc44008c3c0 sp=0xc44008c380
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/substitution.go:13 +0x2f fp=0xc44008c3e0 sp=0xc44008c3c0
github.com/go-akka/configuration/hocon.(*HoconValue).IsString(0xc42000e340, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/value.go:45 +0x5b fp=0xc44008c420 sp=0xc44008c3e0
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/substitution.go:13 +0x2f fp=0xc44008c440 sp=0xc44008c420
github.com/go-akka/configuration/hocon.(*HoconValue).IsString(0xc42000e340, 0x0)
	/go/src/github.com/go-akka/configuration/hocon/value.go:45 +0x5b fp=0xc44008c480 sp=0xc44008c440
github.com/go-akka/configuration/hocon.(*HoconSubstitution).IsString(0xc42000e4e0, 0x0)

with:

package main

import (
    "fmt"
    "github.com/go-akka/configuration"
)

var configText = `
test: yo, "test2":123, foo: [ 1 ], foo: ${foo} [ 2 ]
`

func main() {
    conf := configuration.ParseString(configText)

    fmt.Println("result:", conf.String())
}

HoconValue.isString

HoconValue.isString not just match string

key:[1,ab]

on key 1 is also treat as string,should it be know as NONE string?

Is WithFallback() working in opposite way of java version?

What I guess how does WithFallback() method work... based experiences of Java version
is like this...

  refConf := configuration.Parse("reference.conf")
  myConf := configuration.Parse("my.conf")

  conf := myConf.WithFallback(refConf)

But actually it had to be applied in opposite way to achieve correct result

  conf := refConf.WithFallback(myConf)

Is this intended design?

incorrect lookup for a."b.c"

Looks like the code tries to take into account of quotes in path: https://github.com/go-akka/configuration/blob/master/config.go#L326

func splitDottedPathHonouringQuotes(path string) []string {
	tmp1 := strings.Split(path, "\"")
	var values []string
	for i := 0; i < len(tmp1); i++ {
		tmp2 := strings.Split(tmp1[i], ".")
		for j := 0; j < len(tmp2); j++ {
			if len(tmp2[j]) > 0 {
				values = append(values, tmp2[j])
			}
		}
	}
	return values
}

but it doesn't seem to work as intended. Attempts to look up a path containing quoted nodes doesn't work:

val := config.GetString("a.\"b.c\"")
// val is ""

instead, I have to do the following:

a := config.GetNode("a")
bc := a.GetChildObject("b.c")
val := bc.GetString()
// val has the correct value

The correct behavior is documented in https://github.com/lightbend/config/blob/master/HOCON.md#path-expressions

toMap method

Hi!

Would be cool to support toMap() map[string]interface{} method to simplify JSON-to-HOCON code replacement in the apps.

i.e. I have an app where I use JSON as a configuration format, now I want to use HOCON. My old code was converting JSON string to map[string]interface{} and expected it as the result, so I want to replace only the parsing part.

Thanks!

GetInt32, GetBool panic

if I use in my configuration file * .hocon in a field, for example, a port (this is an int type) and try to get this value using getInt32, I get a panic because in this field I typed this:

test {
    port = 1231dsf
}

It is necessary in this case to return the error interface, because in this case I will have a default value.

is inheritance as specifieed in the akka lightbend spec supported ?

I was expecting test.g3.o1.order to return 1. Mind you the code does not raise an error now but it also does not return a correct value. As such i was not sure if this is supported or no ?

test.g1={
	o1 {
		order    = 1
	}
}

test.g2 = {
	o2 {
		order    = 2
	}
}

test.g3 = ${test.g1}
test.g3 = {
	o2 {
		order    = 2
	}
}

//or another more eaiser syntax way
test.g3 = ${test.g1} {
	o2 {
		order    = 2
	}
}

func TestInheritance(t *testing.T) {
	conf := LoadConfig("tests/t3.conf")
	fmt.Println("test.g3:", conf.GetInt64("test.g3.o1.order"))
}

Thanks !

GetBool panics with correct values

According to API Recommendations booleans may have 6 valid values ("true", "false", "on", "off", "yes", "no"), but they are not fully supported in the library. It supports first four and in lower case only. I suggest to support all of them case insensitively.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.