Skip to content

Commit

Permalink
Adding package comments for data package
Browse files Browse the repository at this point in the history
Adds package comments and extra test cases
  • Loading branch information
MbolotSuse committed Feb 5, 2024
1 parent 63eb31b commit c29289d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/data/values.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Package data contains functions for working with unstructured values like interfaces or map[string]interface{}
// allows reading/writing to these values without having to convert to structured items.
package data

import (
"strconv"
)

// RemoveValue removes a value from data. Keys should be in order of precedence (i.e. "metadata", "annotations") means
// remove the "annotations" key from the "metadata" sub-map. Returns the removed value (if any) and a bool indicating
// if the value was found.
func RemoveValue(data map[string]interface{}, keys ...string) (interface{}, bool) {
for i, key := range keys {
if i == len(keys)-1 {
Expand All @@ -22,6 +27,9 @@ func GetValueN(data map[string]interface{}, keys ...string) interface{} {
return val
}

// GetValue gets a value from the provided data field, which must be a map[string]interface or a []interface. Keys can
// be the string keys (in order of precedence) or can be the index (as a string) of the item to be retrieved. Returns
// the retrieved value (if any) and a bool indicating if they value was found.
func GetValue(data interface{}, keys ...string) (interface{}, bool) {
for i, key := range keys {
if i == len(keys)-1 {
Expand Down Expand Up @@ -54,6 +62,8 @@ func itemByIndex(dataSlice []interface{}, key string) (interface{}, bool) {
return dataSlice[keyInt], true
}

// PutValue puts val into data (in place) at the index specified by keys (specified in order of precedence). If there
// is no current entry at a key, a new map is created for that value.
func PutValue(data map[string]interface{}, val interface{}, keys ...string) {
if data == nil {
return
Expand Down
37 changes: 36 additions & 1 deletion pkg/data/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestGetValue(t *testing.T) {
wantSuccess: true,
},
{
name: "get index of top levelslice",
name: "get index of top level slice",
data: []interface{}{
"alice",
"bob",
Expand All @@ -92,6 +92,26 @@ func TestGetValue(t *testing.T) {
wantValue: "eve",
wantSuccess: true,
},
{
name: "slice of maps",
data: []interface{}{
map[string]interface{}{
"notthisone": "val",
},
map[string]interface{}{
"parent": map[string]interface{}{
"children": []interface{}{
"alice",
"bob",
"eve",
},
},
},
},
keys: []string{"1", "parent", "children", "0"},
wantValue: "alice",
wantSuccess: true,
},
{
name: "index is too big",
data: map[string]interface{}{
Expand Down Expand Up @@ -122,6 +142,21 @@ func TestGetValue(t *testing.T) {
wantValue: nil,
wantSuccess: false,
},
{
name: "index not parseable to int",
data: map[string]interface{}{
"parent": map[string]interface{}{
"children": []interface{}{
"alice",
"bob",
"eve",
},
},
},
keys: []string{"parent", "children", "notanint"},
wantValue: nil,
wantSuccess: false,
},
}
for _, test := range tests {
test := test
Expand Down

0 comments on commit c29289d

Please sign in to comment.