structfilter

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package structfilter converts a struct type to a new struct type with some fields removed or their tags altered. This package provides facilities to convert values from the old struct type to the new one.

A typical use case is to remove sensitive or superfluous fields from or adjust tags in a structure type before marshalling the structure, or before handing it over to a logging framework.

The generated types have neither methods nor unexported fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {

	// tag is the tag of the new struct field.
	Tag reflect.StructTag
	// contains filtered or unexported fields
}

Field describes a struct field in the newly generated structure.

func (*Field) Keep

func (f *Field) Keep()

Keep indicates that this field should be part of the filtered structure. This is the default. However, calling Keep explicitly may be necessary to countermand a Remove call by an earlier filter. A later filter might cause the field to be expluded after all by calling Remove again.

func (*Field) Name

func (f *Field) Name() string

Name returns the name of this field.

func (*Field) Remove

func (f *Field) Remove()

Remove indicates that this field should not be part of the filtered structure. A later filter might cause the field to be included after all by calling Keep.

type Func

type Func func(*Field) error

Func is a function type for altering or removing fields as they are inserted into a new structure. Whenever a Filter function returns a non-nil error, it will be reported back to the original caller of a filter method.

func InsertTagFilter

func InsertTagFilter(m Matcher, tag string) Func

InsertTagFilter inserts the specified structure tag into the structure tags of all fields whose name matches the specified matcher, provided the key in the specified tag string is not present yet. The string tag must have the conventional format for a single key-value pair:

key:"value"

If an original tag string does not have the conventional format, the behaviour of the returned filter is unspecified. If the matcher m is nil, no tags will be inserted.

func RemoveFieldFilter

func RemoveFieldFilter(m Matcher) Func

RemoveFieldFilter returns a filter function for removing all struct fields whose names match the specified matcher. If m is nil, RemoveFieldFilter will not remove any fields.

type Matcher

type Matcher interface {
	// MatchString reports whether the specified string matches.
	MatchString(string) bool
}

Matcher is the interface implemented by types which match a certain subset of strings.

For example, the *regexp.Regexp type from the golang standard library implements this interface.

type T

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

T is the main structfilter type.

The methods of T are unsafe for concurrent use.

func New

func New(filters ...Func) *T

New creates a new structure filter based on the specified filter functions. The filter functions are called in order for each structure field.

func (*T) Convert

func (t *T) Convert(in interface{}) (interface{}, error)

Convert converts the specified input value to an output value based on the filtered type of the dynamic type of the input. If in is nil, the return value is (nil, nil). Maps, pointers, and slices whose type definition does not involve a structure type will be copied shallowly. Struct fields not present in the filtered type are dropped. ToValue also works with recursive (self-referential) values.

func (*T) ReflectType

func (t *T) ReflectType(orig reflect.Type) (reflect.Type, error)

ReflectType allows direct filtering of structure types as presented by the golang reflect package. orig must be a structure type, or a pointer to a a structure type. On success, the returned filtered type is always a structure type, not a pointer type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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