75
// they're present in the components. We do this because it's easier to add a slash depending on the context than to
76
// remove it.
77
>
func (r Route[T]) Representation() string {
route.go
78
>
return r.serialize(func(c Component[T]) string {
79
>
return c.Representation()
80
>
})
81
}
82
83
// Path returns the serialized path of the route with the given params. There will be no leading or trailing slashes,
84
// similar to the behavior of the [Route.Representation] method.
85
>
func (r Route[T]) Path(t T) string {
route.go
86
>
return r.serialize(func(c Component[T]) string {
87
>
return c.Serialize(t)
88
>
})
89
}
90
91
>
func (r Route[T]) serialize(f func(c Component[T]) string) string {
route.go
92
>
var sb strings.Builder
93
>
for i, c := range r.components {
94
>
if i > 0 {
95
>
sb.WriteString("/")
96
>
}
97
>
sb.WriteString(f(c))
98
}
100
}
101
102
// Deserialize the given vars into a new instance of the params type, T.
103
>
func (r Route[T]) Deserialize(vars map[string]string) T {
route.go
104
>
var t T
105
>
for _, c := range r.components {
106
>
c.Deserialize(vars, &t)
107
>
}
108
>
return t
109
}
110
111
// Constant returns a [Component] that represents a series of constant HTTP path components in a Route.
112
// They will be joined via strings when used to construct a path or path representation.
113
>
func Constant[T any](values ...string) constant[T] {
route.go
114
>
return values
115
>
}
116
117
type constant[T any] []string
118
119
>
func (s constant[T]) Representation() string {
route.go
120
>
return strings.Join(s, "/")
121
>
}
122
123
>
func (s constant[T]) Serialize(T) string {
route.go
124
>
return strings.Join(s, "/")
125
>
}
126
127
>
func (s constant[T]) Deserialize(map[string]string, *T) {}
route.go
128
129
// StringVariable returns a [Component] that represents a string variable in a Route.
130
>
func StringVariable[T any](name string, getter func(*T) *string) stringVariable[T] {
route.go
131
>
return stringVariable[T]{name, getter}
132
>
}
133
134
type stringVariable[T any] struct {