-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcept_test.go
148 lines (124 loc) · 3.04 KB
/
concept_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package golymorph
import (
"fmt"
"github.com/mitchellh/mapstructure"
"reflect"
"testing"
)
func TestReadingMapByField(t *testing.T) {
input := map[string]any{
"foo": map[string]any{
"bar": map[string]any{
"test": 123,
},
},
}
t.Logf("input: %v\n", input)
value := reflect.ValueOf(input)
foo := value.MapIndex(reflect.ValueOf("foo")).Elem()
t.Logf("foo: %v\n", foo)
bar := foo.MapIndex(reflect.ValueOf("bar")).Elem()
t.Logf("bar: %v\n", bar)
test := bar.MapIndex(reflect.ValueOf("test")).Elem()
t.Logf("test: %v\n", test)
}
func TestMapstructure(t *testing.T) {
type Bar struct {
Test int
}
type Foo struct {
Bar any
Test int
}
fooVal := reflect.ValueOf(Foo{})
fooType := fooVal.Type()
for i := 0; i < fooType.NumField(); i++ {
field := fooType.Field(i)
t.Logf("fooVal.Field(%d): %s=%v\n", i, field.Name, fooVal.Field(i))
}
input := map[string]any{
"Bar": map[string]int{
"Test": 123,
},
"Test": 456,
}
t.Logf("input: %v\n", input)
// decode
var output Foo
if err := mapstructure.Decode(input, &output); err != nil {
t.Errorf("error decoding: %s", err)
}
t.Logf("output: %T%+v\n", output, output)
}
func TestWritingStructByFields(t *testing.T) {
type Animal struct {
Name string
Specifics any
}
type Horse struct {
Shoes int
}
type Duck struct {
Feathers int
}
animal := Animal{
Name: "horsey",
Specifics: map[string]any{"type": "horse", "shoes": 4},
}
value := reflect.ValueOf(&animal).Elem()
name := value.FieldByName("Name")
t.Logf("name: %v\n", name)
value = value.FieldByName("Specifics")
t.Logf("specifics: %v\n", value)
value.Set(reflect.ValueOf(Horse{4}))
t.Logf("animal: %+v\n", animal)
if reflect.TypeOf(animal.Specifics) != reflect.TypeOf(Horse{}) {
t.Fatalf("expected animal.Specifics to be of type Horse, but got %T", animal.Specifics)
}
}
func TestWritingStructByFieldsInMethod(t *testing.T) {
type Animal struct {
Name string
Specifics any
}
type Horse struct {
Shoes int
}
type Duck struct {
Feathers int
}
animal := Animal{
Name: "horsey",
Specifics: map[string]any{"type": "horse", "shoes": 4},
}
SetField(&animal)
t.Logf("animal: %+v\n", animal)
if reflect.TypeOf(animal.Specifics).Name() != reflect.TypeOf(Horse{4}).Name() {
t.Fatalf("expected animal.Specifics to be of type %+v, but got %+v", reflect.TypeOf(Horse{4}), reflect.TypeOf(animal.Specifics))
}
}
func SetField(animalPtr any) {
value := reflect.ValueOf(animalPtr).Elem()
name := value.FieldByName("Name")
fmt.Printf("name: %v\n", name)
value = value.FieldByName("Specifics")
fmt.Printf("specifics: %v\n", value)
value.Set(reflect.ValueOf(Horse{4}))
fmt.Printf("animal: %+v\n", reflect.ValueOf(animalPtr).Elem().Interface())
}
func TestCompareAny(t *testing.T) {
a := any(1)
b := any(1)
if a != b {
t.Fatalf("expected %v to equal %v", a, b)
}
}
func TestAnyMap(t *testing.T) {
typeVal := reflect.TypeOf(int64(0))
anyMap := TypeMap{
"foo": typeVal,
}
if anyMap["foo"] != typeVal {
t.Fatalf(`expected anyMap["foo"] to equal %+v`, typeVal)
}
}