tags

package module
v0.0.0-...-80e2126 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2017 License: MIT Imports: 4 Imported by: 1

README

wercker status Build Status GoDoc GoCover GoReportCard

Tags

Tags is a simple string parser used for extracting keyword/arguments pairs from structure tags.

Why

I was recreating this feature for a few libraries i'm maintaining and developing (such as validation, binding and a ORM). So i felt the need to create a common library to take care of this job and keeps me stopping repeating my self ;).

Examples

Tags can parse a few formats

Single keyword tag
tags.Parse(`required`)

// outputs map
// []Param{ Param{ Name: "required", Args:nil } 
Multiple keywords

You can delimit keywords with a comma , or a semicolon ;

tags.Parse(`required;email`)
tags.Parse(`required,email`)

// outputs map
// []Param{ 
//    Param{ Name: "required", Args:nil },
//    Param{ Name: "email", Args:nil },
// }
Keywords with params

You can delimit keywords with a comma , or a semicolon ;

tags.Parse(`between(10,20);in("foo", "bar", "foo bar")`)

// outputs map
// []Param{ 
//    Param{ Name: "between", Args:[]string{"10", "20"} },
//    Param{ Name: "in", Args:[]string{"foo", "bar", "foo bar"} },
// }
Ignored keywords

You can delimit keywords with a comma , or a semicolon ;

tags.Parse(`-`)

// gives nil pointer back

Error handling

When a malformed tag is provided the parse function will return a error with the reason why. This could be a unexpected end of string (EOF) or badly closed statements.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseMap

func ParseMap(tag string) (map[string][]string, error)

ParseMap parses the provided tag and returns a map with the found parameters. If the provided tag has the ignore identifier ("-") it will return a nil map If the provided tag is empty it will return an empty map

Types

type Param

type Param struct {
	Name string
	Args []string
}

Param stores a keyword and the arguments extracted from a parsed tag

func Parse

func Parse(tag string) ([]Param, error)

Parse parses the provided tag and returns a slice with the found parameter (keywords/value) pairs. If the provided tag has the ignore identifier ("-") it will return a nil slice If the provided tag is empty it will return a empty slice

type Parser

type Parser interface {
	//Parse will parse a tag and returns the params in order
	Parse(tag string) ([]Param, error)
	//ParseMao will parse a tag and will return an unordered map
	ParseMap(tag string) (map[string][]string, error)
}

Parser interface only exports parse interface

func NewParser

func NewParser(ignoreKeyword string) Parser

NewParser creates a new parser with a custom ignoreKeywords

Jump to

Keyboard shortcuts

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