stagparser

package module
v0.0.0-...-36d76c3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 6 Imported by: 3 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址