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: Fix TestCleanFieldsExclude #4625

Merged
merged 2 commits into from
Dec 2, 2020
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
19 changes: 4 additions & 15 deletions util/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fields

import (
"encoding/json"
"fmt"
"strings"
)

Expand All @@ -24,18 +23,15 @@ func CleanFields(fieldsQuery string, dataBytes []byte) ([]byte, error) {
if err != nil {
return nil, err
}
err = processItem([]string{}, data, exclude, fields)
if err != nil {
return nil, err
}
processItem([]string{}, data, exclude, fields)
clean, err := json.Marshal(data)
if err != nil {
return nil, err
}
return clean, nil
}

func processItem(path []string, item interface{}, exclude bool, fields map[string]interface{}) error {
func processItem(path []string, item interface{}, exclude bool, fields map[string]interface{}) {
if mapItem, ok := item.(map[string]interface{}); ok {
for k, v := range mapItem {

Expand All @@ -53,22 +49,15 @@ func processItem(path []string, item interface{}, exclude bool, fields map[strin

if exclude && !pathIn || !exclude && parentPathIn {
if !pathIn {
if err := processItem(append(path, k), v, exclude, fields); err != nil {
return err
}
processItem(append(path, k), v, exclude, fields)
}
} else {
delete(mapItem, k)
}
}
return nil
} else if arrayItem, ok := item.([]interface{}); ok {
for i := range arrayItem {
if err := processItem(path, arrayItem[i], exclude, fields); err != nil {
return err
}
processItem(path, arrayItem[i], exclude, fields)
}
return nil
}
return fmt.Errorf("cannot process item for fields, unknown format")
}
6 changes: 3 additions & 3 deletions util/fields/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func TestCleanFieldsExclude(t *testing.T) {
err = json.Unmarshal(cleanJsonWf, &cleanWf)
assert.NoError(t, err)

assert.Nil(t, string(cleanWf.Status.Phase))
assert.Nil(t, cleanWf.Spec.Entrypoint)
assert.Nil(t, cleanWf.Name)
assert.Empty(t, string(cleanWf.Status.Phase))
assert.Empty(t, cleanWf.Spec.Entrypoint)
assert.Empty(t, cleanWf.Name)

assert.NotNil(t, cleanWf.Status.Nodes)
}