reflector

package module
v0.0.0-...-12d37cd Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: BSD-2-Clause Imports: 4 Imported by: 1

README

reflector Build Status

This Go package extends standard package reflect with useful utilities:

  • convert map to struct
  • convert struct to map
  • convert slice of maps to slice of structs
  • convert slice of structs to slice of maps

Install it: go get github.com/canghai908/reflector

Documentation is available on godoc.org.

License: Simplified BSD License (see LICENSE).

Documentation

Overview

Package reflector extends standard package reflect with useful utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapToStruct

func MapToStruct(Map map[string]interface{}, StructPointer interface{}, converter Converter, tag string)

Converts a map to struct using converter function. First argument is a map. Second argument is a not-nil pointer to struct which will be modified. Only exported struct fields are set. Omitted or extra values in map are ignored. Pointers will be set. Tag may be used to change mapping between struct field and map key. Currently supports bool, ints, uints, floats, strings and pointer to them. Panics in case of error.

Example (NoConvert)
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8   // no automatic type conversion
		Float32 float32 `json:"f32"` // tag will be used
		String  string  // not present in map, will not be set
		Pstring *string // value will be deep-copied
		foo     int     // not exported, will not be set
	}
	var s T
	m := map[string]interface{}{"Uint8": uint8(8), "f32": float32(3.14), "Pstring": "pstr", "foo": 13}
	reflector.MapToStruct(m, &s, reflector.NoConvert, "json")
	m["Pstring"] = "foo"
	fmt.Printf("%#v %#v %#v %#v %#v\n", s.Uint8, s.Float32, s.String, *s.Pstring, s.foo)
}
Output:
0x8 3.14 "" "pstr" 0
Example (Strconv)
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8   // type conversion via strconv
		Float32 float32 `json:"f32"` // tag will be used
		String  string  // not present in map, will not be set
		Pstring *string // value will be deep-copied
		foo     int     // not exported, will not be set
	}
	var s T
	m := map[string]interface{}{"Uint8": 8, "f32": 3, "Pstring": "pstr", "foo": 13}
	reflector.MapToStruct(m, &s, reflector.Strconv, "json")
	m["Pstring"] = "foo"
	fmt.Printf("%#v %#v %#v %#v %#v\n", s.Uint8, s.Float32, s.String, *s.Pstring, s.foo)
}
Output:
0x8 3 "" "pstr" 0

func MapsToStructs

func MapsToStructs(Maps []map[string]interface{}, SlicePointer interface{}, converter Converter, tag string)

Converts a slice of maps to a slice of structs. Uses MapToStruct(). First argument is a slice of maps. Second argument is a pointer to (possibly nil) slice of structs which will be set.

Example (NoConvert)
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8   // no automatic type conversion
		Float32 float32 `json:"f32"` // tag will be used
		String  string  // not present in first map, will not be set
		Pstring *string // value will be deep-copied
		foo     int     // not exported, will not be set
	}
	var s []T
	maps := []map[string]interface{}{
		{"Uint8": uint8(8), "Pstring": "pstr"},
		{"f32": float32(3.14), "String": "str", "foo": 13},
	}
	reflector.MapsToStructs(maps, &s, reflector.NoConvert, "json")
	fmt.Printf("%#v %#v %#v %#v %#v\n", s[0].Uint8, s[0].Float32, s[0].String, *s[0].Pstring, s[0].foo)
	fmt.Printf("%#v %#v %#v %#v %#v\n", s[1].Uint8, s[1].Float32, s[1].String, s[1].Pstring, s[1].foo)
}
Output:
0x8 0 "" "pstr" 0
0x0 3.14 "str" (*string)(nil) 0
Example (Strconv)
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8   // type conversion via strconv
		Float32 float32 `json:"f32"` // tag will be used
		String  string  // not present in first map, will not be set
		Pstring *string // value will be deep-copied
		foo     int     // not exported, will not be set
	}
	var s []T
	maps := []map[string]interface{}{
		{"Uint8": 8, "f32": 3, "foo": 13, "Pstring": "pstr"},
		{"Uint8": "9", "f32": "4", "String": "43", "foo": "13"},
	}
	reflector.MapsToStructs(maps, &s, reflector.Strconv, "json")
	fmt.Printf("%#v %#v %#v %#v %#v\n", s[0].Uint8, s[0].Float32, s[0].String, *s[0].Pstring, s[0].foo)
	fmt.Printf("%#v %#v %#v %#v %#v\n", s[1].Uint8, s[1].Float32, s[1].String, s[1].Pstring, s[1].foo)
}
Output:
0x8 3 "" "pstr" 0
0x9 4 "43" (*string)(nil) 0

