{"id":37122695,"url":"https://github.com/ggicci/strconvx","last_synced_at":"2026-01-14T14:10:47.459Z","repository":{"id":247147062,"uuid":"803536448","full_name":"ggicci/strconvx","owner":"ggicci","description":"🧣 A lightweight Go package that provides a unified interface for string conversion via ToString and FromString.","archived":false,"fork":false,"pushed_at":"2025-09-12T00:06:29.000Z","size":71,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-11-09T06:04:23.393Z","etag":null,"topics":["go-stringify","string-conversions"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ggicci.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yaml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"ggicci","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2024-05-20T23:08:43.000Z","updated_at":"2025-09-12T00:06:25.000Z","dependencies_parsed_at":"2024-11-21T08:18:43.957Z","dependency_job_id":"2e5ac357-3e12-41ba-87aa-49e51605f1fe","html_url":"https://github.com/ggicci/strconvx","commit_stats":null,"previous_names":["ggicci/stringable","ggicci/strconvx"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ggicci/strconvx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggicci%2Fstrconvx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggicci%2Fstrconvx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggicci%2Fstrconvx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggicci%2Fstrconvx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ggicci","download_url":"https://codeload.github.com/ggicci/strconvx/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggicci%2Fstrconvx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28422408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-stringify","string-conversions"],"created_at":"2026-01-14T14:10:46.662Z","updated_at":"2026-01-14T14:10:47.445Z","avatar_url":"https://github.com/ggicci.png","language":"Go","readme":"# strconvx\n\n**`strconvx`** is a small Go package that defines a unified interface for converting values to and from strings using `ToString` and `FromString`. It also supports wrapping existing types — including those implementing `encoding.TextMarshaler` or `fmt.Stringer` — into a consistent `StringCodec` interface.\n\n[![Go](https://github.com/ggicci/strconvx/actions/workflows/go.yaml/badge.svg)](https://github.com/ggicci/strconvx/actions/workflows/go.yaml)\n[![codecov](https://codecov.io/gh/ggicci/strconvx/graph/badge.svg?token=YU7FGGOY60)](https://codecov.io/gh/ggicci/strconvx)\n[![Go Report Card](https://goreportcard.com/badge/github.com/ggicci/strconvx)](https://goreportcard.com/report/github.com/ggicci/strconvx)\n[![Go Reference](https://pkg.go.dev/badge/github.com/ggicci/strconvx.svg)](https://pkg.go.dev/github.com/ggicci/strconvx)\n\n## ✨ Features\n\n- Unified `ToString` and `FromString` interfaces\n- Compatible with `encoding.TextMarshaler` / `TextUnmarshaler`\n- Dynamically wraps existing values with `strconvx.New(...)`\n- Support for most built-in Go scalar types (int, float, bool, etc.)\n- Easy to integrate into `flag.Value`, `envconfig`, `url.Values`, etc.\n\n## Basic API\n\n```go\nvar yesno bool\nsb, err := strconvx.New(\u0026yesno)\n\nsb.FromString(\"true\")\nsb.ToString()\n```\n\n## Supported Builtin Types\n\n- string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128\n- `time.Time`\n- `[]byte`\n\n## The Hybrid String Codec Instance\n\nWhen calling `strconvx.New(x)` with an instance `x` that is not a `StringCodec` itself, nor any of the above builtin types, it will try to create a _\"hybrid\" StringCodec instance_ from `x` for you.\n\nHere is how the \"hybrid\" StringCodec instance will be created:\n\n1. Create a hybrid instance `h` from the given instance `x`;\n2. If `x` has implemented one of [`strconvx.StringMarshaler`](https://pkg.go.dev/github.com/ggicci/strconvx#StringMarshaler) and [`encoding.TextMarshaler`](https://pkg.go.dev/encoding#TextMarshaler), `h` will use it as the implementation of `strconvx.StringMarshaler`, i.e. the `ToString()` method;\n3. If `x` has implemented one of [`strconvx.StringUnmarshaler`](https://pkg.go.dev/github.com/ggicci/strconvx#StringUnmarshaler) and [`encoding.TextUnmarshaler`](https://pkg.go.dev/encoding#TextUnmarshaler), `h` will use it as the implementation of `strconvx.StringUnmarshaler`, i.e. the `FromString()` method;\n4. As long as `h` has an implementation of either `strconvx.StringMarshaler` or `strconvx.StringUnmarshaler`, we consider `h` is a valid `StringCodec` instance. You can require both by passing in a [`CompleteHybrid()` option](#hybrid-options) to `New` method. For a valid `h`, `strconvx.New(x)` will return `h`. Otherwise, an `ErrUnsupportedType` occurs.\n\nExample:\n\n```go\ntype Location struct {\n\tX int\n\tY int\n}\n\nfunc (l *Location) MarshalText() ([]byte, error) {\n\treturn []byte(fmt.Sprintf(\"L(%d,%d)\", l.X, l.Y)), nil\n}\n\nloc := \u0026Location{3, 4}\nsb, err := strconvx.New(loc) // err is nil\n\nsb.ToString() // L(3,4)\nsb.FromString(\"L(5,6)\") // ErrNotStringUnmarshaler, \"not a StringUnmarshaler\"\n```\n\n### Hybrid Options\n\n1. `New(v, NoHybrid())`: prevent `New` from trying to create a hybrid instance from `v` at all. Instead, returns `ErrUnsupportedType`.\n2. `New(v, CompleteHybrid())`: still allow `New` trying to create a hybrid instance from `v` if necessary, but with the present of `CompleteHybrid()` option, the returned hybrid instance must have a valid implementation of both `FromString` and `ToString`.\n\n## Adapt/Override Existing Types\n\nThe [`Namespace.Adapt()`](https://pkg.go.dev/github.com/ggicci/strconvx#Namespace.Adapt) API is used to customize the behaviour of `strconvx.StringCodec` of a specific type. The principal is to create a **type alias** to the target type you want to override, and implement the `StringCodec` interface on the new type.\n\nWhen should you use this API?\n\n1. change the conversion logic of the builtin types.\n2. change the conversion logic of existing types that are \"hybridizable\", but you don't want to change their implementations.\n\nFor example, the default support of `bool` type in this package uses `strconv.ParseBool` method to convert strings like \"true\", \"TRUE\", \"f\", \"0\", etc. to a bool value. If you want to support also converting \"YES\", \"NO\", \"はい\" to a bool value, you can implement a custom bool type and register it to a `Namespace` instance:\n\n```go\ntype YesNo bool\n\nfunc (yn YesNo) ToString() (string, error) {\n\tif yn {\n\t\treturn \"yes\", nil\n\t} else {\n\t\treturn \"no\", nil\n\t}\n}\n\nfunc (yn *YesNo) FromString(s string) error {\n\tswitch strings.ToLower(s) {\n\tcase \"yes\":\n\t\t*yn = true\n\tcase \"no\":\n\t\t*yn = false\n\tdefault:\n\t\treturn errors.New(\"invalid value\")\n\t}\n\treturn nil\n}\n\nfunc main() {\n\tns := strconvx.NewNamespace()\n\ttyp, adaptor := ToAnyAdaptor(func(b *bool) (StringCodec, error) {\n\t\treturn (*YesNo)(b), nil\n\t})\n\tns.Adapt(typ, adaptor)\n\n\tvar yesno bool = true\n\tsb, err := ns.New(\u0026yesno)\n}\n```\n","funding_links":["https://github.com/sponsors/ggicci"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fggicci%2Fstrconvx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fggicci%2Fstrconvx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fggicci%2Fstrconvx/lists"}