Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ func MergeConfig(configRecords []*fftypes.ConfigRecord) error {
return nil
}

var root = &configPrefix{
keys: map[string]bool{}, // All keys go here, including those defined in sub prefixies
}
var knownKeys = map[string]bool{} // All keys go here, including those defined in sub prefixies
var keysMutex sync.Mutex
var root = &configPrefix{}

// ark adds a root key, used to define the keys that are used within the core
func rootKey(k string) RootKey {
Expand All @@ -406,9 +406,9 @@ func rootKey(k string) RootKey {
// GetKnownKeys gets the known keys
func GetKnownKeys() []string {
var keys []string
root.keysMutex.Lock()
defer root.keysMutex.Unlock()
for k := range root.keys {
keysMutex.Lock()
defer keysMutex.Unlock()
for k := range knownKeys {
keys = append(keys, k)
}
sort.Strings(keys)
Expand All @@ -417,9 +417,7 @@ func GetKnownKeys() []string {

// configPrefix is the main config structure passed to plugins, and used for root to wrap viper
type configPrefix struct {
prefix string
keys map[string]bool
keysMutex sync.Mutex
prefix string
}

// configPrefixArray is a point in the config that supports an array
Expand All @@ -435,15 +433,14 @@ func NewPluginConfig(prefix string) Prefix {
}
return &configPrefix{
prefix: prefix,
keys: root.keys,
}
}

func (c *configPrefix) prefixKey(k string) string {
c.keysMutex.Lock()
defer c.keysMutex.Unlock()
keysMutex.Lock()
defer keysMutex.Unlock()
key := c.prefix + k
if !c.keys[key] {
if !knownKeys[key] {
panic(fmt.Sprintf("Undefined configuration key '%s'", key))
}
return key
Expand All @@ -452,7 +449,6 @@ func (c *configPrefix) prefixKey(k string) string {
func (c *configPrefix) SubPrefix(suffix string) Prefix {
return &configPrefix{
prefix: c.prefix + suffix + ".",
keys: root.keys,
}
}

Expand All @@ -476,7 +472,6 @@ func (c *configPrefixArray) ArraySize() int {
func (c *configPrefixArray) ArrayEntry(i int) Prefix {
cp := &configPrefix{
prefix: c.base + fmt.Sprintf(".%d.", i),
keys: root.keys,
}
for knownKey, defValue := range c.defaults {
cp.AddKnownKey(knownKey, defValue...)
Expand All @@ -495,9 +490,9 @@ func (c *configPrefixArray) ArrayEntry(i int) Prefix {

func (c *configPrefixArray) AddKnownKey(k string, defValue ...interface{}) {
// Put a simulated key in the known keys array, to pop into the help info.
root.keysMutex.Lock()
defer root.keysMutex.Unlock()
root.keys[fmt.Sprintf("%s[].%s", c.base, k)] = true
keysMutex.Lock()
defer keysMutex.Unlock()
knownKeys[fmt.Sprintf("%s[].%s", c.base, k)] = true
c.defaults[k] = defValue
}

Expand All @@ -508,9 +503,9 @@ func (c *configPrefix) AddKnownKey(k string, defValue ...interface{}) {
} else if len(defValue) > 0 {
c.SetDefault(k, defValue)
}
c.keysMutex.Lock()
defer c.keysMutex.Unlock()
c.keys[key] = true
keysMutex.Lock()
defer keysMutex.Unlock()
knownKeys[key] = true
}

func (c *configPrefix) SetDefault(k string, defValue interface{}) {
Expand Down