59 lines
		
	
	
		
			723 B
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			723 B
		
	
	
	
		
			Go
		
	
	
	
package mapstructure
 | 
						|
 | 
						|
import (
 | 
						|
	"reflect"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func TestDecode_Ptr(t *testing.T) {
 | 
						|
	t.Parallel()
 | 
						|
 | 
						|
	type G struct {
 | 
						|
		Id   int
 | 
						|
		Name string
 | 
						|
	}
 | 
						|
 | 
						|
	type X struct {
 | 
						|
		Id   int
 | 
						|
		Name int
 | 
						|
	}
 | 
						|
 | 
						|
	type AG struct {
 | 
						|
		List []*G
 | 
						|
	}
 | 
						|
 | 
						|
	type AX struct {
 | 
						|
		List []*X
 | 
						|
	}
 | 
						|
 | 
						|
	g2 := &AG{
 | 
						|
		List: []*G{
 | 
						|
			{
 | 
						|
				Id:   11,
 | 
						|
				Name: "gg",
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
	x2 := AX{}
 | 
						|
 | 
						|
	// 报错但还是会转换成功,转换后值为目标类型的 0 值
 | 
						|
	err := Decode(g2, &x2)
 | 
						|
 | 
						|
	res := AX{
 | 
						|
		List: []*X{
 | 
						|
			{
 | 
						|
				Id:   11,
 | 
						|
				Name: 0, // 这个类型的 0 值
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	if err == nil {
 | 
						|
		t.Errorf("Decode_Ptr jderr should not be 'nil': %#v", err)
 | 
						|
	}
 | 
						|
 | 
						|
	if !reflect.DeepEqual(res, x2) {
 | 
						|
		t.Errorf("result should be %#v: got %#v", res, x2)
 | 
						|
	}
 | 
						|
}
 |