{"id":20564446,"url":"https://github.com/tarantool/go-tupleconv","last_synced_at":"2025-03-06T08:27:10.359Z","repository":{"id":184173351,"uuid":"670279078","full_name":"tarantool/go-tupleconv","owner":"tarantool","description":"Tarantool tuples converter","archived":false,"fork":false,"pushed_at":"2024-10-09T12:12:10.000Z","size":40,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-01-16T19:44:24.407Z","etag":null,"topics":["go","tarantool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tarantool.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-24T17:30:01.000Z","updated_at":"2024-10-09T12:10:28.000Z","dependencies_parsed_at":"2023-11-01T13:39:40.200Z","dependency_job_id":null,"html_url":"https://github.com/tarantool/go-tupleconv","commit_stats":null,"previous_names":["tarantool/go-tupleconv"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool%2Fgo-tupleconv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool%2Fgo-tupleconv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool%2Fgo-tupleconv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarantool%2Fgo-tupleconv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tarantool","download_url":"https://codeload.github.com/tarantool/go-tupleconv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242174079,"owners_count":20084126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","tarantool"],"created_at":"2024-11-16T04:26:46.691Z","updated_at":"2025-03-06T08:27:10.306Z","avatar_url":"https://github.com/tarantool.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tarantool tuples converter in Go\n\n[![Go Reference][godoc-badge]][godoc-url]\n[![Actions Status][actions-badge]][actions-url]\n[![Code Coverage][coverage-badge]][coverage-url]\n\n## Table of contents\n* [Documentation](#documentation)\n  * [Converter](#converter)\n  * [Mapper](#mapper)\n  * [Mappers to tarantool types](#mappers-to-tarantool-types)\n    * [Example](#example)\n    * [String to nullable](#string-to-nullable)\n    * [String to any/scalar](#string-to-anyscalar)\n    * [Customization](#customization)\n## Documentation\n\n### Converter\n`Converter[S,T]` converts objects of type `S` into objects of type `T`. Converters\nare basic entities on which mappers are based.  \nImplementations of some converters are available, for example, converters\nfrom strings to golang types.   \nUsage example:\n```golang\n// Basic converter.\nstrToBoolConv := tupleconv.MakeStringToBoolConverter()\nresult, err := strToBoolConv.Convert(\"true\") // true \u003cnil\u003e\n\n// Function based converter.\nfuncConv := tupleconv.MakeFuncConverter(func(s string) (string, error) {\n    return s + \" world!\", nil\n})\nresult, err = funcConv.Convert(\"hello\") // hello world! \u003cnil\u003e\n```\n**Note 1**: You can use the provided converters.\n\n**Note 2**: You can create your own converters based on the functions \nwith `tupleconv.MakeFuncConverter`.\n\n**Note 3**: You can create your own converters, implementing\n`Converter[S,T]` interface.\n\n### Mapper\n`Mapper` is an object that converts tuples. It is built using a list of \nconverters.  \nUsage example:\n```golang\n// Mapper example.\nmapper := tupleconv.MakeMapper[string, any]([]tupleconv.Converter[string, any]{\n    tupleconv.MakeFuncConverter(func(s string) (any, error) {\n        return s + \"1\", nil\n    }),\n    tupleconv.MakeFuncConverter(func(s string) (any, error) {\n        iVal, err := strconv.Atoi(s)\n        if err != nil {\n            return nil, errors.New(\"can't convert\")\n        }\n\treturn iVal + 1, nil\n    }),\n})\nresult, err := mapper.Map([]string{\"a\", \"4\"}) // []any{\"a1\", 5} \u003cnil\u003e\nresult, err = mapper.Map([]string{\"0\"}) // []any{\"01\"} \u003cnil\u003e\n```\n```golang\n// Single mapper example.\ntoStringMapper := tupleconv.MakeMapper([]tupleconv.Converter[any, string]{}).\n        WithDefaultConverter(tupleconv.MakeFuncConverter(\n            func(s any) (string, error) {\n                return fmt.Sprintln(s), nil\n        }),\n)\nres, err := toStringMapper.Map([]any{1, 2.5, nil}) // [\"1\\n\", \"2.5\\n\", \"\u003cnil\u003e\\n\"] \u003cnil\u003e\n```\n**Note 1**: To create a mapper, an array of converters is needed, each\nof which transforms a certain type S into type T.   \n\n**Note 2**: To perform tuple mapping, you can use the function \n`Map`, which will return control to the calling code upon the first error.  \n\n**Note 3**: You can set a default converter that will be applied if the tuple length exceeds\nthe size of the primary converters list.   \nFor example, if you only set a default converter, `Map` will work like the `map` function in\nfunctional programming languages.\n\n**Note 4**: If tuple length is less than converters list length, then only corresponding converters\nwill be applied.\n\n### Mappers to tarantool types\n\n#### Example\nFor building an array of converters, especially when it comes to conversions to \ntarantool types, there is a built-in solution.  \nLet's consider an example:\n```golang\nfactory := tupleconv.MakeStringToTTConvFactory().\n                WithDecimalSeparators(\",.\")\n\nspaceFmt := []tupleconv.SpaceField{\n    {Type: tupleconv.TypeUnsigned},\n    {Type: tupleconv.TypeDouble, IsNullable: true},\n    {Type: tupleconv.TypeString},\n}\n\nconverters, _ := tupleconv.MakeTypeToTTConverters[string](factory, spaceFmt)\nmapper := tupleconv.MakeMapper(converters)\nresult, err := mapper.Map([]string{\"1\", \"-2,2\", \"some_string\"}) // [1, -2.2, \"some_string\"] \u003cnil\u003e\n```\n**Note 1**: To build an array of converters, the space format and a \ncertain object implementing `TTConvFactory` are used. Function \n`MakeTypeToTTConverters` takes these entities and gives the converters list.\n\n**Note 2**: `TTConvFactory[Type]` is capable of building a \nconverter from `Type` to each tarantool type.\n\n**Note 3**: There is a basic factory available called \n`StringToTTConvFactory`, which is used for conversions from strings to \ntarantool types.   \n\n**Note 4**: `StringToTTConvFactory` can be configured with options like\n`WithDecimalSeparators`.\n\n#### String to nullable\nWhen converting nullable types with `StringToTTConvFactory`, first, an attempt\nis made to convert to null.\n\nFor example, empty string is interpreted like `null` with default options.\nIf a field has a `string` type and is `nullable`, then an empty string will be\nconverted to null during the conversion process, rather than being\nconverted to empty string.\n\n\n#### String to any/scalar\nWhen converting to `any`/`scalar` with `StringToTTConvFactory`, by default, \nan attempt will be made to convert them to the following types, \nin the following order:\n- `number`\n- `decimal`\n- `boolean`\n- `datetime`\n- `uuid`\n- `interval`\n- `string`\n\n#### Customization\n`TTConvFactory[Type]` is an interface that can build a mapper from \n`Type` to each tarantool type.   \nTo customize the behavior for specific types, one can \ninherit from the existing factory and override the necessary methods.  \nFor example, let's make the standard factory for conversion from strings to \ntarantool types always convert `any` type to a string:\n```golang\ntype customFactory struct {\n    tupleconv.StringToTTConvFactory\n}\n\nfunc (f *customFactory) MakeTypeToAnyMapper() tupleconv.Converter[string, any] {\n    return tupleconv.MakeFuncConverter(func(s string) (any, error) {\n        return s, nil\n    })\n}\n\nfunc example() {\n    factory := \u0026customFactory{}\n    spaceFmt := []tupleconv.SpaceField{{Type: \"any\"}}\n    converters, _ := tupleconv.MakeTypeToTTConverters[string](factory, spaceFmt)\n\n    res, err := converters[0].Convert(\"12\") // \"12\" \u003cnil\u003e\n}\n```\n\n[godoc-badge]: https://pkg.go.dev/badge/github.com/tarantool/go-tupleconv.svg\n[godoc-url]: https://pkg.go.dev/github.com/tarantool/go-tupleconv\n[actions-badge]: https://github.com/tarantool/go-tupleconv/actions/workflows/test.yml/badge.svg\n[actions-url]: https://github.com/tarantool/go-tupleconv/actions/workflows/test.yml\n[coverage-badge]: https://coveralls.io/repos/github/tarantool/go-tupleconv/badge.svg?branch=master\n[coverage-url]: https://coveralls.io/github/tarantool/go-tupleconv?branch=master","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarantool%2Fgo-tupleconv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarantool%2Fgo-tupleconv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarantool%2Fgo-tupleconv/lists"}