-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
94 lines (69 loc) · 3.13 KB
/
Copy pathparser.go
File metadata and controls
94 lines (69 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package parser
import (
"io"
"net/http"
"text/template"
)
// Parser provides high-performance template parsing for HTTP requests
type Parser interface {
// Parse executes the named template with the given HTTP request data
Parse(templateName string, request *http.Request, output io.Writer) (*RequestData, error)
// ParseWith parses a template with custom data and returns the parsed RequestData and any error
ParseWith(templateName string, req *http.Request, customData interface{}, output io.Writer) (*RequestData, error)
// Extract extracts RequestData from the request without parsing any template
// If body is provided, it will be used instead of reading from the request's body stream
Extract(req *http.Request, body ...[]byte) (*RequestData, error)
// UpdateTemplate loads or updates a template with the given content
UpdateTemplate(name string, content string) error
// GetCacheStats returns cache statistics
GetCacheStats() CacheStats
// Close cleanly shuts down the parser and releases resources
Close() error
}
// GenericParser provides type-safe template parsing for HTTP requests
// T is the target type that the parsed template will be converted to
type GenericParser[T any] interface {
// Parse executes the named template and returns the result as type T
Parse(templateName string, request *http.Request) (T, *RequestData, error)
// ParseWith executes the named template with custom data and returns the result as type T
ParseWith(templateName string, request *http.Request, data interface{}) (T, *RequestData, error)
// Extract extracts structured data from HTTP request without parsing templates
// If body is provided, it will be used instead of reading from the request's body stream
Extract(request *http.Request, body ...[]byte) (*RequestData, error)
// UpdateTemplate loads or updates a template with the given content
UpdateTemplate(name string, content string) error
// GetCacheStats returns cache statistics
GetCacheStats() CacheStats
// Close cleanly shuts down the parser and releases resources
Close() error
}
// Config holds configuration for the parser
type Config struct {
// TemplateLoader specifies how to load templates
TemplateLoader TemplateLoader
// WatchFiles enables automatic template reloading on file changes
WatchFiles bool
// MaxCacheSize limits the number of cached templates (0 = unlimited)
MaxCacheSize int
// FuncMap provides custom template functions
FuncMap template.FuncMap
}
// RequestData represents the data structure available to templates
type RequestData struct {
// Request is the original HTTP request
Request *http.Request
// Headers contains all HTTP headers
Headers map[string][]string
// Query contains query parameters
Query map[string][]string
// Form contains form data (for POST requests)
Form map[string][]string
// Body contains the request body as string
Body string
// BodyJSON contains parsed JSON data when Content-Type is application/json
BodyJSON map[string]interface{}
// BodyXML contains parsed XML data when Content-Type is text/xml or application/xml
BodyXML map[string]interface{}
// Custom contains any additional custom data
Custom interface{}
}