From 96258309800daf0507599f0997f5749c984e26a6 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Wed, 31 May 2017 10:02:25 -0700 Subject: [PATCH] helper/schema: Move computedKeys init to init function This simplifies the new value initialization and future-proofs it against more complex initialization functionality. --- helper/schema/resource_diff.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/helper/schema/resource_diff.go b/helper/schema/resource_diff.go index d3c040294..4ed6d9152 100644 --- a/helper/schema/resource_diff.go +++ b/helper/schema/resource_diff.go @@ -22,6 +22,16 @@ type newValueWriter struct { // A lock to prevent races on writes. The underlying writer will have one as // well - this is for computed keys. lock sync.Mutex + + // To be used with init. + once sync.Once +} + +// init performs any initialization tasks for the newValueWriter. +func (w *newValueWriter) init() { + if w.computedKeys == nil { + w.computedKeys = make(map[string]bool) + } } // WriteField overrides MapValueWriter's WriteField, adding the ability to flag @@ -37,12 +47,10 @@ func (w *newValueWriter) WriteField(address []string, value interface{}, compute return err } + w.once.Do(w.init) + w.lock.Lock() defer w.lock.Unlock() - if w.computedKeys == nil { - w.computedKeys = make(map[string]bool) - } - if computed { w.computedKeys[strings.Join(address, ".")] = true } @@ -51,11 +59,7 @@ func (w *newValueWriter) WriteField(address []string, value interface{}, compute // ComputedKeysMap returns the underlying computed keys map. func (w *newValueWriter) ComputedKeysMap() map[string]bool { - w.lock.Lock() - defer w.lock.Unlock() - if w.computedKeys == nil { - w.computedKeys = make(map[string]bool) - } + w.once.Do(w.init) return w.computedKeys }