You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi
I would like to decode this kind of structure with pointers and nested structure with pointers
and keeping the field in input as a flattened map and using squash
But got an error :
Family: unsupported type for squash: ptr
I would like to achieve this results
If LastName is filled with value "test" : Family is initialiazed and Lastname equals "test" and json ouput is
{"family":{"lastname":"test"},"city":"San Francisco","firstname":"Mitchell"}
If LastName is nil : Family is nil and json output is
{"city":"San Francisco","firstname":"Mitchell"}
Thanks
package main
import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
)
func main() {
type Family struct {
LastName *string \`json:"lastname,omitempty"\`
}
type Location struct {
City string \`json:"city,omitempty"\`
}
type Person struct {
*Family \`json:"family,omitempty" mapstructure:",squash"\`
Location \`mapstructure:",squash"\`
FirstName string \`json:"firstname,omitempty"\`
}
input := map[string]interface{}{
"FirstName": "Mitchell",
"City": "San Francisco",
}
var result Person
err := mapstructure.Decode(input, &result)
if err != nil {
panic(err)
}
res, err := json.Marshal(result)
fmt.Println(string(res))
}
The text was updated successfully, but these errors were encountered:
Hi
I would like to decode this kind of structure with pointers and nested structure with pointers
and keeping the field in input as a flattened map and using squash
But got an error :
I would like to achieve this results
If LastName is filled with value "test" : Family is initialiazed and Lastname equals "test" and json ouput is
{"family":{"lastname":"test"},"city":"San Francisco","firstname":"Mitchell"}
If LastName is nil : Family is nil and json output is
{"city":"San Francisco","firstname":"Mitchell"}
Thanks
The text was updated successfully, but these errors were encountered: