gurkankaymak / hocon

go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config
MIT License
79 stars 17 forks source link

Error while parsing configuration when dot is used in key or value #47

Open donglinjy opened 11 months ago

donglinjy commented 11 months ago

I will fail to load string configure as below, with error error while parsing configuration: missing comma! at: 4:13, values should have comma or ASCII newline ('\n') between them.

The root cause may be because text scanner will parse 2.2.0 into 2.2 and .0, and will parse c-f1.2g into c-f1, .2, and g.

    hoconString := `
            key1 {
              a = "a"
              b = 2.2.0
              c-f1.2g = "ok"
            }`

    _, err := hocon.ParseString(hoconString)
    if err != nil {
        log.Println("error while parsing configuration: ", err)
    }
gurkankaymak commented 11 months ago

Hi @donglinjy, thanks for reporting the issue. Yeah, the root of the issue is that the Golang text scanner parses as you described ('2.2.0' into '2.2' and '.0'). I'll look for a solution. In the meantime, you can use double quotes


key1 {
    a = "a"
    b = "2.2.0"
    "c-f1.2g" = "ok"
}
donglinjy commented 11 months ago

Hi @donglinjy, thanks for reporting the issue. Yeah, the root of the issue is that the Golang text scanner parses as you described ('2.2.0' into '2.2' and '.0'). I'll look for a solution. In the meantime, you can use double quotes

key1 {
    a = "a"
    b = "2.2.0"
    "c-f1.2g" = "ok"
}

Thanks. Based on what I tried, to work around it, for the issue in value like 2.2.0, we can double quote it. But for key like c-f1.2g, will need to quote each parts split by . as blow, or the config loading will succeed, but it won't allow you to read config like GetString("key1.c-f1.2g").

    a = "a"
    b = "2.2.0"
    "c-f1"."2g" = "ok"
}
gurkankaymak commented 11 months ago

You're right, I didn't realize 'c-f1.2g' is nested, thought of it as a single key. In fact you can also just double quote the last part like c-f1."2g"