@@ -23,6 +23,7 @@ func (c filterIntConverter) Convert(src string) (int64, error) {
23
23
return strconv .ParseInt (src , 10 , 64 )
24
24
}
25
25
26
+ // ExampleConverter demonstrates the basic usage of the Converter.
26
27
func ExampleConverter () {
27
28
// Basic converter.
28
29
strToBoolConv := tupleconv .MakeStringToBoolConverter ()
@@ -46,6 +47,7 @@ func ExampleConverter() {
46
47
// 100 <nil>
47
48
}
48
49
50
+ // ExampleMapper_basicMapper demonstrates the basic usage of the Mapper.
49
51
func ExampleMapper_basicMapper () {
50
52
// Mapper example.
51
53
mapper := tupleconv.MakeMapper [string , any ]([]tupleconv.Converter [string , any ]{
@@ -71,24 +73,25 @@ func ExampleMapper_basicMapper() {
71
73
// [01] <nil>
72
74
}
73
75
76
+ // ExampleMapper_singleMapper demonstrates the usage of the Mapper with
77
+ // only the default converter.
74
78
func ExampleMapper_singleMapper () {
75
79
// Single mapper example.
76
80
toStringMapper := tupleconv .MakeMapper ([]tupleconv.Converter [any , string ]{}).
77
81
WithDefaultConverter (tupleconv .MakeFuncConverter (
78
82
func (s any ) (string , error ) {
79
- return fmt .Sprintln (s ), nil
83
+ return fmt .Sprint (s ), nil
80
84
}),
81
85
)
82
86
res , err := toStringMapper .Map ([]any {1 , 2.5 , nil })
83
87
fmt .Println (res , err )
84
88
85
89
// Output:
86
- // [1
87
- // 2.5
88
- // <nil>
89
- //] <nil>
90
+ // [1 2.5 <nil>] <nil>
90
91
}
91
92
93
+ // ExampleStringToTTConvFactory demonstrates how to create Converter list for
94
+ // Mapper using helper functions and StringToTTConvFactory.
92
95
func ExampleStringToTTConvFactory () {
93
96
factory := tupleconv .MakeStringToTTConvFactory ().
94
97
WithDecimalSeparators (",." )
@@ -108,6 +111,8 @@ func ExampleStringToTTConvFactory() {
108
111
// [1 -2.2 some_string] <nil>
109
112
}
110
113
114
+ // ExampleStringToTTConvFactory_manualConverters demonstrates how to obtain Converter
115
+ // from TTConvFactory for manual Converter list construction.
111
116
func ExampleStringToTTConvFactory_manualConverters () {
112
117
factory := tupleconv .MakeStringToTTConvFactory ().
113
118
WithDecimalSeparators (",." )
@@ -132,6 +137,9 @@ func ExampleStringToTTConvFactory_manualConverters() {
132
137
// [1 -2.2 some_string] <nil>
133
138
}
134
139
140
+ // ExampleStringToTTConvFactory_convertNullable demonstrates an example of converting
141
+ // a nullable type: an attempt to convert to null will be made before attempting to convert to the
142
+ // main type.
135
143
func ExampleStringToTTConvFactory_convertNullable () {
136
144
factory := tupleconv .MakeStringToTTConvFactory ().
137
145
WithNullValue ("2.5" )
@@ -155,6 +163,8 @@ func (f *customFactory) MakeTypeToAnyMapper() tupleconv.Converter[string, any] {
155
163
})
156
164
}
157
165
166
+ // ExampleTTConvFactory_custom demonstrates how to customize the behavior of TTConvFactory
167
+ // by inheriting from it and overriding the necessary functions.
158
168
func ExampleTTConvFactory_custom () {
159
169
facture := & customFactory {}
160
170
spaceFmt := []tupleconv.SpaceField {{Type : tupleconv .TypeAny }}
@@ -192,6 +202,21 @@ func upTarantool() (func(), error) {
192
202
return cleanup , nil
193
203
}
194
204
205
+ func makeTtEncoder () func (any ) (string , error ) {
206
+ datetimeConverter := tupleconv .MakeDatetimeToStringConverter ()
207
+ return func (src any ) (string , error ) {
208
+ switch src := src .(type ) {
209
+ case datetime.Datetime :
210
+ return datetimeConverter .Convert (& src )
211
+ default :
212
+ return fmt .Sprint (src ), nil
213
+ }
214
+ }
215
+ }
216
+
217
+ // ExampleMap_insertMappedTuples demonstrates the combination of Mapper and go-tarantool
218
+ // functionality: firstly map string tuples to the tarantool types, then insert them to the
219
+ // target space.
195
220
func ExampleMap_insertMappedTuples () {
196
221
cleanupTarantool , err := upTarantool ()
197
222
if err != nil {
@@ -205,7 +230,12 @@ func ExampleMap_insertMappedTuples() {
205
230
Pass : "password" ,
206
231
})
207
232
var spaceFmtResp [][]tupleconv.SpaceField
208
- _ = conn .CallTyped ("get_test_space_fmt" , []any {}, & spaceFmtResp )
233
+ req := tarantool .NewCallRequest ("get_test_space_fmt" )
234
+ if err := conn .Do (req ).GetTyped (& spaceFmtResp ); err != nil {
235
+ fmt .Printf ("can't get target space fmt: %v\n " , err )
236
+ return
237
+ }
238
+
209
239
spaceFmt := spaceFmtResp [0 ]
210
240
fmt .Println (spaceFmt [0 :3 ])
211
241
@@ -251,14 +281,7 @@ func ExampleMap_insertMappedTuples() {
251
281
252
282
tuple0 , _ := resp .Data [0 ].([]any )
253
283
encoder := tupleconv.MakeMapper [any , string ]([]tupleconv.Converter [any , string ]{}).
254
- WithDefaultConverter (tupleconv .MakeFuncConverter (func (s any ) (string , error ) {
255
- asDatetime , isDatetime := s .(datetime.Datetime )
256
- if isDatetime {
257
- return fmt .Sprintln (asDatetime .ToTime ()), nil
258
- } else {
259
- return fmt .Sprintln (s ), nil
260
- }
261
- }))
284
+ WithDefaultConverter (tupleconv .MakeFuncConverter (makeTtEncoder ()))
262
285
263
286
encodedTuple0 , _ := encoder .Map (tuple0 )
264
287
fmt .Println (encodedTuple0 )
@@ -268,32 +291,11 @@ func ExampleMap_insertMappedTuples() {
268
291
// insert response code = 0
269
292
// insert response code = 0
270
293
// insert response code = 0
271
- // [1
272
- // true
273
- // 12
274
- // 143.5
275
- // 2020-08-22 11:27:43.123456789 -0200 -0200
276
- // <nil>
277
- // str
278
- // <nil>
279
- // [1 2 3]
280
- // 190
281
- // <nil>
282
- //]
283
- }
284
-
285
- func makeTtEncoder () func (any ) (string , error ) {
286
- datetimeConverter := tupleconv .MakeDatetimeToStringConverter ()
287
- return func (src any ) (string , error ) {
288
- switch src := src .(type ) {
289
- case datetime.Datetime :
290
- return datetimeConverter .Convert (& src )
291
- default :
292
- return fmt .Sprint (src ), nil
293
- }
294
- }
294
+ // [1 true 12 143.5 2020-08-22T11:27:43.123456789-0200 <nil> str <nil> [1 2 3] 190 <nil>]
295
295
}
296
296
297
+ // Example_ttEncoder demonstrates how to create an encoder, using Mapper with only a default
298
+ // Converter defined.
297
299
func Example_ttEncoder () {
298
300
cleanupTarantool , err := upTarantool ()
299
301
if err != nil {
0 commit comments