rui_orig/properties.go

235 lines
5.7 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"iter"
2026-08-15 11:33:22 +03:00
"maps"
"slices"
2021-09-07 17:36:50 +03:00
"strings"
2026-08-06 16:52:19 +03:00
"sync"
2021-09-07 17:36:50 +03:00
)
// 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.
2024-11-13 12:56:39 +03:00
Get(tag PropertyName) any
getRaw(tag PropertyName) 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
2024-11-13 12:56:39 +03:00
Set(tag PropertyName, value any) bool
setRaw(tag PropertyName, value any)
2026-08-15 11:33:22 +03:00
setAll(properties map[PropertyName]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
2024-11-13 12:56:39 +03:00
Remove(tag PropertyName)
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
// All returns an iterator to access the properties
All() iter.Seq2[PropertyName, any]
2021-09-07 17:36:50 +03:00
// AllTags returns an array of the set properties
2024-11-13 12:56:39 +03:00
AllTags() []PropertyName
IsEmpty() bool
2021-09-07 17:36:50 +03:00
}
type propertyList struct {
2024-11-13 12:56:39 +03:00
properties map[PropertyName]any
normalize func(PropertyName) PropertyName
2026-08-06 16:52:19 +03:00
mutex sync.Mutex
2024-11-13 12:56:39 +03:00
}
type dataProperty struct {
propertyList
supportedProperties []PropertyName
get func(Properties, PropertyName) any
set func(Properties, PropertyName, any) []PropertyName
remove func(Properties, PropertyName) []PropertyName
}
func defaultNormalize(tag PropertyName) PropertyName {
return PropertyName(strings.ToLower(strings.Trim(string(tag), " \t")))
2021-09-07 17:36:50 +03:00
}
func (properties *propertyList) init() {
2024-11-13 12:56:39 +03:00
properties.properties = map[PropertyName]any{}
properties.normalize = defaultNormalize
2021-09-07 17:36:50 +03:00
}
func (properties *propertyList) IsEmpty() bool {
2026-08-06 16:52:19 +03:00
properties.mutex.Lock()
defer properties.mutex.Unlock()
2024-11-13 12:56:39 +03:00
return len(properties.properties) == 0
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func (properties *propertyList) getRaw(tag PropertyName) any {
2026-08-06 16:52:19 +03:00
properties.mutex.Lock()
defer properties.mutex.Unlock()
2021-09-07 17:36:50 +03:00
if value, ok := properties.properties[tag]; ok {
return value
}
return nil
}
2024-11-13 12:56:39 +03:00
func (properties *propertyList) setRaw(tag PropertyName, value any) {
2026-08-06 16:52:19 +03:00
properties.mutex.Lock()
2024-11-13 12:56:39 +03:00
if value == nil {
delete(properties.properties, tag)
} else {
properties.properties[tag] = value
}
2026-08-06 16:52:19 +03:00
properties.mutex.Unlock()
2021-09-07 17:36:50 +03:00
}
2026-08-15 11:33:22 +03:00
func (properties *propertyList) setAll(props map[PropertyName]any) {
properties.mutex.Lock()
maps.Copy(properties.properties, props)
properties.mutex.Unlock()
}
2024-11-13 12:56:39 +03:00
/*
func (properties *propertyList) Remove(tag PropertyName) {
properties.remove(properties, properties.normalize(tag))
}
*/
2026-08-06 16:52:19 +03:00
2021-09-07 17:36:50 +03:00
func (properties *propertyList) Clear() {
2026-08-06 16:52:19 +03:00
properties.mutex.Lock()
2024-11-13 12:56:39 +03:00
properties.properties = map[PropertyName]any{}
2026-08-06 16:52:19 +03:00
properties.mutex.Unlock()
2021-09-07 17:36:50 +03:00
}
func (properties *propertyList) All() iter.Seq2[PropertyName, any] {
2026-08-15 11:33:22 +03:00
properties.mutex.Lock()
defer properties.mutex.Unlock()
return func(yield func(PropertyName, any) bool) {
for tag, value := range properties.properties {
if !yield(tag, value) {
return
}
}
}
}
2024-11-13 12:56:39 +03:00
func (properties *propertyList) AllTags() []PropertyName {
2026-08-06 16:52:19 +03:00
properties.mutex.Lock()
2024-11-13 12:56:39 +03:00
tags := make([]PropertyName, 0, len(properties.properties))
for tag := range properties.properties {
tags = append(tags, tag)
2021-09-07 17:36:50 +03:00
}
2026-08-06 16:52:19 +03:00
properties.mutex.Unlock()
slices.Sort(tags)
2021-09-07 17:36:50 +03:00
return tags
}
2025-07-06 19:11:34 +03:00
/*
2024-04-22 19:14:58 +03:00
func (properties *propertyList) writeToBuffer(buffer *strings.Builder,
2024-11-13 12:56:39 +03:00
indent string, objectTag string, tags []PropertyName) {
2024-04-22 19:14:58 +03:00
2026-09-03 12:39:07 +03:00
writeStrings(buffer, objectTag, " {\n")
2024-04-22 19:14:58 +03:00
indent2 := indent + "\t"
for _, tag := range tags {
if value, ok := properties.properties[tag]; ok {
text := propertyValueToString(tag, value, indent2)
if text != "" {
2026-09-03 12:39:07 +03:00
writeStrings(buffer, indent2, string(tag), " = ", text, ",\n")
}
2024-04-22 19:14:58 +03:00
}
}
2026-09-03 12:39:07 +03:00
writeStrings(buffer, indent, "}")
2024-04-22 19:14:58 +03:00
}
2025-07-06 19:11:34 +03:00
*/
2024-04-22 19:14:58 +03:00
2021-09-07 17:36:50 +03:00
func parseProperties(properties Properties, object DataObject) {
for tag, node := range object.Properties() {
switch node.Type() {
case TextDataValue:
properties.Set(PropertyName(tag), node.String())
case ObjectDataValue:
properties.Set(PropertyName(tag), node.Object())
case ArrayDataValue:
array := node.Array()
if array == nil {
return
}
switch array.Size() {
2025-07-13 17:22:55 +03:00
case 0:
// do nothing
case 1:
if v := array.Element(0); v.Type() == TextDataValue {
properties.Set(PropertyName(tag), v.String())
2025-07-13 17:22:55 +03:00
} else {
properties.Set(PropertyName(tag), v)
2025-07-13 17:22:55 +03:00
}
default:
properties.Set(PropertyName(tag), array)
2025-07-13 17:22:55 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
}
2024-11-13 12:56:39 +03:00
func propertiesGet(properties Properties, tag PropertyName) any {
return properties.getRaw(tag)
}
func propertiesRemove(properties Properties, tag PropertyName) []PropertyName {
if properties.getRaw(tag) == nil {
return []PropertyName{}
}
properties.setRaw(tag, nil)
return []PropertyName{tag}
}
func (data *dataProperty) init() {
data.propertyList.init()
data.get = propertiesGet
data.set = propertiesSet
data.remove = propertiesRemove
}
func (data *dataProperty) Get(tag PropertyName) any {
return data.get(data, data.normalize(tag))
2024-11-13 12:56:39 +03:00
}
func (data *dataProperty) Remove(tag PropertyName) {
data.remove(data, data.normalize(tag))
}
func (data *dataProperty) writeToBuffer(buffer *strings.Builder, indent string, objectName string, tags []PropertyName) {
2026-08-15 11:33:22 +03:00
data.mutex.Lock()
defer data.mutex.Unlock()
2026-09-03 12:39:07 +03:00
writeStrings(buffer, objectName, "{ ")
comma := false
for _, tag := range tags {
if value, ok := data.properties[tag]; ok {
text := propertyValueToString(tag, value, indent)
if text != "" {
if comma {
buffer.WriteString(", ")
}
2026-09-03 12:39:07 +03:00
writeStrings(buffer, string(tag), " = ", text)
comma = true
}
}
}
buffer.WriteString(" }")
}
func (data *dataProperty) writeString(buffer *strings.Builder, indent string) {
data.writeToBuffer(buffer, indent, "_", data.AllTags())
}