rui_orig/params.go

63 lines
1.6 KiB
Go
Raw Normal View History

2021-11-17 12:32:37 +03:00
package rui
import "sort"
// Params defines a type of a parameters list
2024-11-13 12:56:39 +03:00
type Params map[PropertyName]any
2021-11-17 12:32:37 +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.
2024-11-13 12:56:39 +03:00
func (params Params) Get(tag PropertyName) any {
2021-11-17 12:32:37 +03:00
return params.getRaw(tag)
}
2024-11-13 12:56:39 +03:00
func (params Params) getRaw(tag PropertyName) any {
2021-11-17 12:32:37 +03:00
if value, ok := params[tag]; ok {
return value
}
return nil
}
// 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
2024-11-13 12:56:39 +03:00
func (params Params) Set(tag PropertyName, value any) bool {
2021-11-17 12:32:37 +03:00
params.setRaw(tag, value)
return true
}
2024-11-13 12:56:39 +03:00
func (params Params) setRaw(tag PropertyName, value any) {
2021-11-17 12:32:37 +03:00
if value != nil {
params[tag] = value
} else {
delete(params, tag)
}
}
// Remove removes the property with name defined by the argument from a map.
2024-11-13 12:56:39 +03:00
func (params Params) Remove(tag PropertyName) {
2021-11-17 12:32:37 +03:00
delete(params, tag)
}
// 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)
}
}
// AllTags returns a sorted slice of all properties.
2024-11-13 12:56:39 +03:00
func (params Params) AllTags() []PropertyName {
tags := make([]PropertyName, 0, len(params))
2021-11-17 12:32:37 +03:00
for t := range params {
tags = append(tags, t)
}
2024-11-13 12:56:39 +03:00
sort.Slice(tags, func(i, j int) bool {
return tags[i] < tags[j]
})
2021-11-17 12:32:37 +03:00
return tags
}
2024-11-13 12:56:39 +03:00
func (params Params) empty() bool {
return len(params) == 0
}