Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow for objects inside array metadata to be typed properly #9864

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
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
41 changes: 29 additions & 12 deletions master/internal/run/runs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run

import (
"encoding/json"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -101,19 +102,35 @@ func flattenMetadata(data map[string]any) (flatMetadata []model.RunMetadataIndex
flatMetadata = append(flatMetadata, newIndex)
continue
}
for key, value := range typedVal {
newPrefix := entry.prefix + entry.key + "."
if newPrefix == "." {
newPrefix = ""
if entry.array {
newIndex := model.RunMetadataIndex{
FlatKey: entry.prefix + entry.key,
IsArrayElement: true,
}
objJSON, err := json.Marshal(entry.value)
if err != nil {
return nil, fmt.Errorf(
"failed to marshall metadata object in array %w",
err,
)
}
newIndex.StringValue = ptrs.Ptr(string(objJSON))
flatMetadata = append(flatMetadata, newIndex)
} else {
for key, value := range typedVal {
newPrefix := entry.prefix + entry.key + "."
if newPrefix == "." {
newPrefix = ""
}
// push the key-value pair onto the stack
stack = append(stack, metadataEntry{
prefix: newPrefix,
key: key,
value: value,
depth: entry.depth + 1,
array: false,
})
}
// push the key-value pair onto the stack
stack = append(stack, metadataEntry{
prefix: newPrefix,
key: key,
value: value,
depth: entry.depth + 1,
array: entry.array,
})
}
// if the value is a slice, push each element onto the stack.
case []any:
Expand Down
28 changes: 4 additions & 24 deletions master/internal/run/runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ func TestFlattenMetadata(t *testing.T) {
},
{
RunID: 0,
FlatKey: "key7.key8",
IntegerValue: ptrs.Ptr(3),
ProjectID: 0,
FlatKey: "key7",
StringValue: ptrs.Ptr(`{"key8":3}`),
IsArrayElement: true,
},
{
Expand Down Expand Up @@ -170,8 +169,8 @@ func TestFlattenMetadataArrayNested(t *testing.T) {
require.ElementsMatch(t, []model.RunMetadataIndex{
{
RunID: 0,
FlatKey: "key1.key2",
IntegerValue: ptrs.Ptr(1),
FlatKey: "key1",
StringValue: ptrs.Ptr("{\"key2\":1}"),
ProjectID: 0,
IsArrayElement: true,
},
Expand Down Expand Up @@ -289,25 +288,6 @@ func TestFlattenMetadataArrayElementTooLongKey(t *testing.T) {
)
}

func TestFlattenMetadataArrayElementTooLongKeyInNestedArray(t *testing.T) {
data := map[string]interface{}{
"key1": []interface{}{
map[string]interface{}{
"key2": 1,
},
},
}
data["key1"].([]interface{})[0].(map[string]interface{})[strings.Repeat("a", MaxMetadataKeyLength+1)] = 2
_, err := FlattenMetadata(data)
require.ErrorContains(
t,
err,
fmt.Sprintf("metadata key exceeds maximum length of %d characters",
MaxMetadataKeyLength,
),
)
}

func TestFlattenMetadataNestingTooDeep(t *testing.T) {
data := map[string]interface{}{}
current := data
Expand Down
Loading