Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HJSON seems to not be implemented #131

Closed
pacoorozco opened this issue Jul 19, 2019 · 3 comments · Fixed by #138
Closed

HJSON seems to not be implemented #131

pacoorozco opened this issue Jul 19, 2019 · 3 comments · Fixed by #138

Comments

@pacoorozco
Copy link

Hi,

First of all, thanks for this fantastic package. I wanted to use it with a HJSON configuration file. Reading your documentation:

Package ucfg provides a common representation for hierarchical configurations.

The common representation provided by the Config type can be used with different configuration file formats like XML, JSON, HSJSON, YAML, or TOML.

I've understood that HJSON was implemented, but I've not seen anything on the code.
I've tested it and It's failing... so I'm a bit lost 😓

Is HSJON supported?

  • If "yes" can you give me an example of how to use it?
  • If "no" can you fix the documentation?

Thanks

@urso
Copy link

urso commented Jul 22, 2019

Hi,

unfortunately only json and yaml are provided by default. But the core package is made to be independent of any format, as long it is structured. One can put in any data-structure and get a config.

See for example the implementation for JSON parsing:

func NewConfig(in []byte, opts ...ucfg.Option) (*ucfg.Config, error) {
	var m interface{}
	if err := json.Unmarshal(in, &m); err != nil {
		return nil, err
	}
	return ucfg.NewFrom(m, opts...)
}

HJSON parsing is available in the hjson-go package. It even uses similar interfaces to the stdlib json package. Just copy the go-ucfg/json package, and replace all occurcences of json with hjson.

@pacoorozco
Copy link
Author

Hi @urso

I'm sorry but I don't know to proceed. I mean... I should use go-ucfg to deal with my configuration:

import (
	"github.com/elastic/go-ucfg"
	hjson "github.com/my_package/go-ucfg-hjson"
)

// Defines struct to read config from
type ExampleConfig struct {
    Counter  int 	`config:"counter" validate:"min=0, max=9"`
}

// Defines default config option
var (
    defaultConfig = ExampleConfig{
		    Counter: 4,
    }
)

func main() {
    appConfig := defaultConfig // copy default config so it's not overwritten
    config, err := hjson.NewConfigWithFile(path, ucfg.PathSep("."))
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    err = config.Unpack(&appConfig)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

Am i right?

@urso
Copy link

urso commented Aug 21, 2019

Yes, this looks about right. I'd make defaultConfig a function though. This guarantees no false sharing in case an object in defaultConfig holds a pointer.

e.g.

func defaltConfig() ExampleConfig {
  return ...
}

go-ucfg has a many options. In the Beats repo go-ucfg is wrapped, so to always apply a common set of options. See: https://github.com/elastic/beats/blob/master/libbeat/common/config.go#L58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants