github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/generic/node_test.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package generic
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/dynamicgo/thrift"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestNewTypedNode(t *testing.T) {
    29  	type args struct {
    30  		typ      thrift.Type
    31  		et       thrift.Type
    32  		kt       thrift.Type
    33  		children []PathNode
    34  	}
    35  	tests := []struct {
    36  		name    string
    37  		args    args
    38  		opts    Options
    39  		wantRet interface{}
    40  	}{
    41  		{"bool", args{thrift.BOOL, 0, 0, []PathNode{{Node: NewNodeBool(true)}}}, Options{}, true},
    42  		{"string", args{thrift.STRING, 0, 0, []PathNode{{Node: NewNodeString("1")}}}, Options{}, "1"},
    43  		{"binary", args{thrift.STRING, 0, 0, []PathNode{{Node: NewNodeBinary([]byte{1})}}}, Options{CastStringAsBinary: true}, []byte{1}},
    44  		{"byte", args{thrift.BYTE, 0, 0, []PathNode{{Node: NewNodeByte(1)}}}, Options{}, int(1)},
    45  		{"i16", args{thrift.I16, 0, 0, []PathNode{{Node: NewNodeInt16(1)}}}, Options{}, int(1)},
    46  		{"i32", args{thrift.I32, 0, 0, []PathNode{{Node: NewNodeInt32(1)}}}, Options{}, int(1)},
    47  		{"i64", args{thrift.I64, 0, 0, []PathNode{{Node: NewNodeInt64(1)}}}, Options{}, int(1)},
    48  		{"double", args{thrift.DOUBLE, 0, 0, []PathNode{{Node: NewNodeDouble(1)}}}, Options{}, float64(1)},
    49  		{"list", args{thrift.LIST, thrift.BYTE, 0, []PathNode{{Path: NewPathIndex(0), Node: NewNodeByte(1)}}}, Options{}, []interface{}{int(1)}},
    50  		{"set", args{thrift.SET, thrift.BYTE, 0, []PathNode{{Path: NewPathIndex(0), Node: NewNodeByte(1)}}}, Options{}, []interface{}{int(1)}},
    51  		{"int map", args{thrift.MAP, thrift.BYTE, thrift.BYTE, []PathNode{{Path: NewPathIntKey(1), Node: NewNodeByte(1)}}}, Options{}, map[int]interface{}{int(1): int(1)}},
    52  		{"string map", args{thrift.MAP, thrift.BYTE, thrift.STRING, []PathNode{{Path: NewPathStrKey("1"), Node: NewNodeByte(1)}}}, Options{}, map[string]interface{}{"1": int(1)}},
    53  		{"any map + key list", args{thrift.MAP, thrift.BYTE, thrift.LIST, []PathNode{{Path: NewPathBinKey(NewNodeList([]interface{}{1, 2}).Raw()), Node: NewNodeByte(1)}}}, Options{}, map[interface{}]interface{}{&[]interface{}{int(1), int(2)}: int(1)}},
    54  		{"any map + key map", args{thrift.MAP, thrift.BYTE, thrift.MAP, []PathNode{{Path: NewPathBinKey(NewNodeMap(map[interface{}]interface{}{1: 2}, &Options{}).Raw()), Node: NewNodeByte(1)}}}, Options{}, map[interface{}]interface{}{&map[int]interface{}{1: 2}: int(1)}},
    55  		{"struct", args{thrift.STRUCT, 0, 0, []PathNode{{Path: NewPathFieldId(1), Node: NewNodeByte(1)}}}, Options{MapStructById: true}, map[thrift.FieldID]interface{}{thrift.FieldID(1): int(1)}},
    56  		{"struct + struct", args{thrift.STRUCT, 0, 0, []PathNode{{Path: NewPathFieldId(1), Node: NewNodeStruct(map[thrift.FieldID]interface{}{1: 1}, &Options{})}}}, Options{MapStructById: true}, map[thrift.FieldID]interface{}{thrift.FieldID(1): map[thrift.FieldID]interface{}{thrift.FieldID(1): int(1)}}},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			println(tt.name)
    61  			ret := NewTypedNode(tt.args.typ, tt.args.et, tt.args.kt, tt.args.children...)
    62  			fmt.Printf("buf:%+v\n", ret.Raw())
    63  			val, err := ret.Interface(&tt.opts)
    64  			fmt.Printf("val:%#v\n", val)
    65  			if err != nil {
    66  				t.Errorf("NewTypedNode() error = %v", err)
    67  				return
    68  			}
    69  			if strings.Contains(tt.name, "map + key") {
    70  				em := tt.wantRet.(map[interface{}]interface{})
    71  				gm := val.(map[interface{}]interface{})
    72  				require.Equal(t, len(em), len(gm))
    73  				var firstK, firstV interface{}
    74  				for k, v := range em {
    75  					firstK, firstV = k, v
    76  					break
    77  				}
    78  				var firstKgot, firstVgot interface{}
    79  				for k, v := range gm {
    80  					firstKgot, firstVgot = k, v
    81  					break
    82  				}
    83  				require.Equal(t, firstK, firstKgot)
    84  				require.Equal(t, firstV, firstVgot)
    85  			} else {
    86  				require.Equal(t, tt.wantRet, val)
    87  			}
    88  		})
    89  	}
    90  }