Skip to content

Commit

Permalink
Merge pull request #461 from pzaino/develop
Browse files Browse the repository at this point in the history
Fixed a bug that may have caused some crash if a nil value was passedto the Key Value DB
  • Loading branch information
pzaino authored Oct 5, 2024
2 parents ff0c173 + 0e47341 commit 5ddbed5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/common/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,25 @@ func createKeyWithCtx(key string, ctxID string) string {

// Set sets a value (either string or []string) along with its properties for a given key and context.
func (kv *KeyValueStore) Set(key string, value interface{}, properties Properties) error {
if kv == nil {
kv = NewKeyValueStore()
}

// Check if the key is empty
key = strings.TrimSpace(key)
if key == "" {
return errors.New("key cannot be empty")
}

// Check if value is nil
if value == nil {
value = ""
}

// Store the type of the value in the properties
if strings.TrimSpace(properties.Type) == "" {
if properties == (Properties{}) {
properties.Type = reflect.TypeOf(value).String()
} else if strings.TrimSpace(properties.Type) == "" {
properties.Type = reflect.TypeOf(value).String()
}

Expand Down Expand Up @@ -112,6 +124,10 @@ func (kv *KeyValueStore) Set(key string, value interface{}, properties Propertie

// Get retrieves the value (which could be string or []string) and properties for a given key and context.
func (kv *KeyValueStore) Get(key string, ctxID string) (interface{}, Properties, error) {
if kv == nil {
kv = NewKeyValueStore()
}

fullKey := createKeyWithCtx(key, ctxID)
kv.mutex.RLock()
defer kv.mutex.RUnlock()
Expand Down

0 comments on commit 5ddbed5

Please sign in to comment.