Moonshark/router/params.go

45 lines
823 B
Go

package router
// Maximum number of URL parameters per route
const maxParams = 20
// Params holds URL parameters with fixed-size arrays to avoid allocations
type Params struct {
Keys [maxParams]string
Values [maxParams]string
Count int
}
// Get returns a parameter value by name
func (p *Params) Get(name string) string {
for i := range p.Count {
if p.Keys[i] == name {
return p.Values[i]
}
}
return ""
}
// Reset clears all parameters
func (p *Params) Reset() {
p.Count = 0
}
// Set adds or updates a parameter
func (p *Params) Set(name, value string) {
// Try to update existing
for i := range p.Count {
if p.Keys[i] == name {
p.Values[i] = value
return
}
}
// Add new if space available
if p.Count < maxParams {
p.Keys[p.Count] = name
p.Values[p.Count] = value
p.Count++
}
}