rui_orig/properties.go

114 lines
2.7 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
2024-04-23 19:34:36 +03:00
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)
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// Remove removes the property with name defined by the argument
Remove(tag string)
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// Clear removes all properties
Clear()
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// 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
}
2024-04-22 19:14:58 +03:00
func (properties *propertyList) writeToBuffer(buffer *strings.Builder,
indent string, objectTag string, tags []string) {
buffer.WriteString(objectTag)
buffer.WriteString(" {\n")
indent2 := indent + "\t"
for _, tag := range tags {
if value, ok := properties.properties[tag]; ok {
buffer.WriteString(indent2)
buffer.WriteString(tag)
buffer.WriteString(" = ")
writePropertyValue(buffer, tag, value, indent2)
buffer.WriteString(",\n")
}
}
buffer.WriteString(indent)
buffer.WriteString("}")
}
2021-09-07 17:36:50 +03:00
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())
}
}
}
}