Skip to content
jobstoitPublic

About

A simplified golang reflect package for tags

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

30 Commits

Folders and files

Repository files navigation

Tags

GoDoc Build Status

An interface package for parsing tags in structs simplifying tag based golang reflections

Usage

Create your custom tag reader simply defining a parser:

func main() {
	obj := Obj{}

	if err := CustomUnmarshaler(&obj); err != nil {
		panic(err)
	}

	if err := CustomScanner(&obj); err != nil {
		panic(err)
	}
}

type Obj struct {
	Val   bool   `customTag:"yourcase"`
	StVal string `customTag:"required"`
}

func CustomUnmarshaler(obj interface{}) error {
	return tags.Parse(obj, `customTag`, func(tagVal string) (string, error) {
		switch tagVal {
		case `yourcase`:
			return `true`, nil
		default:
			return ``, errCustom
		}
	})
}

func CustomScanner(obj interface{}) error {
	return tags.Scan(obj, `customTag`, func(tagVal string, value interface{}) error {
		v, ok := value.(string)
		if !ok {
			return nil
		}

		if tagVal == `required` && v == `` {
			return errRequired
		}

		return nil
	})
}

you get the values of a parent value in the string seperated by a dot:

err := tags.Parse(obj, `tagname`, func(val string) (string, error) {
	allvalues := strings.Split(val, `.`)
	childVal := allvalues[len(allvalues)-1]
	...

Install

Get this package using go get:

$ go get github.com/jobbitz/tags/...

About

A simplified golang reflect package for tags

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages