/
container.go
226 lines (185 loc) · 5.33 KB
/
container.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package compile
import (
"fmt"
"go.uber.org/thriftrw/ast"
"go.uber.org/thriftrw/wire"
)
// MapSpec represents a key-value mapping between two types.
type MapSpec struct {
linkOnce
nativeThriftType
KeySpec, ValueSpec TypeSpec
Annotations Annotations
}
// compileMapType compiles the given MapType AST into a MapSpec.
func compileMapType(src ast.MapType) (*MapSpec, error) {
annots, err := compileAnnotations(src.Annotations)
if err != nil {
return nil, err
}
keySpec, err := compileTypeReference(src.KeyType)
if err != nil {
return nil, err
}
valueSpec, err := compileTypeReference(src.ValueType)
if err != nil {
return nil, err
}
return &MapSpec{
KeySpec: keySpec,
ValueSpec: valueSpec,
Annotations: annots,
}, nil
}
// Link resolves the type references in the MapSpec.
func (m *MapSpec) Link(scope Scope) (TypeSpec, error) {
if m.linked() {
return m, nil
}
var err error
m.KeySpec, err = m.KeySpec.Link(scope)
if err != nil {
return m, err
}
m.ValueSpec, err = m.ValueSpec.Link(scope)
if err != nil {
return m, err
}
return m, nil
}
// ThriftName for MapSpec
func (m *MapSpec) ThriftName() string {
return fmt.Sprintf(
"map<%s, %s>", m.KeySpec.ThriftName(), m.ValueSpec.ThriftName(),
)
}
// TypeCode for MapSpec
func (m *MapSpec) TypeCode() wire.Type {
return wire.TMap
}
// ForEachTypeReference for MapSpec
func (m *MapSpec) ForEachTypeReference(f func(TypeSpec) error) error {
if err := f(m.KeySpec); err != nil {
return err
}
return f(m.ValueSpec)
}
// ThriftAnnotations returns all associated annotations.
func (m *MapSpec) ThriftAnnotations() Annotations {
return m.Annotations
}
//////////////////////////////////////////////////////////////////////////////
// ListSpec represents lists of values of the same type.
type ListSpec struct {
linkOnce
nativeThriftType
ValueSpec TypeSpec
Annotations Annotations
}
// compileListSpec compiles the given ListType AST into a ListSpec.
func compileListType(src ast.ListType) (*ListSpec, error) {
annots, err := compileAnnotations(src.Annotations)
if err != nil {
return nil, err
}
valueSpec, err := compileTypeReference(src.ValueType)
if err != nil {
return nil, err
}
return &ListSpec{
ValueSpec: valueSpec,
Annotations: annots,
}, nil
}
// Link resolves the type references in the ListSpec.
func (l *ListSpec) Link(scope Scope) (TypeSpec, error) {
if l.linked() {
return l, nil
}
var err error
l.ValueSpec, err = l.ValueSpec.Link(scope)
return l, err
}
// TypeCode for ListSpec
func (l *ListSpec) TypeCode() wire.Type {
return wire.TList
}
// ThriftName for ListSpec
func (l *ListSpec) ThriftName() string {
return fmt.Sprintf("list<%s>", l.ValueSpec.ThriftName())
}
// ForEachTypeReference for ListSpec
func (l *ListSpec) ForEachTypeReference(f func(TypeSpec) error) error {
return f(l.ValueSpec)
}
// ThriftAnnotations returns all associated annotations.
func (l *ListSpec) ThriftAnnotations() Annotations {
return l.Annotations
}
//////////////////////////////////////////////////////////////////////////////
// SetSpec represents sets of values of the same type.
type SetSpec struct {
linkOnce
nativeThriftType
ValueSpec TypeSpec
Annotations Annotations
}
// compileSetSpec compiles the given SetType AST into a SetSpec.
func compileSetType(src ast.SetType) (*SetSpec, error) {
annots, err := compileAnnotations(src.Annotations)
if err != nil {
return nil, err
}
valueSpec, err := compileTypeReference(src.ValueType)
if err != nil {
return nil, err
}
return &SetSpec{
ValueSpec: valueSpec,
Annotations: annots,
}, nil
}
// Link resolves the type references in the SetSpec.
func (s *SetSpec) Link(scope Scope) (TypeSpec, error) {
if s.linked() {
return s, nil
}
var err error
s.ValueSpec, err = s.ValueSpec.Link(scope)
return s, err
}
// TypeCode for SetSpec
func (s *SetSpec) TypeCode() wire.Type {
return wire.TSet
}
// ThriftName for SetSpec.
func (s *SetSpec) ThriftName() string {
return fmt.Sprintf("set<%s>", s.ValueSpec.ThriftName())
}
// ForEachTypeReference for SetSpec
func (s *SetSpec) ForEachTypeReference(f func(TypeSpec) error) error {
return f(s.ValueSpec)
}
// ThriftAnnotations returns all associated annotations.
func (s *SetSpec) ThriftAnnotations() Annotations {
return s.Annotations
}