forked from mbk-lab/rui_orig
2
0
Fork 0
rui/properties.go

88 lines
2.2 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"sort"
"strings"
)
// Properties interface of properties map
type Properties interface {
// 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
Get(tag string) any
getRaw(tag string) any
2021-09-07 17:36:50 +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" are returned and
// a description of the error is written to the log
2022-07-26 18:36:00 +03:00
Set(tag string, value any) bool
setRaw(tag string, value any)
2021-09-07 17:36:50 +03:00
// Remove removes the property with name defined by the argument
Remove(tag string)
// Clear removes all properties
Clear()
// AllTags returns an array of the set properties
AllTags() []string
}
type propertyList struct {
2022-07-26 18:36:00 +03:00
properties map[string]any
2021-09-07 17:36:50 +03:00
}
func (properties *propertyList) init() {
2022-07-26 18:36:00 +03:00
properties.properties = map[string]any{}
2021-09-07 17:36:50 +03:00
}
2022-07-26 18:36:00 +03:00
func (properties *propertyList) Get(tag string) any {
2021-09-07 17:36:50 +03:00
return properties.getRaw(strings.ToLower(tag))
}
2022-07-26 18:36:00 +03:00
func (properties *propertyList) getRaw(tag string) any {
2021-09-07 17:36:50 +03:00
if value, ok := properties.properties[tag]; ok {
return value
}
return nil
}
2022-07-26 18:36:00 +03:00
func (properties *propertyList) setRaw(tag string, value any) {
2021-09-07 17:36:50 +03:00
properties.properties[tag] = value
}
func (properties *propertyList) Remove(tag string) {
delete(properties.properties, strings.ToLower(tag))
}
func (properties *propertyList) remove(tag string) {
delete(properties.properties, tag)
}
func (properties *propertyList) Clear() {
2022-07-26 18:36:00 +03:00
properties.properties = map[string]any{}
2021-09-07 17:36:50 +03:00
}
func (properties *propertyList) AllTags() []string {
tags := make([]string, 0, len(properties.properties))
for t := range properties.properties {
tags = append(tags, t)
}
sort.Strings(tags)
return tags
}
func parseProperties(properties Properties, object DataObject) {
count := object.PropertyCount()
for i := 0; i < count; i++ {
if node := object.Property(i); node != nil {
switch node.Type() {
case TextNode:
properties.Set(node.Tag(), node.Text())
case ObjectNode:
properties.Set(node.Tag(), node.Object())
case ArrayNode:
properties.Set(node.Tag(), node.ArrayElements())
}
}
}
}