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

Omitempty fields #234

Open
guillermo-menjivar opened this issue Jan 25, 2021 · 2 comments
Open

Omitempty fields #234

guillermo-menjivar opened this issue Jan 25, 2021 · 2 comments
Labels

Comments

@guillermo-menjivar
Copy link

guillermo-menjivar commented Jan 25, 2021

Thank you for this project,
I am looking for verification on an assumption I have when it comes to the omitempty flag in mapstructure.

Assumption: I expect any property that has the omitempty flag would be "omitted" from the map that is populated by mapstructure.
I reference this test

// &map[Age:0 FirstName:Somebody]

however, this test has no assertions but rather a comment of what it "should" be without validation. I want to make sure mapstructure "should not" be returning fields that are flagged with omitted. If so I am willing to send a PR, but want to make sure that I am understanding the library correctly.

I ran the following code:

package main

import (
        "fmt"

        "github.com/mitchellh/mapstructure"
)

type Job struct {
        Title string `mapstructure:"title,omitempty"`
}

type Person struct {
        Name string `mapstructure:"name"`
        Age  *int   `mapstructure:"age,omitempty"`
        *Job `mapstructure:",omitempty"`
}

func main() {
        m := &map[string]interface{}{}
        p := Person{Name: "rockets"}
        err := mapstructure.Decode(&p, m)

        if err != nil {
                fmt.Println("received an error while decoding", err)
                return
        }
        fmt.Println(m)
}

// returns: &map[Job:<nil> age:<nil> name:rockets]
@chenjisuan
Copy link

check out your mapstructure version >= 1.3.0 #145

@mitchellh
Copy link
Owner

Yes if you write a test and make a PR, I will merge the fix. This looks like a bug to me.

@mitchellh mitchellh added the bug label Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants