marshaler

package module
v0.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2021 License: Apache-2.0 Imports: 9 Imported by: 2

README

go-marshaler

This utility module currently decodes a map of values into a go struct. You could use this for converting "flat" wire formats (for example, from an SQL database or HTTP response into go structures.

UnmarshalStruct

The simple method is to use "UnmarshalStruct" to perform the transformation. For example,

package main

import (
  marshaler "github.com/djthorpe/go-marshaler"
)

func main() {
  var dest struct {
    A int     `test:"a"`
    B float32 `test:"b"`
    C []int   `test:"c"`
  }
  src := map[string]interface{}{
    "a": int(10),
    "b": float32(3.1415),
    "c": []int{ 1, 2, 3 },
  }
  if err := marshaler.UnmarshalStruct(src, &dest, "test", nil); err != nil {
    panic(err)
  }
  fmt.Println(dest)
}

Will output map[string]interface{}{ a: 10, b: 3.1415, c: [ 1, 2, 3 ] }. You can define a custom scalar decoding function which can transform a source value into a destination type. Pass this custom function as the last argument to UnmarshalStruct. For example,

func CustomTransformer(src reflect.Value, dest reflect.Type) (reflect.Value,error) {
  if dest != /* type I am interested in transforming to */ {
		return reflect.ValueOf(nil), nil
	}
  // Return type if it's already converted
  if src.Type() == dest {
		return v, nil
	}
  // Do transformation here to dest type, return error if the
  // source cannot be transformed....
}

Your function should return an invalid value to skip the transformation, and an error if you want to return an error out of the UnmarshalStruct function.

Decode

Decoding can also be performed as follows:

package main

import (
  marshaler "github.com/djthorpe/go-marshaler"
)

func main() {
  var dest struct {
    A int       `test:"a"`
    B float32   `test:"b"`
    C []int     `test:"c"`
    D time.Time `test:"d"`
  }
  src := map[string]interface{}{
    "a": int(10),
    "b": float32(3.1415),
    "c": []int{ 1, 2, 3 },
    "d": "2016-01-01T00:00:00Z",
  }

  dec := marshaler.NewDecoder("test",marshaler.ConvertTime)

  if err := dec.Decode(src, &dest); err != nil {
    panic(err)
  }
  fmt.Println(dest)
}

The NewDecoder method takes one or more custom functions which can be used for decoding fields. In this example, the ConvertTime decoder will convert a string into a time.Time structure. All the custom functions provided are:

  • marshaler.ConvertTime Converts strings formatted as RFC3339 into time.Time. Empty strings are converted into zero-time.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertDuration added in v0.0.5

func ConvertDuration(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertDuration returns time.Duration from integer, string or time.Duration

func ConvertIntUint added in v0.0.14

func ConvertIntUint(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertIntUint allows conversion from int value to a different int value, and uint value to a different uint value

func ConvertMapInterface added in v0.0.14

func ConvertMapInterface(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertMapInterface returns map[string]<type> from map[string]interface{} when all types within the interface match the destination type

func ConvertQueryValues added in v0.0.8

func ConvertQueryValues(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertQueryValues returns a value from a []string

func ConvertStringToNumber added in v0.0.9

func ConvertStringToNumber(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertStringToNumber returns int, uint,float or bool from string

func ConvertTime

func ConvertTime(v reflect.Value, dest reflect.Type) (reflect.Value, error)

ConvertTime returns time.Time and converts a ISO8601 string to a time.Time or empty string to time.Time{}

func UnmarshalSlice added in v0.0.12

func UnmarshalSlice(src, dst interface{}, fn UnmarshalScalarFunc) error

UnmarshalSlice will decode src into a slice

func UnmarshalStruct

func UnmarshalStruct(src, dst interface{}, name string, fn UnmarshalScalarFunc) error

UnmarshalStruct will decode src into dest field names identified by tag

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(name string, hooks ...UnmarshalScalarFunc) *Decoder

Create a new decoder object with 'name' used as struct tag for interpreting the field name

func (*Decoder) Decode

func (this *Decoder) Decode(src, dest interface{}) error

Decode decodes a map[string]interface{} type

func (*Decoder) DecodeQuery added in v0.0.8

func (this *Decoder) DecodeQuery(src url.Values, dest interface{}) error

DecodeQuery decodes a url.Values type

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(name string) *Encoder

Create a new decoder object with 'name' used as struct tag for interpreting the field name

func (*Encoder) Reflect

func (this *Encoder) Reflect(v interface{}) []*Field

Reflect on a structure (or pointer to structure) returns field names and their tags or nil if any field is ignored

type Error

type Error int
const (
	ErrSuccess Error = iota
	ErrBadParameter
)

func (Error) Error

func (e Error) Error() string

func (Error) With

func (e Error) With(args ...interface{}) error

type Field

type Field struct {
	Index int
	Name  string
	Type  reflect.Type
	Value reflect.Value
	Tags  []string
}

type UnmarshalScalarFunc

type UnmarshalScalarFunc func(reflect.Value, reflect.Type) (reflect.Value, error)

Custom function for converting a scalar value, the first argument is the source value and the second argument is the type of the destination

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL