-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
203 lines (170 loc) · 4.3 KB
/
types.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
/*
Package ydbtypes provides tools for integration ydb types with go/types package.
It is intended primarily for writing generation tools and linters.
*/
package ydbtypes
import (
"fmt"
"go/types"
"strings"
"github.com/yandex-cloud/ydb-go-sdk/v2/internal"
)
func PrimitiveTypeFromString(s string) (t internal.PrimitiveType, err error) {
switch strings.ToLower(s) {
case "bool":
t = internal.TypeBool
case "int8":
t = internal.TypeInt8
case "uint8":
t = internal.TypeUint8
case "int16":
t = internal.TypeInt16
case "uint16":
t = internal.TypeUint16
case "int32":
t = internal.TypeInt32
case "uint32":
t = internal.TypeUint32
case "int64":
t = internal.TypeInt64
case "uint64":
t = internal.TypeUint64
case "float":
t = internal.TypeFloat
case "double":
t = internal.TypeDouble
case "date":
t = internal.TypeDate
case "datetime":
t = internal.TypeDatetime
case "timestamp":
t = internal.TypeTimestamp
case "interval":
t = internal.TypeInterval
case "tzdate":
t = internal.TypeTzDate
case "tzdatetime":
t = internal.TypeTzDatetime
case "tztimestamp":
t = internal.TypeTzTimestamp
case "string":
t = internal.TypeString
case "utf8":
t = internal.TypeUTF8
case "yson":
t = internal.TypeYSON
case "json":
t = internal.TypeJSON
case "uuid":
t = internal.TypeUUID
case "jsondocument":
t = internal.TypeJSONDocument
case "dynumber":
t = internal.TypeDyNumber
default:
err = fmt.Errorf("ydbtypes: unknown type: %q", s)
}
return
}
func PrimitiveTypeFromGoType(typ types.Type) (t internal.PrimitiveType, err error) {
switch t := typ.(type) {
case *types.Slice:
b, basic := t.Elem().(*types.Basic)
if basic && b.Kind() == types.Byte {
return internal.TypeString, nil
}
case *types.Basic:
switch t.Kind() {
case types.Bool:
return internal.TypeBool, nil
case types.Int8:
return internal.TypeInt8, nil
case types.Int16:
return internal.TypeInt16, nil
case types.Int32:
return internal.TypeInt32, nil
case types.Int64:
return internal.TypeInt64, nil
case types.Uint8:
return internal.TypeUint8, nil
case types.Uint16:
return internal.TypeUint16, nil
case types.Uint32:
return internal.TypeUint32, nil
case types.Uint64:
return internal.TypeUint64, nil
case types.Float32:
return internal.TypeFloat, nil
case types.Float64:
return internal.TypeDouble, nil
case types.String:
return internal.TypeUTF8, nil
case types.Int, types.Uint:
return 0, fmt.Errorf(
"ydbtypes: ambiguous mapping for go %s type exists",
typ,
)
}
}
return 0, fmt.Errorf("ydbtypes: unsupported type: %s", typ)
}
var (
bytesArray16 = types.NewArray(types.Typ[types.Byte], 16)
bytesSlice = types.NewSlice(types.Typ[types.Byte])
)
func GoTypeFromPrimitiveType(t internal.PrimitiveType) types.Type {
switch t {
case internal.TypeBool:
return types.Typ[types.Bool]
case internal.TypeInt8:
return types.Typ[types.Int8]
case internal.TypeUint8:
return types.Typ[types.Uint8]
case internal.TypeInt16:
return types.Typ[types.Int16]
case internal.TypeUint16:
return types.Typ[types.Uint16]
case internal.TypeInt32:
return types.Typ[types.Int32]
case internal.TypeUint32:
return types.Typ[types.Uint32]
case internal.TypeInt64:
return types.Typ[types.Int64]
case internal.TypeUint64:
return types.Typ[types.Uint64]
case internal.TypeFloat:
return types.Typ[types.Float32]
case internal.TypeDouble:
return types.Typ[types.Float64]
case internal.TypeDate:
return types.Typ[types.Uint32]
case internal.TypeDatetime:
return types.Typ[types.Uint32]
case internal.TypeTimestamp:
return types.Typ[types.Uint64]
case internal.TypeInterval:
return types.Typ[types.Int64]
case internal.TypeTzDate:
return types.Typ[types.String]
case internal.TypeTzDatetime:
return types.Typ[types.String]
case internal.TypeTzTimestamp:
return types.Typ[types.String]
case internal.TypeUTF8:
return types.Typ[types.String]
case internal.TypeYSON:
return types.Typ[types.String]
case internal.TypeJSON:
return types.Typ[types.String]
case internal.TypeUUID:
return bytesArray16
case internal.TypeJSONDocument:
return types.Typ[types.String]
case internal.TypeDyNumber:
return types.Typ[types.String]
case internal.TypeString:
return bytesSlice
default:
panic("uncovered primitive type")
}
}