Documentation
¶
Overview ¶
Package rawconv implements conversions to and from raw string representations of any (custom) data type in Go.
Basic conversions ¶
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
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.
Index ¶
- Constants
- func RegisterMarshalFunc(typ reflect.Type, fn MarshalFunc)
- func RegisterUnmarshalFunc(typ reflect.Type, fn UnmarshalFunc)
- func Unmarshal(val Value, v any) error
- type MarshalFunc
- type Marshaler
- type UnmarshalFunc
- type Unmarshaler
- type UnsupportedTypeError
- type Value
- func (v Value) Bool() (bool, error)
- func (v Value) BoolVar(p *bool) (err error)
- func (v Value) Bytes() []byte
- func (v Value) BytesVar(p *[]byte)
- func (v Value) Complex128() (complex128, error)
- func (v Value) Complex128Var(p *complex128) (err error)
- func (v Value) Complex64() (complex64, error)
- func (v Value) Complex64Var(p *complex64) (err error)
- func (v Value) Duration() (time.Duration, error)
- func (v Value) DurationVar(p *time.Duration) (err error)
- func (v Value) Float32() (float32, error)
- func (v Value) Float32Var(p *float32) (err error)
- func (v Value) Float64() (float64, error)
- func (v Value) Float64Var(p *float64) (err error)
- func (v Value) GoString() string
- func (v Value) Int() (int, error)
- func (v Value) Int16() (int16, error)
- func (v Value) Int16Var(p *int16) (err error)
- func (v Value) Int32() (int32, error)
- func (v Value) Int32Var(p *int32) (err error)
- func (v Value) Int64() (int64, error)
- func (v Value) Int64Var(p *int64) (err error)
- func (v Value) Int8() (int8, error)
- func (v Value) Int8Var(p *int8) (err error)
- func (v Value) IntVar(p *int) (err error)
- func (v Value) IsEmpty() bool
- func (v Value) String() string
- func (v Value) StringVar(p *string)
- func (v Value) Uint() (uint, error)
- func (v Value) Uint16() (uint16, error)
- func (v Value) Uint16Var(p *uint16) (err error)
- func (v Value) Uint32() (uint32, error)
- func (v Value) Uint32Var(p *uint32) (err error)
- func (v Value) Uint64() (uint64, error)
- func (v Value) Uint64Var(p *uint64) (err error)
- func (v Value) Uint8() (uint8, error)
- func (v Value) Uint8Var(p *uint8) (err error)
- func (v Value) UintVar(p *uint) (err error)
- func (v Value) Url() (*url.URL, error)
- func (v Value) UrlVar(p *url.URL) error
Examples ¶
Constants ¶
const ( ErrPointerExpected errors.Msg = "expected a non-nil pointer to a value" ErrUnableToSet errors.Msg = "unable to set value" ErrUnableToAddr errors.Msg = "unable to addr value" // ImplementationError indicates the programmer made a mistake implementing // the package and this should be fixed. ImplementationError errors.Kind = "implementation error" )
const ( ParseError errors.Kind = "parse error" ValidationError errors.Kind = "validation error" )
Variables ¶
This section is empty.
Functions ¶
func RegisterMarshalFunc ¶
func RegisterMarshalFunc(typ reflect.Type, fn MarshalFunc)
RegisterMarshalFunc registers the MarshalFunc for typ, making it globally available for Marshal, MarshalValue, MarshalReflect and any Marshaler.
func RegisterUnmarshalFunc ¶
func RegisterUnmarshalFunc(typ reflect.Type, fn UnmarshalFunc)
RegisterUnmarshalFunc registers the UnmarshalFunc for typ, making it globally available for Unmarshal and any Unmarshaler.
func Unmarshal ¶
Unmarshal parses Value and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an ErrPointerExpected error. If v is not a supported type an UnsupportedTypeError is returned. By default, the following types are supported: - encoding.TextUnmarshaler - time.Duration - url.URL - string - bool - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - complex64, complex128 Use RegisterUnmarshalFunc to add additional (custom) types.
Example ¶
var duration time.Duration if err := Unmarshal("1h2m3s", &duration); err != nil { panic(err) } fmt.Println(duration)
Output: 1h2m3s
Types ¶
type MarshalFunc ¶
func GetMarshalFunc ¶
func GetMarshalFunc(typ reflect.Type) MarshalFunc
GetMarshalFunc returns the globally registered MarshalFunc for reflect.Type typ or nil if there is none registered with RegisterMarshalFunc.
type Marshaler ¶
type Marshaler struct {
// contains filtered or unexported fields
}
Marshaler is a type which can marshal any reflect.Value to its raw string representation as long as it's registered with Register. It wil always fallback to the global Marshaler when a type is not registered.
Example ¶
var m Marshaler target, _ := url.ParseRequestURI("https://example.com") val, err := m.Marshal(reflect.ValueOf(target)) if err != nil { panic(err) } fmt.Println(val.String())
Output: https://example.com
func (*Marshaler) Func ¶
func (m *Marshaler) Func(typ reflect.Type) MarshalFunc
Func returns the (globally) registered MarshalFunc for reflect.Type typ or nil if there is none registered with Register or RegisterMarshalFunc.
type UnmarshalFunc ¶
UnmarshalFunc is a function which can unmarshal a Value to any type. Argument dest is always a pointer to the value to unmarshal to.
func GetUnmarshalFunc ¶
func GetUnmarshalFunc(typ reflect.Type) UnmarshalFunc
GetUnmarshalFunc returns the globally registered UnmarshalFunc for reflect.Type typ or nil if there is none registered with RegisterUnmarshalFunc.
func (UnmarshalFunc) Exec ¶
func (fn UnmarshalFunc) Exec(v Value, dest reflect.Value) error
Exec executes the UnmarshalFunc by taking the address of dest, and passing it as an interface to UnmarshalFunc. It will return an error when the address of reflect.Value dest cannot be taken, or when it is unable to set. Any error returned by UnmarshalFunc is wrapped with ParseError.
type Unmarshaler ¶
type Unmarshaler struct {
// contains filtered or unexported fields
}
Unmarshaler is a type which can unmarshal a Value to any type that's registered with Register. It wil always fallback to the global Unmarshaler when a type is not registered.
Example ¶
var u Unmarshaler var target *url.URL if err := u.Unmarshal("https://example.com", reflect.ValueOf(&target)); err != nil { panic(err) } fmt.Println(target.String())
Output: https://example.com
func (*Unmarshaler) Func ¶
func (u *Unmarshaler) Func(typ reflect.Type) UnmarshalFunc
Func returns the (globally) registered UnmarshalFunc for reflect.Type typ or nil if there is none registered with Register or RegisterUnmarshalFunc.
func (*Unmarshaler) Register ¶
func (u *Unmarshaler) Register(typ reflect.Type, fn UnmarshalFunc) *Unmarshaler
Register the UnmarshalFunc for typ but only for this Unmarshaler.
Example ¶
type myType struct { something string } var u Unmarshaler u.Register(reflect.TypeOf(myType{}), func(val 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" }
type UnsupportedTypeError ¶
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
func (*UnsupportedTypeError) Is ¶
func (e *UnsupportedTypeError) Is(err error) bool
type Value ¶
type Value string
Value is a textual representation of a raw value which is able to cast itself to any of the supported types using its corresponding method.
boolVal, err := rawconv.Value("true").Bool()
func Marshal ¶
Marshal formats the value pointed to by v to a raw string Value. If v is not a supported type an UnsupportedTypeError is returned. By default, the following types are supported: - encoding.TextUnmarshaler - time.Duration - url.URL - string - bool - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - float32, float64 - complex64, complex128 Use RegisterMarshalFunc to add additional (custom) types.
Example ¶
duration := time.Hour + (time.Minute * 2) + (time.Second * 3) val, err := Marshal(duration) if err != nil { panic(err) } fmt.Println(val.String())
Output: 1h2m3s
func (Value) Bool ¶
Bool tries to parse Value as a bool using strconv.ParseBool. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.
func (Value) Complex128 ¶
func (v Value) Complex128() (complex128, error)
Complex128 tries to parse Value as a complex128 using strconv.ParseComplex.
func (Value) Complex128Var ¶
func (v Value) Complex128Var(p *complex128) (err error)
Complex128Var sets the value p points to using Complex128.
func (Value) Complex64Var ¶
Complex64Var sets the value p points to using Complex64.
func (Value) DurationVar ¶
DurationVar sets the value p points to using Duration.
func (Value) Float32Var ¶
Float32Var sets the value p points to using Float32.
func (Value) Float64Var ¶
Float64Var sets the value p points to using Float64.