Skip to content

Package rawconv contains everything needed to create (custom) types which can unmarshal raw string values into any type in Go.

License

Notifications You must be signed in to change notification settings

go-pogo/rawconv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rawconv

Latest release Build status Go Report Card Documentation

Package rawconv implements conversions to and from raw string representations of any (custom) data types 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

go get github.com/go-pogo/rawconv
import "github.com/go-pogo/rawconv"

Usage

Below example demonstrates how to unmarshal a raw string into a time.Duration type using Unmarshal.

package main

import (
    "fmt"
    "github.com/go-pogo/rawconv"
    "time"
)

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.

package main

import (
    "fmt"
    "github.com/go-pogo/rawconv"
    "reflect"
)

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, 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 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 orUnmarshalFunc implementations, it is possible to register them to a new Marshaler or Unmarshaler and use those instances in your application instead.

package main

import (
    "github.com/davecgh/go-spew/spew"
    "github.com/go-pogo/rawconv"
    "reflect"
)

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

Created with

License

Copyright © 2022-2024 Roel Schut. All rights reserved.

This project is governed by a BSD-style license that can be found in the LICENSE file.

About

Package rawconv contains everything needed to create (custom) types which can unmarshal raw string values into any type in Go.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages