strct

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 7 Imported by: 3

README

strct

GoDoc Build Status

Strct is a simplified golang reflector. See godoc for usage

Installation

Install this package using go get:

$ go get github.com/jobstoit/strct

Documentation

Overview

Package strct a simplified golang reflect package

The struct package takes the complexety of golang reflecs away by giving you 2 functions to work with: the scanner and the parser. The scanner takes the object that needs to be scanned and a function that goes over each type property of the struct of the given object. The scanner function has 2 parameters: the reflect.Structfield which contains the data such as the property name and the property tags and contains a pointer to the reflect.Value which you than can either interface with or set a new value of.

You can easaly set a new value using the Parser the parser takes the value that needs to be set as string (because its univerably accessable) and the reflect.Value pointer that needs to be set.

Usage

Get the values you need using the scanner

func TagEnvParser(obj interface{}, parsevalue string) error {
	return strct.Scan(obj, func(field reflect.StructField, value *reflect.Value) error {
		tagVal := field.Tag.Get(`env`)
		if tagVal == `` {
			return nil
		}

		strct.Parse(os.GetEnv(tagval), value)
	})
}

Index

Constants

This section is empty.

Variables

View Source
var ErrNoPtr = fmt.Errorf(`insert is not a pointer or a struct`)

ErrNoPtr gets thrown if the inserted object is not a pointer or a struct type

Functions

func Parse

func Parse(val string, fv *reflect.Value) error

Parse sets a string as value to the the reflected value

func ParseHard

func ParseHard(val string, fv *reflect.Value) error

ParseHard sets a string as value to the given value and overides previous values

func Scan

func Scan(obj interface{}, onProperty func(reflect.StructField, *reflect.Value) error) error

Scan scans the properties of the given object struct

func ScanAll

func ScanAll(obj interface{}, onStruct func(reflect.StructField) error, onProperty func(reflect.StructField, *reflect.Value) error) error

ScanAll scans each structs attribute

Types

This section is empty.

Jump to

Keyboard shortcuts

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