Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Mar 20, 2024
1 parent 78fe9ec commit f00116c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rawconv
[doc-url]: https://pkg.go.dev/github.com/go-pogo/rawconv


Package `rawconv` implements conversions to and from raw string representations
Package `rawconv` implements conversions to and from raw string representations
of any (custom) data types in Go.

```sh
Expand All @@ -33,25 +33,49 @@ go get github.com/go-pogo/rawconv
import "github.com/go-pogo/rawconv"
```

## Basic conversions
## Key features

- Convert from raw string to out of the box supported types:
* `string`
* `bool`
* `int`, `int8`, `int16`, `int32`, `int64`
* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
* `float32`, `float64`
* `complex64`, `complex128`
* `time.Duration`
* `url.URL`
* `encoding.TextUnmarshaler`
- Globally add support for your own custom types
- Or isolate support for your own custom types via `Marshaler` and `Unmarshaler` instances

Basic conversions are done using the `strconv` package and are implemented as
methods on the `Value` type. The following conversions are supported:
- `string`
- `bool`
- `int`, `int8`, `int16`, `int32`, `int64`
- `uint`, `uint8`, `uint16`, `uint32`, `uint64`
- `float32`, `float64`
- `complex64`, `complex128`
- `time.Duration`
- `url.URL`
```go
var duration time.Duration
if err := rawconv.Unmarshal("1h2m3s", &duration); err != nil {
return fmt.Errorf("invalid duration: %w", err)
}
```

## 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.
> Nested arrays, slices and maps are 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

Conversions for global custom types are done by registering a `MarshalFunc` and/or
`UnmarshalFunc` using the `RegisterMarshalFunc` and `RegisterUnmarshalFunc` functions.
It is also possible to use `Marshaler` and/or `Unmarshaler` if you do not want to
expose the `MarshalFunc` or `UnmarshalFunc` implementations.
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.

## Documentation

Expand Down
23 changes: 19 additions & 4 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Copyright (c) 2022, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand All @@ -8,20 +8,35 @@ of any (custom) data type in Go.
# Basic conversions
Basic conversions are done using the strconv package. The following conversions
are supported by default:
Out of the box, this package supports all logical base types and some common types:
- string
- bool
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- complex64, complex128
- array, slice
- map
- time.Duration
- url.URL
These types are also implemented as methods on the Value type.
- encoding.TextUnmarshaler
# 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.
> Nested arrays, slices and maps are 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
Expand Down

0 comments on commit f00116c

Please sign in to comment.