Documentation
¶
Overview ¶
Package stagparser provides a generic parser for golang struct tag. stagparser can parse tags like the following:
- `validate:"required,length(min=1, max=10)"`
- `validate:"max=10,list=[apple,'star fruits']"`
tags are consists of 'definition'. 'definition' have 3 forms:
- name only: required
- name with a single attribute: max=10
- in this case, parse result is name="max", attributes={"max":10}
- name with multiple attributes: length(min=1, max=10)
name and attribute must be a golang identifier. An attribute value must be one of an int64, a float64, an identifier, a string quoted by "'" and an array.
- int64: 123
- float64: 111.12
- string: 'ab\tc'
- identifiers are interpreted as string in value context
- array: [1, 2, aaa]
You can parse objects just call ParseStruct:
import "github.com/yuin/stagparser"
type User struct {
Name string `validate:"required,length(min=4,max=10)"`
}
func main() {
user := &User{"bob"}
definitions, err := stagparser.ParseStruct(user)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseStruct ¶
func ParseStruct(obj interface{}, tag string) (map[string][]Definition, error)
ParseStruct parses struct tags of given object. map key is a field name.
Types ¶
type Definition ¶
type Definition interface {
// Name is a name of the definition
Name() string
// Attributes are attributes of the definition
Attributes() map[string]interface{}
// Attribute returns an attribute value and true if an attribute exists
Attribute(name string) (interface{}, bool)
}
Definition is a struct tag value element.
Click to show internal directories.
Click to hide internal directories.