tags

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 2 Imported by: 0

README

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/...

Documentation

Overview

Package tags a simplified golang reflect for your tags.

Usage

Create your custom parser:

func ParseEnv(obj interface{}) error {
	return tags.Parse(obj, `env`, func(tagval string) (string, error) {
		envVar := os.Env(tagval)
		if envVar == `` {
			return ``, fmt.Errorf(`enviroment variable "%s" not found`, tagval)
		}
		return envVar, nil
	})
}

Creating your own scanner:

func ValidateEnum(obj interface{}) error {
	return tags.Scan(obj, `enum`, func(tagval string, propVal interface{}) error {
		validEnums := strings.Split(tagval, `;`)
		actual := fmt.Sprint(propVal)
		for _, enum := range validEnums {
			if actual == enum {
				return nil
			}
		}
		return fmt.Errorf(`Not a valid enum`)
	})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(obj interface{}, tagname string, parser func(string) (string, error)) error

Parse parses the given tag values using the given parser

func ParseHard

func ParseHard(obj interface{}, tagname string, parser func(string) (string, error)) error

ParseHard parses the giver tag values using the given parser and overrides the previous values

func Scan

func Scan(obj interface{}, tagname string, scanner func(string, interface{}) error) error

Scan gives the given tag value and the value in that stuct property to the given scanner

Types

This section is empty.

Directories

Path Synopsis
Package defaults sets default values in your struct.
Package defaults sets default values in your struct.
Package env is for parsing environment variables.
Package env is for parsing environment variables.
Package flags is for parsing execution flags.
Package flags is for parsing execution flags.
Package forms is for parsing http.Request form data Usage: func MyHandler(w http.ResponseWriter, r *http.Request) { var body reqBody if err := form.Decode(r, &body); err != nil { w.WriterHeader(http.StatusUnprocessibleEnitity) return } } type reqBody struct { Name string `form:"name"` Age int `form:"age"` }
Package forms is for parsing http.Request form data Usage: func MyHandler(w http.ResponseWriter, r *http.Request) { var body reqBody if err := form.Decode(r, &body); err != nil { w.WriterHeader(http.StatusUnprocessibleEnitity) return } } type reqBody struct { Name string `form:"name"` Age int `form:"age"` }

Jump to

Keyboard shortcuts

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