Skip to content

Commit

Permalink
normalize lazy props init
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Nov 8, 2023
1 parent e8a6042 commit a79339b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand All @@ -28,27 +29,34 @@ 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
}

// Has returns whether the key exists in the metadata.
//
// 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
}

// 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{}{}
}
}

0 comments on commit a79339b

Please sign in to comment.