-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
223 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package httphandler | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
) | ||
|
||
func collectParams(params map[string][]string) map[string]string { | ||
paramMap := make(map[string]string) | ||
for key, values := range params { | ||
if len(values) > 0 { | ||
sanitizedValue := sanitizeInput(values[0]) | ||
paramMap[key] = sanitizedValue | ||
} | ||
} | ||
return paramMap | ||
} | ||
|
||
func addTimestampToThisData(paramMap map[string]string, path string) { | ||
// if using patch and path is empty than add a timestamp with the value suffix | ||
if path == "" { | ||
allKeys := make([]string, 0, len(paramMap)) | ||
for k := range paramMap { | ||
allKeys = append(allKeys, k) | ||
} | ||
// add a timestamp with the value suffix for all keys | ||
timestamp := time.Now().UTC().Format(time.RFC3339) | ||
for _, k := range allKeys { | ||
paramMap[k+"_timestamp"] = timestamp | ||
} | ||
} | ||
|
||
timestamp := time.Now().UTC().Format(time.RFC3339) | ||
paramMap["timestamp"] = timestamp | ||
} | ||
|
||
func (c Config) modifyData(downloadKey string, paramMap map[string]string, path string, isPatch bool) (map[string]interface{}, error) { | ||
var dataToStore map[string]interface{} | ||
|
||
if isPatch { | ||
existingData, err := c.StorageInstance.Retrieve(downloadKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mergeData(existingData, paramMap, strings.Split(path, "/")) | ||
dataToStore = existingData | ||
} else { | ||
dataToStore = make(map[string]interface{}) | ||
for k, v := range paramMap { | ||
dataToStore[k] = v | ||
} | ||
} | ||
|
||
// add timestamp so that root level timestamp is always the latest of any updated value | ||
timestamp := time.Now().UTC().Format(time.RFC3339) | ||
dataToStore["timestamp"] = timestamp | ||
|
||
return dataToStore, nil | ||
} | ||
|
||
func mergeData(existingData map[string]interface{}, newData map[string]string, path []string) { | ||
if len(path) == 0 || (len(path) == 1 && path[0] == "") { | ||
for k, v := range newData { | ||
existingData[k] = v | ||
} | ||
return | ||
} | ||
|
||
currentKey := path[0] | ||
if _, exists := existingData[currentKey]; !exists { | ||
existingData[currentKey] = make(map[string]interface{}) | ||
} | ||
|
||
if nestedMap, ok := existingData[currentKey].(map[string]interface{}); ok { | ||
mergeData(nestedMap, newData, path[1:]) | ||
} else { | ||
existingData[currentKey] = newData | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package httphandler | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMergeData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
existingData map[string]interface{} | ||
newData map[string]string | ||
path []string | ||
expectedData map[string]interface{} | ||
}{ | ||
{ | ||
name: "Empty path", | ||
existingData: map[string]interface{}{ | ||
"key1": "value1", | ||
}, | ||
newData: map[string]string{ | ||
"key2": "value2", | ||
}, | ||
path: []string{}, | ||
expectedData: map[string]interface{}{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Single empty string in path", | ||
existingData: map[string]interface{}{ | ||
"key1": "value1", | ||
}, | ||
newData: map[string]string{ | ||
"key2": "value2", | ||
}, | ||
path: []string{""}, | ||
expectedData: map[string]interface{}{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Nested merge", | ||
existingData: map[string]interface{}{ | ||
"key1": map[string]interface{}{ | ||
"subkey1": "subvalue1", | ||
}, | ||
}, | ||
newData: map[string]string{ | ||
"subkey2": "subvalue2", | ||
}, | ||
path: []string{"key1"}, | ||
expectedData: map[string]interface{}{ | ||
"key1": map[string]interface{}{ | ||
"subkey1": "subvalue1", | ||
"subkey2": "subvalue2", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Override non-map value with new data", | ||
existingData: map[string]interface{}{ | ||
"key1": "value1", | ||
}, | ||
newData: map[string]string{ | ||
"subkey1": "subvalue1", | ||
}, | ||
path: []string{"key1"}, | ||
expectedData: map[string]interface{}{ | ||
"key1": map[string]string{ | ||
"subkey1": "subvalue1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Deeply nested merge", | ||
existingData: map[string]interface{}{ | ||
"key1": map[string]interface{}{ | ||
"subkey1": map[string]interface{}{ | ||
"subsubkey1": "subsubvalue1", | ||
}, | ||
}, | ||
}, | ||
newData: map[string]string{ | ||
"subsubkey2": "subsubvalue2", | ||
}, | ||
path: []string{"key1", "subkey1"}, | ||
expectedData: map[string]interface{}{ | ||
"key1": map[string]interface{}{ | ||
"subkey1": map[string]interface{}{ | ||
"subsubkey1": "subsubvalue1", | ||
"subsubkey2": "subsubvalue2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Create new nested map", | ||
existingData: map[string]interface{}{ | ||
"key1": "value1", | ||
}, | ||
newData: map[string]string{ | ||
"subkey1": "subvalue1", | ||
}, | ||
path: []string{"key2"}, | ||
expectedData: map[string]interface{}{ | ||
"key1": "value1", | ||
"key2": map[string]interface{}{ | ||
"subkey1": "subvalue1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mergeData(tt.existingData, tt.newData, tt.path) | ||
if !reflect.DeepEqual(tt.existingData, tt.expectedData) { | ||
t.Errorf("mergeData() = %v, want %v", tt.existingData, tt.expectedData) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters