Skip to content

Commit 20f3f3b

Browse files
committed
fix: concurrent map
1 parent bd72d11 commit 20f3f3b

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
OWNER := dnitsch
33
NAME := configmanager
4-
GIT_TAG := "1.17.1-pre"
4+
GIT_TAG := "1.17.2"
55
VERSION := "v$(GIT_TAG)"
66
# VERSION := "$(shell git describe --tags --abbrev=0)"
77
REVISION := $(shell git rev-parse --short HEAD)

pkg/generator/generator.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ type GenVarsiface interface {
6060
ConfigOutputPath() string
6161
}
6262

63+
type muRawMap struct {
64+
sync.RWMutex
65+
tokenMap ParsedMap
66+
}
67+
6368
// GenVars is the main struct holding the
6469
// strategy patterns iface
6570
// any initialised config if overridded with withers
@@ -72,8 +77,7 @@ type GenVars struct {
7277
config GenVarsConfig
7378
outString []string
7479
// rawMap is the internal object that holds the values of original token => retrieved value - decrypted in plain text
75-
rawMap ParsedMap
76-
mu sync.RWMutex
80+
rawMap muRawMap //ParsedMap
7781
}
7882

7983
// setValue implements GenVarsiface
@@ -100,7 +104,7 @@ func newGenVars() *GenVars {
100104
keySeparator: keySeparator,
101105
}
102106
return &GenVars{
103-
rawMap: m,
107+
rawMap: muRawMap{tokenMap: m},
104108
ctx: context.TODO(),
105109
// return using default config
106110
config: defaultConf,
@@ -134,20 +138,20 @@ func (c *GenVars) ConfigOutputPath() string {
134138
}
135139

136140
func (c *GenVars) RawMap() ParsedMap {
137-
c.mu.RLock()
138-
defer c.mu.RUnlock()
141+
c.rawMap.RLock()
142+
defer c.rawMap.RUnlock()
139143
// make a copy of the map
140144
m := make(ParsedMap)
141-
for k, v := range c.rawMap {
145+
for k, v := range c.rawMap.tokenMap {
142146
m[k] = v
143147
}
144148
return m
145149
}
146150

147151
func (c *GenVars) AddRawMap(key, val string) {
148-
c.mu.Lock()
149-
defer c.mu.Unlock()
150-
c.rawMap[key] = c.keySeparatorLookup(key, val)
152+
c.rawMap.Lock()
153+
defer c.rawMap.Unlock()
154+
c.rawMap.tokenMap[key] = c.keySeparatorLookup(key, val)
151155
}
152156

153157
// GenVarsConfig defines the input config object to be passed
@@ -317,11 +321,11 @@ func (c *GenVars) ConvertToExportVar() []string {
317321
for k, v := range c.RawMap() {
318322
rawKeyToken := strings.Split(k, "/") // assumes a path like token was used
319323
topLevelKey := rawKeyToken[len(rawKeyToken)-1]
320-
trm := &ParsedMap{}
321-
if parsedOk := isParsed(v, trm); parsedOk {
324+
trm := make(ParsedMap)
325+
if parsedOk := isParsed(v, &trm); parsedOk {
322326
// if is a map
323327
// try look up on key if separator defined
324-
normMap := c.envVarNormalize(*trm)
328+
normMap := c.envVarNormalize(trm)
325329
c.exportVars(normMap)
326330
continue
327331
}
@@ -332,7 +336,7 @@ func (c *GenVars) ConvertToExportVar() []string {
332336

333337
// envVarNormalize
334338
func (c *GenVars) envVarNormalize(pmap ParsedMap) ParsedMap {
335-
normalizedMap := ParsedMap{}
339+
normalizedMap := make(ParsedMap)
336340
for k, v := range pmap {
337341
normalizedMap[c.normalizeKey(k)] = v
338342
}

pkg/generator/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func Test_ConvertToExportVars(t *testing.T) {
253253
t.Run(name, func(t *testing.T) {
254254
f := newFixture(t)
255255
f.configGenVars(standardop, standardts)
256-
f.c.rawMap = tt.rawMap
256+
f.c.rawMap = muRawMap{tokenMap: tt.rawMap}
257257
f.c.ConvertToExportVar()
258258
got := f.c.outString
259259
if got == nil {

0 commit comments

Comments
 (0)