Documentation
¶
Overview ¶
Example (TtEncoder) ¶
Example_ttEncoder demonstrates how to create an encoder, using Mapper with only a default Converter defined.
cleanupTarantool, err := upTarantool() if err != nil { fmt.Println(err) return } defer cleanupTarantool() converter := tupleconv.MakeFuncConverter(makeTtEncoder()) tupleEncoder := tupleconv.MakeMapper([]tupleconv.Converter[any, string]{}). WithDefaultConverter(converter) conn, _ := tarantool.Connect(context.Background(), dialer, opts) defer conn.Close() req := tarantool.NewSelectRequest("finances") var tuples [][]any if err := conn.Do(req).GetTyped(&tuples); err != nil { fmt.Printf("can't select tuples: %v\n", err) return } for _, tuple := range tuples { encoded, err := tupleEncoder.Map(tuple) if err != nil { fmt.Printf("can't encode tuple: %v\n", err) return } fmt.Println(encoded) }
Output: [1 14.15 2023-08-30T12:13:00+0000] [2 193000 2023-08-31T00:00:00 Europe/Paris] [3 -111111 2023-09-02T00:01:00+0400]
Index ¶
- type Converter
- func GetConverterByType[Type any](fac TTConvFactory[Type], typ TypeName) (conv Converter[Type, any], err error)
- func MakeSequenceConverter[S any, T any](converters []Converter[S, T]) Converter[S, T]
- func MakeTypeToTTConverters[Type any](fac TTConvFactory[Type], spaceFmt []SpaceField) ([]Converter[Type, any], error)
- type DatetimeToStringConverter
- type FuncConverter
- type IdentityConverter
- type IntervalToStringConverter
- type Mapper
- type SpaceField
- type StringToBinaryConverter
- type StringToBoolConverter
- type StringToDatetimeConverter
- type StringToDecimalConverter
- type StringToFloatConverter
- type StringToIntConverter
- type StringToIntervalConverter
- type StringToMapConverter
- type StringToNullConverter
- type StringToSliceConverter
- type StringToTTConvFactory
- func (fac StringToTTConvFactory) GetAnyConverter() Converter[string, any]
- func (StringToTTConvFactory) GetArrayConverter() Converter[string, any]
- func (StringToTTConvFactory) GetBooleanConverter() Converter[string, any]
- func (StringToTTConvFactory) GetDatetimeConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetDecimalConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetDoubleConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetIntegerConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetIntervalConverter() Converter[string, any]
- func (StringToTTConvFactory) GetMapConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetNumberConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetScalarConverter() Converter[string, any]
- func (StringToTTConvFactory) GetStringConverter() Converter[string, any]
- func (StringToTTConvFactory) GetUUIDConverter() Converter[string, any]
- func (fac StringToTTConvFactory) GetUnsignedConverter() Converter[string, any]
- func (StringToTTConvFactory) GetVarbinaryConverter() Converter[string, any]
- func (fac StringToTTConvFactory) MakeNullableConverter(converter Converter[string, any]) Converter[string, any]
- func (fac StringToTTConvFactory) WithDecimalSeparators(separators string) StringToTTConvFactory
- func (fac StringToTTConvFactory) WithNullValue(nullValue string) StringToTTConvFactory
- func (fac StringToTTConvFactory) WithThousandSeparators(separators string) StringToTTConvFactory
- type StringToUIntConverter
- type StringToUUIDConverter
- type TTConvFactory
- type TypeName
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Converter ¶
Converter is a converter from S to T.
Example ¶
ExampleConverter demonstrates the basic usage of the Converter.
// Basic converter. strToBoolConv := tupleconv.MakeStringToBoolConverter() result, err := strToBoolConv.Convert("true") fmt.Println(result, err) // Function based converter. funcConv := tupleconv.MakeFuncConverter(func(s string) (string, error) { return s + " world!", nil }) result, err = funcConv.Convert("hello") fmt.Println(result, err) var filterConv tupleconv.Converter[string, int64] = filterIntConverter{toFilter: "th"} result, err = filterConv.Convert("100th") fmt.Println(result, err)
Output: true <nil> hello world! <nil> 100 <nil>
func GetConverterByType ¶
func GetConverterByType[Type any]( fac TTConvFactory[Type], typ TypeName) (conv Converter[Type, any], err error)
GetConverterByType returns a converter by TTConvFactory and typename.
func MakeSequenceConverter ¶
MakeSequenceConverter makes a sequential Converter from a Converter list.
func MakeTypeToTTConverters ¶
func MakeTypeToTTConverters[Type any]( fac TTConvFactory[Type], spaceFmt []SpaceField) ([]Converter[Type, any], error)
MakeTypeToTTConverters creates list of the converters from Type to tt type by the factory and space format.
type DatetimeToStringConverter ¶
type DatetimeToStringConverter struct{}
DatetimeToStringConverter is a converter from datetime.Datetime to string.
func MakeDatetimeToStringConverter ¶
func MakeDatetimeToStringConverter() DatetimeToStringConverter
MakeDatetimeToStringConverter creates DatetimeToStringConverter.
type FuncConverter ¶
FuncConverter is a function-based Converter.
func MakeFuncConverter ¶
func MakeFuncConverter[S any, T any](convFunc func(S) (T, error)) FuncConverter[S, T]
MakeFuncConverter creates FuncConverter.
func (FuncConverter[S, T]) Convert ¶
func (conv FuncConverter[S, T]) Convert(src S) (T, error)
Convert is the implementation of Converter for FuncConverter.
type IdentityConverter ¶
type IdentityConverter[S any] struct{}
IdentityConverter is a converter from S to any, that doesn't change the input.
func MakeIdentityConverter ¶
func MakeIdentityConverter[S any]() IdentityConverter[S]
MakeIdentityConverter creates IdentityConverter.
func (IdentityConverter[S]) Convert ¶
func (IdentityConverter[S]) Convert(src S) (any, error)
Convert is the implementation of Converter[S, any] for IdentityConverter.
type IntervalToStringConverter ¶
type IntervalToStringConverter struct{}
IntervalToStringConverter is a converter from datetime.Interval to string.
func MakeIntervalToStringConverter ¶
func MakeIntervalToStringConverter() IntervalToStringConverter
MakeIntervalToStringConverter creates IntervalToStringConverter.
type Mapper ¶
Mapper performs tuple mapping.
Example (BasicMapper) ¶
ExampleMapper_basicMapper demonstrates the basic usage of the Mapper.
// Mapper example. mapper := tupleconv.MakeMapper[string, any]([]tupleconv.Converter[string, any]{ tupleconv.MakeFuncConverter(func(s string) (any, error) { return s + "1", nil }), tupleconv.MakeFuncConverter(func(s string) (any, error) { iVal, err := strconv.Atoi(s) if err != nil { return nil, errors.New("can't convert") } return iVal + 1, nil }), }) result, err := mapper.Map([]string{"a", "4"}) fmt.Println(result, err) result, err = mapper.Map([]string{"0"}) fmt.Println(result, err)
Output: [a1 5] <nil> [01] <nil>
Example (SingleMapper) ¶
ExampleMapper_singleMapper demonstrates the usage of the Mapper with only the default converter.
// Single mapper example. toStringMapper := tupleconv.MakeMapper([]tupleconv.Converter[any, string]{}). WithDefaultConverter(tupleconv.MakeFuncConverter( func(s any) (string, error) { return fmt.Sprint(s), nil }), ) res, err := toStringMapper.Map([]any{1, 2.5, nil}) fmt.Println(res, err)
Output: [1 2.5 <nil>] <nil>
func MakeMapper ¶
MakeMapper creates Mapper.
func (Mapper[S, T]) WithDefaultConverter ¶
WithDefaultConverter sets defaultConverter.
type SpaceField ¶
type SpaceField struct { Id uint32 `msgpack:"id,omitempty"` Name string `msgpack:"name"` Type TypeName `msgpack:"type"` IsNullable bool `msgpack:"is_nullable,omitempty"` }
SpaceField is a space field.
type StringToBinaryConverter ¶
type StringToBinaryConverter struct{}
StringToBinaryConverter is a converter from string to binary.
func MakeStringToBinaryConverter ¶
func MakeStringToBinaryConverter() StringToBinaryConverter
MakeStringToBinaryConverter creates StringToBinaryConverter.
type StringToBoolConverter ¶
type StringToBoolConverter struct{}
StringToBoolConverter is a converter from string to bool.
func MakeStringToBoolConverter ¶
func MakeStringToBoolConverter() StringToBoolConverter
MakeStringToBoolConverter creates StringToBoolConverter.
type StringToDatetimeConverter ¶
type StringToDatetimeConverter struct{}
StringToDatetimeConverter is a converter from string to datetime.Datetime. Time formats used: - 2006-01-02T15:04:05.999999999-0700 - 2006-01-02T15:04:05.999999999 Europe/Moscow.
func MakeStringToDatetimeConverter ¶
func MakeStringToDatetimeConverter() StringToDatetimeConverter
MakeStringToDatetimeConverter creates StringToDatetimeConverter.
type StringToDecimalConverter ¶
type StringToDecimalConverter struct {
// contains filtered or unexported fields
}
StringToDecimalConverter is a converter from string to decimal.Decimal.
func MakeStringToDecimalConverter ¶
func MakeStringToDecimalConverter(ignoreChars, decSeparators string) StringToDecimalConverter
MakeStringToDecimalConverter creates StringToDecimalConverter.
type StringToFloatConverter ¶
type StringToFloatConverter struct {
// contains filtered or unexported fields
}
StringToFloatConverter is a converter from string to float64.
func MakeStringToFloatConverter ¶
func MakeStringToFloatConverter(ignoreChars, decSeparators string) StringToFloatConverter
MakeStringToFloatConverter creates StringToFloatConverter.
type StringToIntConverter ¶
type StringToIntConverter struct {
// contains filtered or unexported fields
}
StringToIntConverter is a converter from string to int64.
func MakeStringToIntConverter ¶
func MakeStringToIntConverter(ignoreChars string) StringToIntConverter
MakeStringToIntConverter creates StringToIntConverter.
type StringToIntervalConverter ¶
type StringToIntervalConverter struct{}
StringToIntervalConverter is a converter from string to datetime.Interval.
func MakeStringToIntervalConverter ¶
func MakeStringToIntervalConverter() StringToIntervalConverter
MakeStringToIntervalConverter creates StringToIntervalConverter.
type StringToMapConverter ¶
type StringToMapConverter struct{}
StringToMapConverter is a converter from string to map. Only `json` is supported now.
func MakeStringToMapConverter ¶
func MakeStringToMapConverter() StringToMapConverter
MakeStringToMapConverter creates StringToMapConverter.
type StringToNullConverter ¶
type StringToNullConverter struct {
// contains filtered or unexported fields
}
StringToNullConverter is a converter from string to nil.
func MakeStringToNullConverter ¶
func MakeStringToNullConverter(nullValue string) StringToNullConverter
MakeStringToNullConverter creates StringToNullConverter.
type StringToSliceConverter ¶
type StringToSliceConverter struct{}
StringToSliceConverter is a converter from string to slice. Only `json` is supported now.
func MakeStringToSliceConverter ¶
func MakeStringToSliceConverter() StringToSliceConverter
MakeStringToSliceConverter creates StringToSliceConverter.
type StringToTTConvFactory ¶
type StringToTTConvFactory struct {
// contains filtered or unexported fields
}
StringToTTConvFactory is the default TypeToTTConvFactory for strings. To customize the creation of converters, inherit from it and override the necessary methods.
Example ¶
ExampleStringToTTConvFactory demonstrates how to create Converter list for Mapper using helper functions and StringToTTConvFactory.
factory := tupleconv.MakeStringToTTConvFactory(). WithDecimalSeparators(",.") spaceFmt := []tupleconv.SpaceField{ {Type: tupleconv.TypeUnsigned}, {Type: tupleconv.TypeDouble, IsNullable: true}, {Type: tupleconv.TypeString}, } converters, _ := tupleconv.MakeTypeToTTConverters[string](factory, spaceFmt) mapper := tupleconv.MakeMapper(converters) result, err := mapper.Map([]string{"1", "-2,2", "some_string"}) fmt.Println(result, err)
Output: [1 -2.2 some_string] <nil>
Example (ConvertNullable) ¶
ExampleStringToTTConvFactory_convertNullable demonstrates an example of converting a nullable type: an attempt to convert to null will be made before attempting to convert to the main type.
factory := tupleconv.MakeStringToTTConvFactory(). WithNullValue("2.5") converters, _ := tupleconv.MakeTypeToTTConverters[string](factory, []tupleconv.SpaceField{ {Type: tupleconv.TypeDouble, IsNullable: true}, }) fmt.Println(converters[0].Convert("2.5"))
Output: <nil> <nil>
Example (ManualConverters) ¶
ExampleStringToTTConvFactory_manualConverters demonstrates how to obtain Converter from TTConvFactory for manual Converter list construction.
factory := tupleconv.MakeStringToTTConvFactory(). WithDecimalSeparators(",.") fieldTypes := []tupleconv.TypeName{ tupleconv.TypeUnsigned, tupleconv.TypeDouble, tupleconv.TypeString, } converters := make([]tupleconv.Converter[string, any], 0) for _, typ := range fieldTypes { conv, _ := tupleconv.GetConverterByType[string](factory, typ) converters = append(converters, conv) } mapper := tupleconv.MakeMapper(converters) result, err := mapper.Map([]string{"1", "-2,2", "some_string"}) fmt.Println(result, err)
Output: [1 -2.2 some_string] <nil>
func MakeStringToTTConvFactory ¶
func MakeStringToTTConvFactory() StringToTTConvFactory
MakeStringToTTConvFactory creates StringToTTConvFactory.
func (StringToTTConvFactory) GetAnyConverter ¶
func (fac StringToTTConvFactory) GetAnyConverter() Converter[string, any]
func (StringToTTConvFactory) GetArrayConverter ¶
func (StringToTTConvFactory) GetArrayConverter() Converter[string, any]
func (StringToTTConvFactory) GetBooleanConverter ¶
func (StringToTTConvFactory) GetBooleanConverter() Converter[string, any]
func (StringToTTConvFactory) GetDatetimeConverter ¶
func (StringToTTConvFactory) GetDatetimeConverter() Converter[string, any]
func (StringToTTConvFactory) GetDecimalConverter ¶
func (fac StringToTTConvFactory) GetDecimalConverter() Converter[string, any]
func (StringToTTConvFactory) GetDoubleConverter ¶
func (fac StringToTTConvFactory) GetDoubleConverter() Converter[string, any]
func (StringToTTConvFactory) GetIntegerConverter ¶
func (fac StringToTTConvFactory) GetIntegerConverter() Converter[string, any]
func (StringToTTConvFactory) GetIntervalConverter ¶
func (fac StringToTTConvFactory) GetIntervalConverter() Converter[string, any]
func (StringToTTConvFactory) GetMapConverter ¶
func (StringToTTConvFactory) GetMapConverter() Converter[string, any]
func (StringToTTConvFactory) GetNumberConverter ¶
func (fac StringToTTConvFactory) GetNumberConverter() Converter[string, any]
func (StringToTTConvFactory) GetScalarConverter ¶
func (fac StringToTTConvFactory) GetScalarConverter() Converter[string, any]
func (StringToTTConvFactory) GetStringConverter ¶
func (StringToTTConvFactory) GetStringConverter() Converter[string, any]
func (StringToTTConvFactory) GetUUIDConverter ¶
func (StringToTTConvFactory) GetUUIDConverter() Converter[string, any]
func (StringToTTConvFactory) GetUnsignedConverter ¶
func (fac StringToTTConvFactory) GetUnsignedConverter() Converter[string, any]
func (StringToTTConvFactory) GetVarbinaryConverter ¶
func (StringToTTConvFactory) GetVarbinaryConverter() Converter[string, any]
func (StringToTTConvFactory) MakeNullableConverter ¶
func (StringToTTConvFactory) WithDecimalSeparators ¶
func (fac StringToTTConvFactory) WithDecimalSeparators(separators string) StringToTTConvFactory
WithDecimalSeparators sets decimalSeparators.
func (StringToTTConvFactory) WithNullValue ¶
func (fac StringToTTConvFactory) WithNullValue(nullValue string) StringToTTConvFactory
WithNullValue sets nullValue.
func (StringToTTConvFactory) WithThousandSeparators ¶
func (fac StringToTTConvFactory) WithThousandSeparators( separators string) StringToTTConvFactory
WithThousandSeparators sets thousandSeparators.
type StringToUIntConverter ¶
type StringToUIntConverter struct {
// contains filtered or unexported fields
}
StringToUIntConverter is a converter from string to uint64.
func MakeStringToUIntConverter ¶
func MakeStringToUIntConverter(ignoreChars string) StringToUIntConverter
MakeStringToUIntConverter creates StringToUIntConverter.
type StringToUUIDConverter ¶
type StringToUUIDConverter struct{}
StringToUUIDConverter is a converter from string to UUID.
func MakeStringToUUIDConverter ¶
func MakeStringToUUIDConverter() StringToUUIDConverter
MakeStringToUUIDConverter creates StringToUUIDConverter.
type TTConvFactory ¶
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] }
TTConvFactory is a factory capable of creating converters from Type to tarantool types.
Example (Custom) ¶
ExampleTTConvFactory_custom demonstrates how to customize the behavior of TTConvFactory by inheriting from it and overriding the necessary functions.
facture := &customFactory{} spaceFmt := []tupleconv.SpaceField{{Type: tupleconv.TypeAny}} converters, _ := tupleconv.MakeTypeToTTConverters[string](facture, spaceFmt) res, err := converters[0].Convert("12") fmt.Println(res, err)
Output: 12 <nil>
type TypeName ¶
type TypeName string
TypeName is the data type for type names.
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" )
Types are supported tarantool types.