34 lines
818 B
Go
34 lines
818 B
Go
package parser
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// ParsedMessage is the result of parsing an incoming notification body.
|
|
type ParsedMessage struct {
|
|
Event string
|
|
Data map[string]interface{} // field name → value, passed to template
|
|
Body string // raw body (for text mode)
|
|
}
|
|
|
|
// Parser converts a raw HTTP body into a ParsedMessage.
|
|
type Parser interface {
|
|
Parse(body []byte) (*ParsedMessage, error)
|
|
}
|
|
|
|
func NewParser(parseMode string, parsePattern string) (Parser, error) {
|
|
switch parseMode {
|
|
case "json":
|
|
return &JSONParser{}, nil
|
|
case "regex":
|
|
if parsePattern == "" {
|
|
return nil, fmt.Errorf("regex parse mode requires parse_pattern")
|
|
}
|
|
return NewRegexParser(parsePattern)
|
|
case "text":
|
|
return &TextParser{}, nil
|
|
default:
|
|
return nil, fmt.Errorf("unknown parse_mode: %s", parseMode)
|
|
}
|
|
}
|