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

Disable tag support #5

Closed
novln opened this issue Aug 22, 2019 · 3 comments
Closed

Disable tag support #5

novln opened this issue Aug 22, 2019 · 3 comments

Comments

@novln
Copy link

novln commented Aug 22, 2019

Hello,

Is it possible to disable tag support with UnmarshalWithConf so we could unmarshal a konf instance to a struct without having to provides tags?

Reference: https://github.com/knadh/koanf/blob/master/koanf.go#L199

Like adding a new field called DisableTag as a boolean (in order to avoid compatibility break).

Let me know what you think.
Thank you in advance.

@knadh
Copy link
Owner

knadh commented Aug 23, 2019

koanf uses the mapstructure package to do the unmarshalling. Looks like mapstructure implicitly behaves the way you described. That is, if there's no tag on a struct field, it unmarshals it any way. I'll investigate more and document this behaviour.

package main

import (
	"fmt"
	"log"

	"github.com/knadh/koanf"
	"github.com/knadh/koanf/parsers/json"
	"github.com/knadh/koanf/providers/file"
)

func main() {
	var k = koanf.New(".")

	// struct with no 'koanf' tags.
	type test struct {
		Type string
	}

	// Load JSON config.
	if err := k.Load(file.Provider("mock/mock.json"), json.Parser()); err != nil {
		log.Fatalf("error loading config: %v", err)
	}

	var out test
	k.Unmarshal("", &out)

	fmt.Println(out)
}

@novln
Copy link
Author

novln commented Aug 23, 2019

Oh, okay. It's kind of misleading 😕
Thank you for the explanation, and yeah, some comments on this behavior would be great.

@rhnvrm
Copy link
Collaborator

rhnvrm commented Dec 13, 2019

Added a wiki entry to document this: https://github.com/knadh/koanf/wiki#mapstructure

@rhnvrm rhnvrm closed this as completed Dec 13, 2019
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

3 participants