File: reflect_test.go

package info (click to toggle)
golang-github-segmentio-encoding 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,468 kB
  • sloc: makefile: 286
file content (178 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package proto

import (
	"fmt"
	"reflect"
	"testing"
)

type RecursiveMessage struct {
	Next *RecursiveMessage `protobuf:"bytes,1,opt,name=next,proto3"`
}

func TestTypeOf(t *testing.T) {
	tests := []struct {
		value any
		proto string
	}{
		// primitive types
		{value: true, proto: "bool"},
		{value: int(1), proto: "int64"},
		{value: int32(1), proto: "int32"},
		{value: int64(1), proto: "int64"},
		{value: uint(1), proto: "uint64"},
		{value: uint32(1), proto: "uint32"},
		{value: uint64(1), proto: "uint64"},
		{value: float32(1), proto: "float"},
		{value: float64(1), proto: "double"},
		{value: "hello", proto: "string"},
		{value: []byte("A"), proto: "bytes"},

		// map types
		{value: map[int]string{}, proto: "map<int64, string>"},

		// struct types
		{
			value: struct{}{},
			proto: `message {}`,
		},

		{
			value: struct{ A int }{},
			proto: `message {
  int64 A = 1;
}`,
		},

		{
			value: struct {
				A int    `protobuf:"varint,1,opt,name=hello,proto3"`
				B string `protobuf:"bytes,3,rep,name=world,proto3"`
			}{},
			proto: `message {
  int64 hello = 1;
  repeated string world = 3;
}`,
		},

		{
			value: RecursiveMessage{},
			proto: `message RecursiveMessage {
  RecursiveMessage next = 1;
}`,
		},

		{
			value: struct {
				M RawMessage
			}{},
			proto: `message {
  bytes M = 1;
}`,
		},
	}

	for _, test := range tests {
		t.Run(fmt.Sprintf("%T", test.value), func(t *testing.T) {
			typ := TypeOf(reflect.TypeOf(test.value))
			str := typ.String()

			if str != test.proto {
				t.Error("protobuf representation mismatch")
				t.Log("want:", test.proto)
				t.Log("got: ", str)
			}
		})
	}
}

func TestTypesAreEqual(t *testing.T) {
	if TypeOf(reflect.TypeOf(true)) != TypeOf(reflect.TypeOf(false)) {
		t.Error("type of true is not equal to type of false")
	}
}

func TestTypesAreNotEqual(t *testing.T) {
	if TypeOf(reflect.TypeOf(false)) == TypeOf(reflect.TypeOf(0)) {
		t.Error("type of bool equal type of int")
	}
}

func TestParseStructTag(t *testing.T) {
	tests := []struct {
		str string
		tag structTag
	}{
		{
			str: `bytes,1,rep,name=next,proto3`,
			tag: structTag{
				name:        "next",
				version:     3,
				wireType:    Varlen,
				fieldNumber: 1,
				extensions:  map[string]string{},
				repeated:    true,
			},
		},

		{
			str: `bytes,5,opt,name=key,proto3`,
			tag: structTag{
				name:        "key",
				version:     3,
				wireType:    Varlen,
				fieldNumber: 5,
				extensions:  map[string]string{},
			},
		},

		{
			str: `fixed64,6,opt,name=seed,proto3`,
			tag: structTag{
				name:        "seed",
				version:     3,
				wireType:    Fixed64,
				fieldNumber: 6,
				extensions:  map[string]string{},
			},
		},

		{
			str: `varint,8,opt,name=expire_after,json=expireAfter,proto3`,
			tag: structTag{
				name:        "expire_after",
				json:        "expireAfter",
				version:     3,
				wireType:    Varint,
				fieldNumber: 8,
				extensions:  map[string]string{},
			},
		},

		{
			str: `bytes,17,opt,name=batch_key,json=batchKey,proto3,customtype=U128`,
			tag: structTag{
				name:        "batch_key",
				json:        "batchKey",
				version:     3,
				wireType:    Varlen,
				fieldNumber: 17,
				extensions: map[string]string{
					"customtype": "U128",
				},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.str, func(t *testing.T) {
			tag, err := parseStructTag(test.str)
			if err != nil {
				t.Fatal(err)
			}
			if !reflect.DeepEqual(tag, test.tag) {
				t.Errorf("struct tag mismatch\nwant: %+v\ngot: %+v", test.tag, tag)
			}
		})
	}
}