github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/jsonutils/map_test.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package jsonutils
     4  
     5  import (
     6  	"github.com/iwind/TeaGo/assert"
     7  	"github.com/iwind/TeaGo/maps"
     8  	"testing"
     9  )
    10  
    11  func TestMapToObject(t *testing.T) {
    12  	a := assert.NewAssertion(t)
    13  
    14  	type typeA struct {
    15  		B int  `json:"b"`
    16  		C bool `json:"c"`
    17  	}
    18  
    19  	{
    20  		var obj = &typeA{B: 1, C: true}
    21  		m, err := ObjectToMap(obj)
    22  		if err != nil {
    23  			t.Fatal(err)
    24  		}
    25  		PrintT(m, t)
    26  		a.IsTrue(m.GetInt("b") == 1)
    27  		a.IsTrue(m.GetBool("c") == true)
    28  	}
    29  
    30  	{
    31  		var obj = &typeA{}
    32  		err := MapToObject(maps.Map{
    33  			"b": 1024,
    34  			"c": true,
    35  		}, obj)
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		a.IsTrue(obj.B == 1024)
    40  		a.IsTrue(obj.C == true)
    41  		PrintT(obj, t)
    42  	}
    43  }