VictoriaMetrics/vendor/github.com/aws/smithy-go/properties.go

70 lines
1.7 KiB
Go
Raw Normal View History

2023-08-11 16:16:25 +02:00
package smithy
2024-09-26 22:33:05 +02:00
import "maps"
2023-08-11 16:16:25 +02:00
// PropertiesReader provides an interface for reading metadata from the
// underlying metadata container.
type PropertiesReader interface {
2024-09-26 22:33:05 +02:00
Get(key any) any
2023-08-11 16:16:25 +02:00
}
// Properties provides storing and reading metadata values. Keys may be any
2023-11-16 20:20:27 +01:00
// comparable value type. Get and Set will panic if a key is not comparable.
2023-08-11 16:16:25 +02:00
//
2023-11-16 20:20:27 +01:00
// The zero value for a Properties instance is ready for reads/writes without
// any additional initialization.
2023-08-11 16:16:25 +02:00
type Properties struct {
2024-09-26 22:33:05 +02:00
values map[any]any
2023-08-11 16:16:25 +02:00
}
// Get attempts to retrieve the value the key points to. Returns nil if the
// key was not found.
//
// Panics if key type is not comparable.
2024-09-26 22:33:05 +02:00
func (m *Properties) Get(key any) any {
2023-11-16 20:20:27 +01:00
m.lazyInit()
2023-08-11 16:16:25 +02:00
return m.values[key]
}
// Set stores the value pointed to by the key. If a value already exists at
// that key it will be replaced with the new value.
//
// Panics if the key type is not comparable.
2024-09-26 22:33:05 +02:00
func (m *Properties) Set(key, value any) {
2023-11-16 20:20:27 +01:00
m.lazyInit()
2023-08-11 16:16:25 +02:00
m.values[key] = value
}
// Has returns whether the key exists in the metadata.
//
// Panics if the key type is not comparable.
2024-09-26 22:33:05 +02:00
func (m *Properties) Has(key any) bool {
2023-11-16 20:20:27 +01:00
m.lazyInit()
2023-08-11 16:16:25 +02:00
_, ok := m.values[key]
return ok
}
2023-11-16 20:20:27 +01:00
// SetAll accepts all of the given Properties into the receiver, overwriting
// any existing keys in the case of conflicts.
func (m *Properties) SetAll(other *Properties) {
if other.values == nil {
return
}
m.lazyInit()
for k, v := range other.values {
m.values[k] = v
}
}
2024-09-26 22:33:05 +02:00
// Values returns a shallow clone of the property set's values.
func (m *Properties) Values() map[any]any {
return maps.Clone(m.values)
}
2023-11-16 20:20:27 +01:00
func (m *Properties) lazyInit() {
if m.values == nil {
2024-09-26 22:33:05 +02:00
m.values = map[any]any{}
2023-11-16 20:20:27 +01:00
}
}