/
type.go
201 lines (183 loc) · 5.88 KB
/
type.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
// Copyright (c) 2018 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 gen
import (
"fmt"
"go.uber.org/thriftrw/compile"
)
// TypeDefinition generates code for the given TypeSpec.
func TypeDefinition(g Generator, spec compile.TypeSpec) error {
switch s := spec.(type) {
case *compile.EnumSpec:
return enum(g, s)
case *compile.StructSpec:
return structure(g, s)
case *compile.TypedefSpec:
return typedef(g, s)
default:
panic(fmt.Sprintf("%q is not a defined type", spec.ThriftName()))
}
}
// isHashable returns true if the given type is considered hashable by
// thriftrw.
//
// Only primitive types, enums, and typedefs of other hashable types are
// considered hashable.
func isHashable(t compile.TypeSpec) bool {
return isPrimitiveType(t)
}
// isPrimitiveType returns true if the given type is a primitive type.
// Primitive types, enums, and typedefs of primitive types are considered
// primitive.
//
// Note that binary is not considered a primitive type because it is
// represented as []byte in Go.
func isPrimitiveType(spec compile.TypeSpec) bool {
spec = compile.RootTypeSpec(spec)
switch spec.(type) {
case *compile.BoolSpec, *compile.I8Spec, *compile.I16Spec, *compile.I32Spec,
*compile.I64Spec, *compile.DoubleSpec, *compile.StringSpec:
return true
}
_, isEnum := spec.(*compile.EnumSpec)
return isEnum
}
// isReferenceType checks if the given TypeSpec represents a reference type.
//
// Sets, maps, lists, and slices are reference types.
func isReferenceType(spec compile.TypeSpec) bool {
spec = compile.RootTypeSpec(spec)
if _, ok := spec.(*compile.BinarySpec); ok {
return true
}
switch spec.(type) {
case *compile.MapSpec, *compile.ListSpec, *compile.SetSpec:
return true
default:
return false
}
}
func isStructType(spec compile.TypeSpec) bool {
spec = compile.RootTypeSpec(spec)
_, isStruct := spec.(*compile.StructSpec)
return isStruct
}
// typeReference returns a string representation of a reference to the given
// type.
func typeReference(g Generator, spec compile.TypeSpec) (string, error) {
name, err := typeName(g, spec)
if err != nil {
return "", err
}
if isStructType(spec) {
// Prepend "*" to the result if the field is not required and the type
// isn't a reference type.
name = "*" + name
}
return name, nil
}
// typeReferencePtr returns a strung representing a reference to a pointer of
// the given type. The pointer prefix is not added for types that are already
// reference types.
func typeReferencePtr(g Generator, spec compile.TypeSpec) (string, error) {
ref, err := typeName(g, spec)
if err != nil {
return "", err
}
if !isReferenceType(spec) {
// need * prefix for everything but map, string, and list.
return "*" + ref, nil
}
return ref, nil
}
// typeName returns the name of the given type, whether it's a custom type or
// native.
func typeName(g Generator, spec compile.TypeSpec) (string, error) {
switch s := spec.(type) {
case *compile.BoolSpec:
return "bool", nil
case *compile.I8Spec:
return "int8", nil
case *compile.I16Spec:
return "int16", nil
case *compile.I32Spec:
return "int32", nil
case *compile.I64Spec:
return "int64", nil
case *compile.DoubleSpec:
return "float64", nil
case *compile.StringSpec:
return "string", nil
case *compile.BinarySpec:
return "[]byte", nil
case *compile.MapSpec:
k, err := typeReference(g, s.KeySpec)
if err != nil {
return "", err
}
v, err := typeReference(g, s.ValueSpec)
if err != nil {
return "", err
}
if !isHashable(s.KeySpec) {
// unhashable type
return fmt.Sprintf("[]struct{Key %s; Value %s}", k, v), nil
}
return fmt.Sprintf("map[%s]%s", k, v), nil
case *compile.ListSpec:
v, err := typeReference(g, s.ValueSpec)
if err != nil {
return "", err
}
return "[]" + v, nil
case *compile.SetSpec:
v, err := typeReference(g, s.ValueSpec)
if err != nil {
return "", err
}
if !isHashable(s.ValueSpec) {
// unhashable type
return fmt.Sprintf("[]%s", v), nil
}
return fmt.Sprintf("map[%s]struct{}", v), nil
case *compile.EnumSpec, *compile.StructSpec, *compile.TypedefSpec:
return g.LookupTypeName(spec)
default:
panic(fmt.Sprintf("Unknown type (%T) %v", spec, spec))
}
}
func equalsFuncName(g Generator, spec compile.TypeSpec) string {
return fmt.Sprintf("_%s_Equals", g.MangleType(spec))
}
func equalsPtrFuncName(g Generator, spec compile.TypeSpec) string {
return fmt.Sprintf("_%s_EqualsPtr", g.MangleType(spec))
}
func readerFuncName(g Generator, spec compile.TypeSpec) string {
return fmt.Sprintf("_%s_Read", g.MangleType(spec))
}
func valueListName(g Generator, spec compile.TypeSpec) string {
return fmt.Sprintf("_%s_ValueList", g.MangleType(spec))
}
// canBeConstant returns true if the given type can be a constant.
func canBeConstant(t compile.TypeSpec) bool {
// Only primitives can use const declarations. Everything else has to be a
// `var` declaration.
return isPrimitiveType(t)
}