From a79339b83f64d9a509916af2ae5945ca60efa0ca Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Wed, 8 Nov 2023 12:29:59 -0500 Subject: [PATCH] normalize lazy props init --- properties.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/properties.go b/properties.go index df0465905..c9af66c0e 100644 --- a/properties.go +++ b/properties.go @@ -20,6 +20,7 @@ type Properties struct { // // Panics if key type is not comparable. func (m *Properties) Get(key interface{}) interface{} { + m.lazyInit() return m.values[key] } @@ -28,9 +29,7 @@ func (m *Properties) Get(key interface{}) interface{} { // // Panics if the key type is not comparable. func (m *Properties) Set(key, value interface{}) { - if m.values == nil { - m.values = map[interface{}]interface{}{} - } + m.lazyInit() m.values[key] = value } @@ -38,9 +37,7 @@ func (m *Properties) Set(key, value interface{}) { // // Panics if the key type is not comparable. func (m *Properties) Has(key interface{}) bool { - if m.values == nil { - return false - } + m.lazyInit() _, ok := m.values[key] return ok } @@ -48,7 +45,18 @@ func (m *Properties) Has(key interface{}) bool { // 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 } } + +func (m *Properties) lazyInit() { + if m.values == nil { + m.values = map[interface{}]interface{}{} + } +}