https://github.com/go-pogo/rawconv
Package rawconv contains everything needed to create (custom) types which can unmarshal raw string values into any type in Go.
https://github.com/go-pogo/rawconv
decode encode go golang marshal parse parser types unmarshal
Last synced: 2 months ago
JSON representation
Package rawconv contains everything needed to create (custom) types which can unmarshal raw string values into any type in Go.
- Host: GitHub
- URL: https://github.com/go-pogo/rawconv
- Owner: go-pogo
- License: bsd-3-clause
- Created: 2022-08-29T17:31:48.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-10-01T11:36:15.000Z (9 months ago)
- Last Synced: 2026-01-14T13:43:03.402Z (5 months ago)
- Topics: decode, encode, go, golang, marshal, parse, parser, types, unmarshal
- Language: Go
- Homepage:
- Size: 188 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
rawconv
=======
[![Latest release][latest-release-img]][latest-release-url]
[![Build status][build-status-img]][build-status-url]
[![Go Report Card][report-img]][report-url]
[![Documentation][doc-img]][doc-url]
[latest-release-img]: https://img.shields.io/github/release/go-pogo/rawconv.svg?label=latest
[latest-release-url]: https://github.com/go-pogo/rawconv/releases
[build-status-img]: https://github.com/go-pogo/rawconv/actions/workflows/test.yml/badge.svg
[build-status-url]: https://github.com/go-pogo/rawconv/actions/workflows/test.yml
[report-img]: https://goreportcard.com/badge/github.com/go-pogo/rawconv
[report-url]: https://goreportcard.com/report/github.com/go-pogo/rawconv
[doc-img]: https://godoc.org/github.com/go-pogo/rawconv?status.svg
[doc-url]: https://pkg.go.dev/github.com/go-pogo/rawconv
Package `rawconv` implements conversions to and from raw string representations of any (custom) data type in Go.
Included features are:
- Convert from raw string to out of the box supported types, and vice versa:
* `string`, `rune`
* `bool`
* `int`, `int8`, `int16`, `int32`, `int64`
* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
* `float32`, `float64`
* `complex64`, `complex128`
* `array`, `slice`
* `map`
* `time.Duration`
* `url.URL`
* `encoding.TextUnmarshaler`, `encoding.TextMarshaler`
- Globally add support for your own custom types
- Or isolate support for your own custom types via `Marshaler` and `Unmarshaler` instances
```sh
go get github.com/go-pogo/rawconv
```
```go
import "github.com/go-pogo/rawconv"
```
## Usage
Below example demonstrates how to unmarshal a raw `string` into a `time.Duration` type using `Unmarshal`.
```go
package main
import (
"fmt"
"time"
"github.com/go-pogo/rawconv"
)
func main() {
var duration time.Duration
if err := rawconv.Unmarshal("1h2m3s", &duration); err != nil {
panic(err)
}
fmt.Println(duration)
// Output: 1h2m3s
}
```
### Array, slice and map conversions
Conversions to `array`, `slice` or `map` are done by splitting the raw string. The separator can be set via the
`Options` type and defaults to `DefaultItemsSeparator`. For maps there is also a separator for the key-value pairs,
which defaults to `DefaultKeyValueSeparator`.
Values within the `array`, `slice`, or `map` are unmarshaled using the called `Unmarshaler`. This is also done for keys
of maps.
```go
package main
import (
"fmt"
"reflect"
"github.com/go-pogo/rawconv"
)
func main() {
var u rawconv.Unmarshaler
u.ItemsSeparator = ";"
var list []string
if err := u.Unmarshal("foo;bar", reflect.ValueOf(&list)); err != nil {
panic(err)
}
fmt.Println(list)
// Output: [foo bar]
}
```
> Nested arrays are currently not supported.
### Structs
This package does not contain any logic for traversing `struct` types, because the implementation would really depend
on the use case. However, it is possible to incorporate this package in your own struct unmarshaling logic.
### Custom types
Custom types are supported in two ways; by implementing the `encoding.TextUnmarshaler` and/or `encoding.TextMarshaler`
interfaces, or by registering a `MarshalFunc` with `RegisterMarshalFunc` and/or an `UnmarshalFunc` with
`RegisterUnmarshalFunc`.
If you do not wish to globally expose your `MarshalFunc` or`UnmarshalFunc` implementations, it is possible to register
them to a new `Marshaler` or `Unmarshaler` and use those instances in your application instead.
```go
package main
import (
"reflect"
"github.com/davecgh/go-spew/spew"
"github.com/go-pogo/rawconv"
)
func main() {
type myType struct {
something string
}
var u rawconv.Unmarshaler
u.Register(reflect.TypeOf(myType{}), func(val rawconv.Value, dest any) error {
mt := dest.(*myType)
mt.something = val.String()
return nil
})
var target myType
if err := u.Unmarshal("some value", reflect.ValueOf(&target)); err != nil {
panic(err)
}
spew.Dump(target)
// Output:
// (rawconv.myType) {
// something: (string) (len=10) "some value"
// }
}
```
## Documentation
Additional detailed documentation is available at [pkg.go.dev][doc-url]
## Created with
## License
Copyright © 2022-2025 [Roel Schut](https://roelschut.nl). All rights reserved.
This project is governed by a BSD-style license that can be found in the [LICENSE](LICENSE) file.