2021-11-17 12:32:37 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import "sort"
|
|
|
|
|
|
|
|
// Params defines a type of a parameters list
|
2022-07-26 18:36:00 +03:00
|
|
|
type Params map[string]any
|
2021-11-17 12:32:37 +03:00
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Get returns a value of the property with name defined by the argument. The type of return value depends
|
|
|
|
// on the property. If the property is not set then nil is returned.
|
2022-07-26 18:36:00 +03:00
|
|
|
func (params Params) Get(tag string) any {
|
2021-11-17 12:32:37 +03:00
|
|
|
return params.getRaw(tag)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (params Params) getRaw(tag string) any {
|
2021-11-17 12:32:37 +03:00
|
|
|
if value, ok := params[tag]; ok {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Set sets the value (second argument) of the property with name defined by the first argument.
|
|
|
|
// Return "true" if the value has been set, in the opposite case "false" is returned and a description of an error is written to the log
|
2022-07-26 18:36:00 +03:00
|
|
|
func (params Params) Set(tag string, value any) bool {
|
2021-11-17 12:32:37 +03:00
|
|
|
params.setRaw(tag, value)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (params Params) setRaw(tag string, value any) {
|
2021-11-17 12:32:37 +03:00
|
|
|
if value != nil {
|
|
|
|
params[tag] = value
|
|
|
|
} else {
|
|
|
|
delete(params, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Remove removes the property with name defined by the argument from a map.
|
2021-11-17 12:32:37 +03:00
|
|
|
func (params Params) Remove(tag string) {
|
|
|
|
delete(params, tag)
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Clear removes all properties from a map.
|
2021-11-17 12:32:37 +03:00
|
|
|
func (params Params) Clear() {
|
|
|
|
for tag := range params {
|
|
|
|
delete(params, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// AllTags returns a sorted slice of all properties.
|
2021-11-17 12:32:37 +03:00
|
|
|
func (params Params) AllTags() []string {
|
|
|
|
tags := make([]string, 0, len(params))
|
|
|
|
for t := range params {
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
|
|
|
sort.Strings(tags)
|
|
|
|
return tags
|
|
|
|
}
|