-
Notifications
You must be signed in to change notification settings - Fork 0
/
tt.go
299 lines (252 loc) · 9.17 KB
/
tt.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package tupleconv
import (
"fmt"
)
// TypeName is the data type for type names.
type TypeName string
// Types are supported tarantool types.
const (
TypeBoolean TypeName = "boolean"
TypeString TypeName = "string"
TypeInteger TypeName = "integer"
TypeUnsigned TypeName = "unsigned"
TypeDouble TypeName = "double"
TypeNumber TypeName = "number"
TypeDecimal TypeName = "decimal"
TypeDatetime TypeName = "datetime"
TypeUUID TypeName = "uuid"
TypeArray TypeName = "array"
TypeMap TypeName = "map"
TypeVarbinary TypeName = "varbinary"
TypeScalar TypeName = "scalar"
TypeAny TypeName = "any"
TypeInterval TypeName = "interval"
)
const (
defaultThousandSeparators = ""
defaultDecimalSeparators = "."
defaultNullValue = ""
)
// TTConvFactory is a factory capable of creating converters from Type
// to tarantool types.
type TTConvFactory[Type any] interface {
// GetBooleanConverter returns a converter from Type to boolean.
GetBooleanConverter() Converter[Type, any]
// GetStringConverter returns a converter from Type to string.
GetStringConverter() Converter[Type, any]
// GetUnsignedConverter returns a converter from Type to unsigned.
GetUnsignedConverter() Converter[Type, any]
// GetDatetimeConverter returns a converter from Type to datetime.
GetDatetimeConverter() Converter[Type, any]
// GetUUIDConverter returns a converter from Type to uuid.
GetUUIDConverter() Converter[Type, any]
// GetMapConverter returns a converter from Type to map.
GetMapConverter() Converter[Type, any]
// GetArrayConverter returns a converter from Type to array.
GetArrayConverter() Converter[Type, any]
// GetVarbinaryConverter returns a converter from Type to varbinary.
GetVarbinaryConverter() Converter[Type, any]
// GetDoubleConverter returns a converter from Type to double.
GetDoubleConverter() Converter[Type, any]
// GetDecimalConverter returns a converter from Type to decimal.
GetDecimalConverter() Converter[Type, any]
// GetIntegerConverter returns a converter from Type to integer.
GetIntegerConverter() Converter[Type, any]
// GetNumberConverter returns a converter from Type to number.
GetNumberConverter() Converter[Type, any]
// GetAnyConverter returns a converter from Type to any.
GetAnyConverter() Converter[Type, any]
// GetScalarConverter returns a converter from Type to scalar.
GetScalarConverter() Converter[Type, any]
// GetIntervalConverter returns a converter from Type to interval.
GetIntervalConverter() Converter[Type, any]
// MakeNullableConverter extends the incoming converter to a nullable converter.
MakeNullableConverter(Converter[Type, any]) Converter[Type, any]
}
// StringToTTConvFactory is the default TypeToTTConvFactory for strings.
// To customize the creation of converters, inherit from it and override the necessary methods.
type StringToTTConvFactory struct {
// thousandSeparators are thousands separators for numeric types.
// In fact, these symbols will be ignored for numeric types.
// For example:
// thousandSeparators = "`#e": 1`2`3 -> 123, 123#456#789 -> 123456789, 1e5 -> 15.
thousandSeparators string
// decimalSeparators are additional decimal separators for numeric types (besides `.`).
// In fact, these symbols will be replaced with `.`.
// For example:
// decimalSeparators = ",#": 12,3 -> 12.3, 100.500 -> 100.500, 12#13 -> 12.13.
decimalSeparators string
// nullValue is a value that is interpreted as null.
nullValue string
}
// MakeStringToTTConvFactory creates StringToTTConvFactory.
func MakeStringToTTConvFactory() StringToTTConvFactory {
return StringToTTConvFactory{
thousandSeparators: defaultThousandSeparators,
decimalSeparators: defaultDecimalSeparators,
nullValue: defaultNullValue,
}
}
func (StringToTTConvFactory) GetBooleanConverter() Converter[string, any] {
return MakeStringToBoolConverter()
}
func (StringToTTConvFactory) GetStringConverter() Converter[string, any] {
return MakeIdentityConverter[string]()
}
func (fac StringToTTConvFactory) GetUnsignedConverter() Converter[string, any] {
return MakeStringToUIntConverter(fac.thousandSeparators)
}
func (StringToTTConvFactory) GetDatetimeConverter() Converter[string, any] {
return MakeStringToDatetimeConverter()
}
func (StringToTTConvFactory) GetUUIDConverter() Converter[string, any] {
return MakeStringToUUIDConverter()
}
func (StringToTTConvFactory) GetMapConverter() Converter[string, any] {
return MakeStringToMapConverter()
}
func (StringToTTConvFactory) GetArrayConverter() Converter[string, any] {
return MakeStringToSliceConverter()
}
func (StringToTTConvFactory) GetVarbinaryConverter() Converter[string, any] {
return MakeStringToBinaryConverter()
}
func (fac StringToTTConvFactory) GetDoubleConverter() Converter[string, any] {
return MakeStringToFloatConverter(fac.thousandSeparators, fac.decimalSeparators)
}
func (fac StringToTTConvFactory) GetDecimalConverter() Converter[string, any] {
return MakeStringToDecimalConverter(fac.thousandSeparators, fac.decimalSeparators)
}
func (fac StringToTTConvFactory) GetIntegerConverter() Converter[string, any] {
return MakeSequenceConverter([]Converter[string, any]{
MakeStringToUIntConverter(fac.thousandSeparators),
MakeStringToIntConverter(fac.thousandSeparators),
})
}
func (fac StringToTTConvFactory) GetNumberConverter() Converter[string, any] {
return MakeSequenceConverter([]Converter[string, any]{
MakeStringToUIntConverter(fac.thousandSeparators),
MakeStringToIntConverter(fac.thousandSeparators),
MakeStringToFloatConverter(fac.thousandSeparators, fac.decimalSeparators),
})
}
func (fac StringToTTConvFactory) GetIntervalConverter() Converter[string, any] {
return MakeStringToIntervalConverter()
}
func (fac StringToTTConvFactory) GetAnyConverter() Converter[string, any] {
return MakeSequenceConverter([]Converter[string, any]{
fac.GetNumberConverter(),
fac.GetDecimalConverter(),
fac.GetBooleanConverter(),
fac.GetDatetimeConverter(),
fac.GetUUIDConverter(),
fac.GetIntervalConverter(),
fac.GetStringConverter(),
})
}
func (fac StringToTTConvFactory) GetScalarConverter() Converter[string, any] {
return MakeSequenceConverter([]Converter[string, any]{
fac.GetNumberConverter(),
fac.GetDecimalConverter(),
fac.GetBooleanConverter(),
fac.GetDatetimeConverter(),
fac.GetUUIDConverter(),
fac.GetIntervalConverter(),
fac.GetStringConverter(),
})
}
func (fac StringToTTConvFactory) MakeNullableConverter(
converter Converter[string, any]) Converter[string, any] {
return MakeSequenceConverter([]Converter[string, any]{
MakeStringToNullConverter(fac.nullValue),
converter,
})
}
// WithNullValue sets nullValue.
func (fac StringToTTConvFactory) WithNullValue(nullValue string) StringToTTConvFactory {
fac.nullValue = nullValue
return fac
}
// WithThousandSeparators sets thousandSeparators.
func (fac StringToTTConvFactory) WithThousandSeparators(
separators string) StringToTTConvFactory {
fac.thousandSeparators = separators
return fac
}
// WithDecimalSeparators sets decimalSeparators.
func (fac StringToTTConvFactory) WithDecimalSeparators(separators string) StringToTTConvFactory {
fac.decimalSeparators = separators
return fac
}
var _ TTConvFactory[string] = (*StringToTTConvFactory)(nil)
// GetConverterByType returns a converter by TTConvFactory and typename.
func GetConverterByType[Type any](
fac TTConvFactory[Type], typ TypeName) (conv Converter[Type, any], err error) {
switch typ {
case TypeBoolean:
conv = fac.GetBooleanConverter()
case TypeString:
conv = fac.GetStringConverter()
case TypeUnsigned:
conv = fac.GetUnsignedConverter()
case TypeDatetime:
conv = fac.GetDatetimeConverter()
case TypeUUID:
conv = fac.GetUUIDConverter()
case TypeMap:
conv = fac.GetMapConverter()
case TypeArray:
conv = fac.GetArrayConverter()
case TypeVarbinary:
conv = fac.GetVarbinaryConverter()
case TypeDouble:
conv = fac.GetDoubleConverter()
case TypeDecimal:
conv = fac.GetDecimalConverter()
case TypeInteger:
conv = fac.GetIntegerConverter()
case TypeNumber:
conv = fac.GetNumberConverter()
case TypeAny:
conv = fac.GetAnyConverter()
case TypeScalar:
conv = fac.GetScalarConverter()
case TypeInterval:
conv = fac.GetIntervalConverter()
default:
return nil, fmt.Errorf("unexpected type: %s", typ)
}
return
}
// SpaceField is a space field.
type SpaceField struct {
Id uint32 `msgpack:"id,omitempty"`
Name string `msgpack:"name"`
Type TypeName `msgpack:"type"`
IsNullable bool `msgpack:"is_nullable,omitempty"`
}
// MakeTypeToTTConverters creates list of the converters
// from Type to tt type by the factory and space format.
func MakeTypeToTTConverters[Type any](
fac TTConvFactory[Type],
spaceFmt []SpaceField) ([]Converter[Type, any], error) {
converters := make([]Converter[Type, any], len(spaceFmt))
for i, fieldFmt := range spaceFmt {
typ := fieldFmt.Type
conv, err := GetConverterByType(fac, typ)
if err != nil {
return nil, err
}
if fieldFmt.IsNullable {
conv = fac.MakeNullableConverter(conv)
}
converters[i] = MakeFuncConverter(func(s Type) (any, error) {
result, err := conv.Convert(s)
if err != nil {
return nil, fmt.Errorf("unexpected value %v for type %q", s, typ)
}
return result, nil
})
}
return converters, nil
}