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

Period in map key name breaks when unmarshalling to struct #348

Open
dustin-decker opened this issue May 17, 2017 · 3 comments
Open

Period in map key name breaks when unmarshalling to struct #348

dustin-decker opened this issue May 17, 2017 · 3 comments

Comments

@dustin-decker
Copy link

I'm unmarshalling yaml to a struct.
I've encountered issues unmarshalling if a map's key containers period character.

Struct that I unmarshall to:

type Config struct {
	ListenInterface string
	ListenPort      int
	Thing           map[string]SomeOtherThing
}

type SomeOtherThing struct {
	Stuff string
}

YAML:

ListenInterface: 127.0.0.1
ListenPort: 8080
Thing:
  .broken:
    Stuff: missing
  partially.works:
    Stuff: missing
  fully-works:
    Stuff: works

Output looks like:

(main.Config) {
 ListenInterface: (string) (len=9) "127.0.0.1",
 ListenPort: (int) 8080,
 Thing: (map[string]main.SomeOtherThing) (len=3) {
  (string) "": (main.SomeOtherThing) {
   Stuff: (string) ""
  },
  (string) (len=9) "partially": (main.SomeOtherThing) {
   Stuff: (string) ""
  },
  (string) (len=11) "fully-works": (main.SomeOtherThing) {
   Stuff: (string) (len=5) "works"
  }
 }
}

I put a demo here:
https://github.com/sevoma/testViper

@dustin-decker dustin-decker changed the title Period in map key name not breaks when unmarshalling to struct Period in map key name breaks when unmarshalling to struct May 17, 2017
@dustin-decker
Copy link
Author

I added a go-yaml output example (which works correctly), but only after lower-casing the YAML keys. The Viper output remains the same after lower-casing the YAML keys.

@johnabass
Copy link

This also happens with JSON maps as well, e.g.

{ "events": {"foo.bar": "moo"}}

When unmarshalled to a struct, the error ...source data must be an array or slice, got map is returned.

@dustin-decker
Copy link
Author

dupe of #324

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