reflectme

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 4 Imported by: 4

README

ReflectME

Build Status GoDoc

Package reflectme provides high level abstractions above the golang reflect library with some utilities functions.

The base code is a fork of https://github.com/oleiade/reflections. Then some other concepts and functions were added.

Installation

Use some dependency manager tool like glide pinning to some tag version. Eg

package: github.com/franciscocpg/test
import:
- package: github.com/franciscocpg/reflectme
  version: 0.1.4

Usage

As the code is 100% test covered the easier way is to look at tests and godoc.

Contribute

  • Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
  • Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).
  • Write tests which shows that the bug was fixed or that the feature works as expected and make sure the is 100% coverage. One can run make test-coverage to see the coverage %. If it's not 100%, one can run make coverage-missing to catch the lines that are not covered.
  • Send a pull request
  • If all checks status are successful the PR is going to be merged.
  • Every merged PR is going to tag a new semver release

Documentation

Overview

Package reflectme provides high level abstractions above the golang reflect library with some utilities functions.

The base code is a fork of https://github.com/oleiade/reflections. Then some other concepts and functions were added. Package reflections provides high level abstractions above the reflect library.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCopyOptions are the default options for copy function
	DefaultCopyOptions = CopyOptions{
		CopyZeroValues:       true,
		IgnoreNotFoundFields: true,
	}
)

Functions

func Copy

func Copy(from interface{}, to interface{}) error

Copy copies all values from "from" to "to" with DefaultCopyOptions

func CopyField

func CopyField(from interface{}, to interface{}, name string) error

CopyField copies the value from/to with field name

func CopyWithOptions

func CopyWithOptions(from interface{}, to interface{}, options CopyOptions) error

CopyWithOptions copies all values from "from" to "to" with CopyOptions

func Fields

func Fields(obj interface{}) ([]reflect.StructField, error)

Fields returns the struct fields list. obj can whether be a structure or pointer to structure.

func FieldsNames

func FieldsNames(obj interface{}) ([]string, error)

FieldsNames returns the struct fields names list. obj can whether be a structure or pointer to structure.

func GetField

func GetField(obj interface{}, name string) (interface{}, error)

GetField returns the value of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldKind

func GetFieldKind(obj interface{}, name string) (reflect.Kind, error)

GetFieldKind returns the kind of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldTag

func GetFieldTag(obj interface{}, fieldName, tagKey string) (string, error)

GetFieldTag returns the provided obj field tag value. obj can whether be a structure or pointer to structure.

func HasField

func HasField(obj interface{}, name string) (bool, error)

HasField checks if the provided field name is part of a struct. obj can whether be a structure or pointer to structure.

func IsStruct

func IsStruct(obj interface{}) bool

IsStruct checks whether the kind of a type is a struct

func IsZeroValue

func IsZeroValue(i interface{}) bool

IsZeroValue indicates if the interface has value according to golang spec: https://golang.org/ref/spec#The_zero_value

func Items

func Items(obj interface{}) (map[string]interface{}, error)

Items returns the field - value struct pairs as a map. obj can whether be a structure or pointer to structure.

func SetField

func SetField(s interface{}, name string, value interface{}) error

SetField sets the provided obj field with provided value. obj param has to be a pointer to a struct, otherwise it will soundly fail. Provided value type should match with the struct field you're trying to set.

func Tags

func Tags(obj interface{}, key string) (map[string]string, error)

Tags lists the struct tag fields. obj can whether be a structure or pointer to structure.

Types

type CopyOptions

type CopyOptions struct {
	CopyZeroValues       bool
	IgnoreNotFoundFields bool
}

CopyOptions are options for copy function

Jump to

Keyboard shortcuts

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