-
Notifications
You must be signed in to change notification settings - Fork 5
/
route_dom_context.go
61 lines (54 loc) · 1.51 KB
/
route_dom_context.go
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
package fir
import (
"encoding/json"
"strings"
"github.com/tidwall/gjson"
)
func newRouteDOMContext(ctx RouteContext, errs map[string]any) *RouteDOMContext {
return &RouteDOMContext{
URLPath: ctx.request.URL.Path,
Name: ctx.route.appName,
errors: errs,
}
}
// RouteDOMContext is a struct that holds route context data and is passed to the template
type RouteDOMContext struct {
Name string
URLPath string
errors map[string]any
}
// ActiveRoute returns the class if the route is active
func (rc *RouteDOMContext) ActiveRoute(path, class string) string {
if rc.URLPath == path {
return class
}
return ""
}
// NotActive returns the class if the route is not active
func (rc *RouteDOMContext) NotActiveRoute(path, class string) string {
if rc.URLPath != path {
return class
}
return ""
}
// Error can be used to lookup an error by name
// Example: {{.fir.Error "myevent.field"}} will return the error for the field myevent.field
// Example: {{.fir.Error "myevent" "field"}} will return the error for the event myevent.field
// It can be used in conjunction with ctx.FieldError to get the error for a field
func (rc *RouteDOMContext) Error(paths ...string) any {
data, _ := json.Marshal(rc.errors)
return gjson.GetBytes(data, getErrorLookupPath(paths...)).Value()
}
func getErrorLookupPath(paths ...string) string {
path := ""
if len(paths) == 0 {
path = "route"
} else {
for _, p := range paths {
p = strings.Trim(p, ".")
path += p + "."
}
}
path = strings.Trim(path, ".")
return path
}