Documentation
¶
Overview ¶
Package parser provides a parser for data, header, and query parameter input strings.
The parser accepts multiple input formats in a single string or across multiple strings. Each input string can contain one or more of the following formats:
## Headers
Headers use the format "name:value" where:
- name: alphanumeric characters, hyphens, and underscores only
- value: any characters (including spaces)
- Examples: "Content-Type:application/json", "Authorization:Bearer token"
## Query Parameters
Query parameters use the format "name==value" (note the double equals) where:
- name: any characters except equals signs
- value: any characters
- Examples: "q==search term", "page==1", "filter==active"
- Note: Query parameters can be repeated with the same name to create multiple values
## Body Parameters (Key-Value)
Body parameters use the format "path=value" where path can be:
### Simple Keys
- "key=value" creates {"key": "value"}
### Nested Object Access
Object keys can be accessed using:
- Dot notation: "obj.key=value" creates {"obj": {"key": "value"}}
- Bracket notation: "obj[key]=value" creates {"obj": {"key": "value"}}
- Mixed notation: "obj[key].subkey=value" creates {"obj": {"key": {"subkey": "value"}}}
### Array Access
Arrays can be accessed using:
- Array end (append): "arr[]=value" appends to array
- Array index: "arr[0]=value" sets specific index
- Nested arrays: "arr[0][1]=value" for multi-dimensional arrays
### Complex Examples
- "user[name]=John" → {"user": {"name": "John"}}
- "items[0]=apple" → {"items": ["apple"]}
- "items[]=apple" → {"items": ["apple"]}
- "items[]=banana" → {"items": ["apple", "banana"]}
- "matrix[0][1]=5" → {"matrix": [[null, 5]]}
- "config.database.host=localhost" → {"config": {"database": {"host": "localhost"}}}
## Body Parameters (JSON Values)
JSON values use the format "path:=jsonValue" where:
- path: same as key-value format above
- jsonValue: valid JSON (strings, numbers, booleans, null, objects, arrays)
- Examples:
- "name:=\"John\"" → {"name": "John"}
- "age:=25" → {"age": 25}
- "active:=true" → {"active": true}
- "data:=null" → {"data": null}
- "config:={\"host\":\"localhost\"}" → {"config": {"host": "localhost"}}
- "items:=[1,2,3]" → {"items": [1,2,3]}
## Multiple Inputs
Multiple parameters can be specified in a single string or across multiple strings. The parser will merge them into a single ParsedInput structure.
## Error Handling
The parser will return an error if:
- Input contains unexpected characters that don't match any format
- JSON values are malformed
- Array/object access patterns are invalid
## Examples
Input: []string{"Content-Type:application/json", "q==search", "user[name]=John", "items[]=apple"}
Output: ParsedInput{
Headers: []ParsedHeader{{"Content-Type", "application/json"}},
QueryParams: []ParsedQueryParam{{"q", "search"}},
Body: map[string]any{
"user": map[string]any{"name": "John"},
"items": []any{"apple"},
},
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONNull ¶
type JSONNull struct{}
JSONNull is a value representing a JSON null value.
This is required because `parseJSON` will be passed over if it returns `nil` in an OrdChoice combinator.
func (JSONNull) MarshalJSON ¶
MarshalJSON satisfies the json.Marshaler interface.
type ParsedHeader ¶
A ParsedHeader represents a single parsed header. They may be repeated in one input.
type ParsedInput ¶
type ParsedInput struct {
Headers []ParsedHeader
QueryParams []ParsedQueryParam
Body any
}
A ParsedInput represents data gathered from a user input string.
func ParseInput ¶
func ParseInput(in []string) (*ParsedInput, error)
ParseInput parses a string of headers.
type ParsedQueryParam ¶
A ParsedQueryParam represents a single parsed query parameter. They may be repeated in one input.