Documentation
¶
Overview ¶
Package tagparser parses Go struct tag syntax with support for quoted values, escapes, and flexible key-value pairs.
This parser is stricter than reflect.StructTag, providing comprehensive error reporting with precise position information. It supports both options-only parsing and name extraction modes.
Performance ¶
For zero-allocation parsing, use ParseFunc or ParseFuncWithName with pre-allocated maps. The parser uses fast-path optimizations for simple tags without quotes or escapes.
Example ¶
Parse a tag with all items as options:
tag, err := tagparser.Parse(`json,omitempty,min=5`)
if err != nil {
log.Fatal(err)
}
// tag.Options = {"json": "", "omitempty": "", "min": "5"}
Parse a tag with name extraction:
tag, err := tagparser.ParseWithName(`json:"name,omitempty"`)
if err != nil {
log.Fatal(err)
}
// tag.Name = "json"
// tag.Options = {"name": "", "omitempty": ""}
Zero-allocation parsing:
opts := make(map[string]string, 4)
err := tagparser.ParseFunc(`json,omitempty`, func(key, value string) error {
opts[key] = value
return nil
})
Index ¶
Constants ¶
const MaxTagLength = 1 << 16 // 64KB
MaxTagLength is the maximum allowed tag length to prevent DoS attacks. This limit of 64KB is far larger than any realistic struct tag.
Variables ¶
var ErrDuplicateKey = errors.New("duplicate option key")
ErrDuplicateKey is returned as Error.Cause for duplicate tag keys.
var ErrTagTooLarge = errors.New("tag exceeds maximum length")
ErrTagTooLarge is returned when a tag exceeds MaxTagLength.
Functions ¶
func ParseFunc ¶
ParseFunc enumerates fields of a tag treating all items as options.
Format: key1,key2=value2,key3='quoted, value',key4
Rules:
- Items are comma-separated; key=value pairs use equals sign
- Values can be bare words or single-quoted strings
- Backslash escapes special characters
- Leading/trailing ASCII whitespace is trimmed
- All items are treated as options (no name extraction)
- Empty keys are not allowed
Returns ErrTagTooLarge if tag length exceeds MaxTagLength.
func ParseFuncWithName ¶
ParseFuncWithName enumerates fields of a tag treating the first item as a name.
Format: name,key1,key2=value2,key3='quoted, value',key4
Rules:
- Items are comma-separated; key=value pairs use equals sign
- Values can be bare words or single-quoted strings
- Backslash escapes special characters
- Leading/trailing ASCII whitespace is trimmed
- First item without equals becomes the name (empty key in callback)
- If first item has equals, it's treated as a normal option
- Empty keys are not allowed for normal items
Returns ErrTagTooLarge if tag length exceeds MaxTagLength.
Types ¶
type Error ¶
type Error struct {
Tag string // Original tag string
Pos int // 0-based position of error
Msg string // Error message
Cause error // Optional underlying error
}
Error is the type of error returned by parse funcs in this package.
type Tag ¶
Tag represents a parsed struct tag.
func Parse ¶
Parse parses a tag treating all items as options (default behavior). Example: "foo,bar=baz" → Name="", Options={"foo": "", "bar": "baz"}.
Returns ErrTagTooLarge if tag length exceeds MaxTagLength.
func ParseWithName ¶
ParseWithName parses a tag treating the first item without equals as a name. Example: "foo,bar=baz" → Name="foo", Options={"bar": "baz"}. Example: "foo=bar,baz" → Name="", Options={"foo": "bar", "baz": ""}.
Returns ErrTagTooLarge if tag length exceeds MaxTagLength.