fatih / structs

Utilities for Go structs

Home Page:http://godoc.org/github.com/fatih/structs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Values that are structs lose their tags

nikolawannabe opened this issue · comments

I'm using this library to get the keys for a struct, and then marshalling to json for values that are objects. It appears though that when the value is mapped, it loses its tags:

type ApiArg struct {
	Title         string       `json:"title"`
        RightsHolder RightsHolder `json:"rights_holder"`
}

type RightsHolder struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Email      string `json:"email"`
}

apiArg := ApiArg{Title: "blah", RightsHolder: RightsHolder{FirstName: "John", LastName: "Doe", Email: "blah@blah.com"}}
apiArgMap := structs.Map(apiArg)
for key, val := range apiArgMap {
  valBytes, _ := json.Marshal(val)
  valString := string(valBytes)
}

FirstName, LastName, Email marshal as FirstName, etc, instead of first_name, last_name, email.

Changing the struct field names to lowercase makes it so they don't end up showing up in the map though.

If anyone comes across this, you can use multiple tags on a struct.
https://stackoverflow.com/questions/18635671/how-to-define-multiple-name-tags-in-a-struct

Hi,

Unfortunately, I'm archiving this project and will no longer maintaining it. For more information, please read my blog post: Taking an indefinite sabbatical from my projects.

Thanks for the feedback and contribution.