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

Add support of HJSON #5

Closed
chmike opened this issue Oct 22, 2017 · 5 comments
Closed

Add support of HJSON #5

chmike opened this issue Oct 22, 2017 · 5 comments

Comments

@chmike
Copy link

chmike commented Oct 22, 2017

Could it be possible to add support of hjson by mean of the HJSON-go package ?

hjson is human friendly json. This means a json encoding more convenient for humans to read and write. The best way to use it is to convert the hjson string into json, and than use the json decoding.

An alternate solution would be to support a string as config input instead of a file. This would allow users to any type of source for the config. It could be extracted from a database for instance.

@stevenroose
Copy link
Owner

stevenroose commented Oct 22, 2017 via email

@stevenroose
Copy link
Owner

Look at this commit: c814583

I think it's ok for you to specify a custom decoder for HJSON. I don't think HJSON is common enough to be worth it maintaining it in the package.

You can create a function like here: c814583#diff-3b7b9b0c1df08c66e0e7ffb8929051d1R23

@chmike
Copy link
Author

chmike commented Oct 23, 2017

The DecoderTryAll is a brute force solution.
Define an interface for ConfigDecoder functions and use the encoding type string as key.
In the Init() function we may register the decoding type and function.

@stevenroose
Copy link
Owner

Well, TryAll is only used when no decoder is provided in Conf here: c814583#diff-b67b03abee47ec66b6b9d0e2302a2576R35

So yes it's brute force last resort. I'm quite impartial for removing it and panicing if no decoder is provided in the Conf struct.

@stevenroose
Copy link
Owner

I believe this is solved.

gonfig supports custom config file decoders. F.e. for hjson, one could make one like this:

// DecoderHJSON is an HJSON decoding function for config files.
var DecoderHJSON = func(c []byte) (map[string]interface{}, error) {
	var m map[string]interface{}
	if err := hjson.Unmarshal(c, &m); err != nil {
		return nil, fmt.Errorf("error parsing HJSON config file: %s", err)
	}

	return m, nil
}

And pass it to Load as follows:

gonfig.Load(&config, gonfig.Conf{
    FileDecoder: DecoderHJSON,
}

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

No branches or pull requests

2 participants