Help us shape the future of Go! Take the 2026 Go Developer Survey

reflectz

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 3 Imported by: 0

README

Reflectz

Reflectz is a small library which provides deep inspection of struct fields.

GoDoc MIT license

type MyStruct struct {
	ID    int     `json:"id"`
	Email string  `json:"email"`
	Info  Another `json:"info"`
}

type Another struct {
	Address string `reflectz:"-"`
	Details []string `json:"list"`
}

func main() {
	myStruct := MyStruct{
		ID:    10,
		Email: "foo@bar.com",
	}

	opts := &reflectz.Config{
		Recursive:   true,
		NamingTag:   "json",
		EmbeddedSep: "_",
	}

	for _, r := range reflectz.Inspect(&myStruct, opts) {
		fmt.Printf("%+v\n", r)
	} 
	// {FieldName:id Kind:int Type:int Tags:json:"id" Index:[0] Value:10 IsZero:false}
	// {FieldName:email Kind:string Type:string Tags:json:"email" Index:[1] Value:foo@bar.com IsZero:false}
	// {FieldName:info_list Kind:slice Type:[]string Tags:json:"list" Index:[2 0] Value:[] IsZero:true}
}

Configuration

Reflectz offers multiple methods of naming field results, snake_case, PascalCase or camelCase. The config option NamingTag can also be used to allow field names by a specified struct tag (e.g. json)

Reflectz offers the following struct tag options:

  • reflectz:"-" Does not include field or field children in results.
  • reflectz:"nodive" Does not dive and inspect field children (Set Recursive: false to enable this on all struct subfields.)

The Index field returns a slice of integers which can be used to dive back to an embedded field within the struct.

type Example struct {
	Data Embedded
}
type Embedded struct {
	Value string
}
// Example setting a field
e := Example{Data: Embedded{Value: "original"}}
structs := reflectz.Inspect(&e)
embeddedField := reflect.ValueOf(&e).Elem().FieldByIndex(structs[0].Index)
embeddedField.Set(reflect.ValueOf("newValue"))

fmt.Println(e)
// {{newValue}}

Documentation

Overview

Package reflectz provides inspection of structs.

Index

Constants

This section is empty.

Variables

View Source
var Tag = "reflectz"

Tag is the name of struct tag used for reflectz directives "-" and "nodive" are the current tag options available

Functions

This section is empty.

Types

type Config

type Config struct {
	// If Recursive is false Inspect will not dive embedded structs
	// true by default
	Recursive bool

	// EmbeddedSep controls the separator between embedded structs
	// underscore by default
	EmbeddedSep string

	// NamingScheme controls the naming convention of struct fields
	// Options include snake_case, PascalCase or camelCase
	// snake_case by default
	NamingScheme NameType

	// NamingTag controls the name of struct tag which should be used
	// for field names, for example 'json'. If the tag is not present
	// on a field, the name will fallback to the specified NamingScheme
	NamingTag string
}

type NameType

type NameType int
const (
	SnakeCase  NameType = 0
	CamelCase  NameType = 1
	PascalCase NameType = 2
)

type StructField

type StructField struct {
	FieldName string
	Kind      reflect.Kind
	Type      reflect.Type
	Tags      reflect.StructTag
	Index     []int
	Value     interface{}
	IsZero    bool
}

func Inspect

func Inspect(model interface{}, opt ...*Config) []StructField

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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