Skip to content

Commit 7f201df

Browse files
committed
examples: add comments, make the output more readable
1 parent 699db88 commit 7f201df

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

example_test.go

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (c filterIntConverter) Convert(src string) (int64, error) {
2323
return strconv.ParseInt(src, 10, 64)
2424
}
2525

26+
// ExampleConverter demonstrates the basic usage of the Converter.
2627
func ExampleConverter() {
2728
// Basic converter.
2829
strToBoolConv := tupleconv.MakeStringToBoolConverter()
@@ -46,6 +47,7 @@ func ExampleConverter() {
4647
// 100 <nil>
4748
}
4849

50+
// ExampleMapper_basicMapper demonstrates the basic usage of the Mapper.
4951
func ExampleMapper_basicMapper() {
5052
// Mapper example.
5153
mapper := tupleconv.MakeMapper[string, any]([]tupleconv.Converter[string, any]{
@@ -71,24 +73,25 @@ func ExampleMapper_basicMapper() {
7173
// [01] <nil>
7274
}
7375

76+
// ExampleMapper_singleMapper demonstrates the usage of the Mapper with
77+
// only the default converter.
7478
func ExampleMapper_singleMapper() {
7579
// Single mapper example.
7680
toStringMapper := tupleconv.MakeMapper([]tupleconv.Converter[any, string]{}).
7781
WithDefaultConverter(tupleconv.MakeFuncConverter(
7882
func(s any) (string, error) {
79-
return fmt.Sprintln(s), nil
83+
return fmt.Sprint(s), nil
8084
}),
8185
)
8286
res, err := toStringMapper.Map([]any{1, 2.5, nil})
8387
fmt.Println(res, err)
8488

8589
// Output:
86-
// [1
87-
// 2.5
88-
// <nil>
89-
//] <nil>
90+
// [1 2.5 <nil>] <nil>
9091
}
9192

93+
// ExampleStringToTTConvFactory demonstrates how to create Converter list for
94+
// Mapper using helper functions and StringToTTConvFactory.
9295
func ExampleStringToTTConvFactory() {
9396
factory := tupleconv.MakeStringToTTConvFactory().
9497
WithDecimalSeparators(",.")
@@ -108,6 +111,8 @@ func ExampleStringToTTConvFactory() {
108111
// [1 -2.2 some_string] <nil>
109112
}
110113

114+
// ExampleStringToTTConvFactory_manualConverters demonstrates how to obtain Converter
115+
// from TTConvFactory for manual Converter list construction.
111116
func ExampleStringToTTConvFactory_manualConverters() {
112117
factory := tupleconv.MakeStringToTTConvFactory().
113118
WithDecimalSeparators(",.")
@@ -132,6 +137,9 @@ func ExampleStringToTTConvFactory_manualConverters() {
132137
// [1 -2.2 some_string] <nil>
133138
}
134139

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.
135143
func ExampleStringToTTConvFactory_convertNullable() {
136144
factory := tupleconv.MakeStringToTTConvFactory().
137145
WithNullValue("2.5")
@@ -155,6 +163,8 @@ func (f *customFactory) MakeTypeToAnyMapper() tupleconv.Converter[string, any] {
155163
})
156164
}
157165

166+
// ExampleTTConvFactory_custom demonstrates how to customize the behavior of TTConvFactory
167+
// by inheriting from it and overriding the necessary functions.
158168
func ExampleTTConvFactory_custom() {
159169
facture := &customFactory{}
160170
spaceFmt := []tupleconv.SpaceField{{Type: tupleconv.TypeAny}}
@@ -192,6 +202,21 @@ func upTarantool() (func(), error) {
192202
return cleanup, nil
193203
}
194204

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.
195220
func ExampleMap_insertMappedTuples() {
196221
cleanupTarantool, err := upTarantool()
197222
if err != nil {
@@ -205,7 +230,12 @@ func ExampleMap_insertMappedTuples() {
205230
Pass: "password",
206231
})
207232
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+
209239
spaceFmt := spaceFmtResp[0]
210240
fmt.Println(spaceFmt[0:3])
211241

@@ -251,14 +281,7 @@ func ExampleMap_insertMappedTuples() {
251281

252282
tuple0, _ := resp.Data[0].([]any)
253283
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()))
262285

263286
encodedTuple0, _ := encoder.Map(tuple0)
264287
fmt.Println(encodedTuple0)
@@ -268,32 +291,11 @@ func ExampleMap_insertMappedTuples() {
268291
// insert response code = 0
269292
// insert response code = 0
270293
// 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>]
295295
}
296296

297+
// Example_ttEncoder demonstrates how to create an encoder, using Mapper with only a default
298+
// Converter defined.
297299
func Example_ttEncoder() {
298300
cleanupTarantool, err := upTarantool()
299301
if err != nil {

0 commit comments

Comments
 (0)