Skip to content

Instantly share code, notes, and snippets.

@zhujintao
Forked from bxcodec/_struct_to_map.go
Created November 21, 2022 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhujintao/06ffdce522480894551d62665b02fe42 to your computer and use it in GitHub Desktop.
Save zhujintao/06ffdce522480894551d62665b02fe42 to your computer and use it in GitHub Desktop.
Golang Struct To Map Example By JSON tag
/*
This function will help you to convert your object from struct to map[string]interface{} based on your JSON tag in your structs.
Example how to use posted in sample_test.go file.
*/
func structToMap(item interface{}) map[string]interface{} {
res := map[string]interface{}{}
if item == nil {
return res
}
v := reflect.TypeOf(item)
reflectValue := reflect.ValueOf(item)
reflectValue = reflect.Indirect(reflectValue)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
tag := v.Field(i).Tag.Get("json")
field := reflectValue.Field(i).Interface()
if tag != "" && tag != "-" {
if v.Field(i).Type.Kind() == reflect.Struct {
res[tag] = structToMap(field)
} else {
res[tag] = field
}
}
}
return res
}
package sample_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type SampleStruct struct {
Name string `json:"name"`
ID string `json:"id"`
}
type EmbededStruct struct {
FieldStruct `json:"field"`
Hello string `json:"hello"`
}
type FieldStruct struct {
OnePoint string `json:"one_point"`
Sample *SampleStruct `json:"sample"`
}
func TestStructToMap_Normal(t *testing.T) {
sample := SampleStruct{
Name: "John Doe",
ID: "12121",
}
res := structToMap(sample)
require.NotNil(t, res)
fmt.Printf("%+v \n", res)
// Output: map[name:John Doe id:12121]
jbyt, err := json.Marshal(res)
require.NoError(t, err)
fmt.Println(string(jbyt))
// Output: {"id":"12121","name":"John Doe"}
}
func TestStructToMap_FieldStruct(t *testing.T) {
sample := &SampleStruct{
Name: "John Doe",
ID: "12121",
}
field := FieldStruct{
Sample: sample,
OnePoint: "yuhuhuu",
}
res := structToMap(field)
require.NotNil(t, res)
fmt.Printf("%+v \n", res)
// Output: map[sample:0xc4200f04a0 one_point:yuhuhuu]
jbyt, err := json.Marshal(res)
require.NoError(t, err)
fmt.Println(string(jbyt))
// Output: {"one_point":"yuhuhuu","sample":{"name":"John Doe","id":"12121"}}
}
func TestStructToMap_EmbeddedStruct(t *testing.T) {
sample := &SampleStruct{
Name: "John Doe",
ID: "12121",
}
field := FieldStruct{
Sample: sample,
OnePoint: "yuhuhuu",
}
embed := EmbededStruct{
FieldStruct: field,
Hello: "WORLD!!!!",
}
res := structToMap(embed)
require.NotNil(t, res)
fmt.Printf("%+v \n", res)
//Output: map[field:map[one_point:yuhuhuu sample:0xc420106420] hello:WORLD!!!!]
jbyt, err := json.Marshal(res)
require.NoError(t, err)
fmt.Println(string(jbyt))
// Output: {"field":{"one_point":"yuhuhuu","sample":{"name":"John Doe","id":"12121"}},"hello":"WORLD!!!!"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment