mapping_test.go 1.1 KB
package utils

import (
	"github.com/tiptok/gocomm/common"
	"testing"
)

func TestLoadCustomField(t *testing.T) {

	type User struct {
		Name string
		Id   int
	}
	v := []struct {
		Name string
		Id   int
	}{{Name: "c1", Id: 1}, {Name: "c2", Id: 2}, {Name: "c3", Id: 3}}
	ret := LoadCustomField(&v, "Name")
	t.Log(common.JsonAssertString(ret))

	v2 := struct {
		Name string
		Id   int
	}{Name: "c1", Id: 1}
	ret2 := LoadCustomField(v2, "Name", "Id")
	t.Log(common.JsonAssertString(ret2))

	v3 := []*User{&User{Name: "c1", Id: 1}, &User{Name: "c2", Id: 2}, &User{Name: "c3", Id: 3}}
	ret3 := LoadCustomField(&v3, "Name", "Name2")
	t.Log(common.JsonAssertString(ret3))
}

func TestAppendCustomField(t *testing.T) {
	customMap := map[string]interface{}{"1": "h", "2": "e"}
	t.Log(customMap)

	customeFiles := map[string]interface{}{
		"Value":  map[string]interface{}{"a": "a", "b": "b"},
		"Value2": "cc",
		"Value3": 9999,
	}
	t.Log(AppendCustomField(customMap, customeFiles))

	t.Log(AppendCustomField(struct {
		Name string `json:"name"`
		Age  int64  `json:"age"`
	}{Name: "ccc", Age: 20}, customeFiles))
}