stagparser

package module
v0.0.0-...-e10a811 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2018 License: MIT Imports: 6 Imported by: 2

README

stagparser - generic parser for golang struct tag

GoDoc

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 consist of 'definition'. 'definition' has 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 calling 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)
}

License

MIT

Author

Yusuke Inuzuka

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

func ParseTag

func ParseTag(value string, name string) ([]Definition, error)

ParseTag parses a given tag value.

type ParseError

type ParseError interface {
	error
	// Source is a source name
	Source() string

	// Column is a column error occurred
	Column() int

	// Line is a line number error occurred
	Line() int
}

ParseError is an error indicating invalid tag value

Jump to

Keyboard shortcuts

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