-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouting_option.go
82 lines (68 loc) · 2 KB
/
routing_option.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package xun
// RoutingOptions holds metadata and a viewer for routing configuration.
type RoutingOptions struct {
metadata map[string]any
viewer Viewer
}
// Get returns the value associated with the given name from the routing metadata.
// If the name does not exist, it returns nil.
func (ro *RoutingOptions) Get(name string) any {
return ro.metadata[name]
}
// GetString returns the value associated with the given name from the routing
// metadata as a string. If the name does not exist, it returns an empty string.
func (ro *RoutingOptions) GetString(name string) string {
it, ok := ro.metadata[name]
if !ok {
return ""
}
s, ok := it.(string)
if !ok {
return ""
}
return s
}
// GetInt returns the value associated with the given name from the routing
// metadata as an integer. If the name does not exist, it returns 0.
func (ro *RoutingOptions) GetInt(name string) int {
it, ok := ro.metadata[name]
if !ok {
return 0
}
v, ok := it.(int)
if !ok {
return 0
}
return v
}
// RoutingOption is a function that takes a pointer to RoutingOptions and
// modifies it. It is used to customize the behavior of the router when
// adding routes.
type RoutingOption func(*RoutingOptions)
const (
NavigationName = "name"
NavigationIcon = "icon"
NavigationAccess = "access"
)
// WithMetadata adds a key-value pair to the routing metadata.
// It creates a new map if the metadata map is nil.
func WithMetadata(key string, value any) RoutingOption {
return func(ro *RoutingOptions) {
if ro.metadata == nil {
ro.metadata = make(map[string]any)
}
ro.metadata[key] = value
}
}
// WithNavigation adds navigation-related metadata to the routing options.
// It sets the name, icon, and access level for the navigation element.
func WithNavigation(name, icon, access string) RoutingOption {
return func(ro *RoutingOptions) {
if ro.metadata == nil {
ro.metadata = make(map[string]any)
}
ro.metadata[NavigationName] = name
ro.metadata[NavigationIcon] = icon
ro.metadata[NavigationAccess] = access
}
}