reflectx

package
v0.0.0-...-04766b6 Latest Latest
Warning

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

Go to latest
Published: May 5, 2015 License: MIT, MIT Imports: 3 Imported by: 0

README

reflectx

The sqlx package has special reflect needs. In particular, it needs to:

  • be able to map a name to a field
  • understand embedded structs
  • understand mapping names to fields by a particular tag
  • user specified name -> field mapping functions

These behaviors mimic the behaviors by the standard library marshallers and also the behavior of standard Go accessors.

The first two are amply taken care of by Reflect.Value.FieldByName, and the third is addressed by Reflect.Value.FieldByNameFunc, but these don't quite understand struct tags in the ways that are vital to most marshalers, and they are slow.

This reflectx package extends reflect to achieve these goals.

Documentation

Overview

Package reflect implements extensions to the standard reflect lib suitable for implementing marshaling and unmarshaling packages. The main Mapper type allows for Go-compatible named atribute access, including accessing embedded struct attributes and the ability to use functions and struct tags to customize field names.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deref

func Deref(t reflect.Type) reflect.Type

Deref is Indirect for reflect.Types

func FieldByIndexes

func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value

FieldByIndexes returns a value for a particular struct traversal.

func FieldByIndexesReadOnly

func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value

FieldByIndexesReadOnly returns a value for a particular struct traversal, but is not concerned with allocating nil pointers because the value is going to be used for reading and not setting.

Types

type Kinder

type Kinder interface {
	Kind() reflect.Kind
}

type Mapper

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

Mapper is a general purpose mapper of names to struct fields. A Mapper behaves like most marshallers, optionally obeying a field tag for name mapping and a function to provide a basic mapping of fields to names.

func NewMapper

func NewMapper(tagName string) *Mapper

NewMapper returns a new mapper which optionally obeys the field tag given by tagName. If tagName is the empty string, it is ignored.

func NewMapperFunc

func NewMapperFunc(tagName string, f func(string) string) *Mapper

NewMapperFunc returns a new mapper which optionally obeys a field tag and a struct field name mapper func given by f. Tags will take precedence, but for any other field, the mapped name will be f(field.Name)

func (*Mapper) FieldByName

func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value

FieldByName returns a field by the its mapped name as a reflect.Value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value if the name is not found.

func (*Mapper) FieldMap

func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value

FieldMap returns the mapper's mapping of field names to reflect values. Panics if v's Kind is not Struct, or v is not Indirectable to a struct kind.

func (*Mapper) FieldsByName

func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value

FieldsByName returns a slice of values corresponding to the slice of names for the value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value for each name not found.

func (*Mapper) TraversalsByName

func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int

Traversals by name returns a slice of int slices which represent the struct traversals for each mapped name. Panics if t is not a struct or Indirectable to a struct. Returns empty int slice for each name not found.

func (*Mapper) TypeMap

func (m *Mapper) TypeMap(t reflect.Type) fieldMap

TypeMap returns a mapping of field strings to int slices representing the traversal down the struct to reach the field.

Jump to

Keyboard shortcuts

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