func MapsToStructs2

func MapsToStructs2(Maps []interface{}, SlicePointer interface{}, converter Converter, tag string)

Variant of MapsToStructs() with relaxed signature.

func NoConvert

func NoConvert(value interface{}, kind reflect.Kind) interface{}

Converter: requires value to be exactly of specified kind.

func Strconv

func Strconv(value interface{}, kind reflect.Kind) (res interface{})

Converter: uses strconv.Parse* functions.

func StructToMap

func StructToMap(StructPointer interface{}, Map map[string]interface{}, tag string)

Converts a struct to map. First argument is a pointer to struct. Second argument is a not-nil map which will be modified. Only exported struct fields are used. Pointers will be followed, nils will be present. Tag may be used to change mapping between struct field and map key. Currently supports bool, ints, uints, floats, strings and pointer to them. Panics in case of error.

Example
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8
		Float32 float32 `json:"f32"` // tag will be used
		String  string
		Pstring *string // pointer will be followed
		foo     int     // not exported
	}
	pstr := "pstr"
	s := T{8, 3.14, "str", &pstr, 13}
	m := make(map[string]interface{})
	reflector.StructToMap(&s, m, "json")
	fmt.Printf("%#v %#v %#v %#v %#v\n", m["Uint8"], m["f32"], m["String"], m["Pstring"], m["foo"])
}
Output:
0x8 3.14 "str" "pstr" <nil>

func StructValueToMap

func StructValueToMap(Struct interface{}, Map map[string]interface{}, tag string)

Converts a struct to map. Uses StructToMap(). First argument is a struct. Second argument is a not-nil map which will be modified. Only exported struct fields are used. Pointers will be followed, nils will be present. Tag may be used to change mapping between struct field and map key. Currently supports bool, ints, uints, floats, strings and pointer to them. Panics in case of error.

Example
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8
		Float32 float32 `json:"f32"` // tag will be used
		String  string
		Pstring *string // nil will be present in map
		foo     int     // not exported
	}
	s := T{8, 3.14, "str", nil, 13}
	m := make(map[string]interface{})
	reflector.StructValueToMap(s, m, "json")
	fmt.Printf("%#v %#v %#v %#v %#v\n", m["Uint8"], m["f32"], m["String"], m["Pstring"], m["foo"])
	_, ok := m["Pstring"]
	fmt.Println(ok)
}
Output:
0x8 3.14 "str" <nil> <nil>
true

func StructsToMaps

func StructsToMaps(Structs interface{}, Maps *[]map[string]interface{}, tag string)

Converts a slice of structs to a slice of maps. Uses StructValueToMap(). First argument is a slice of structs. Second argument is a pointer to (possibly nil) slice of maps which will be set.

Example
package main

import (
	"fmt"
	"github.com/canghai908/reflector"
)

func main() {
	type T struct {
		Uint8   uint8
		Float32 float32 `json:"f32"` // tag will be used
		String  string
		Pstring *string // pointer will be followed
		foo     int     // not exported
	}
	pstr := "pstr"
	s := []T{{8, 3.14, "str", &pstr, 13}}
	var m []map[string]interface{}
	reflector.StructsToMaps(s, &m, "json")
	fmt.Printf("%#v %#v %#v %#v %#v\n", m[0]["Uint8"], m[0]["f32"], m[0]["String"], m[0]["Pstring"], m[0]["foo"])
}
Output:
0x8 3.14 "str" "pstr" <nil>

Types

type Converter

type Converter func(value interface{}, kind reflect.Kind) interface{}

Converts value to kind. Panics if it can't be done.

Jump to

Keyboard shortcuts

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