feat: message parser with json/regex/text modes

This commit is contained in:
2026-06-27 13:11:05 +08:00
parent d030f0f0df
commit 69e32cb3ec
5 changed files with 189 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
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)
}
}