-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Perform fields filtering server side (#4595)
Signed-off-by: Simon Behar <simbeh7@gmail.com>
- Loading branch information
Showing
5 changed files
with
207 additions
and
49 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
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,74 @@ | ||
package fields | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func CleanFields(fieldsQuery string, dataBytes []byte) ([]byte, error) { | ||
exclude := false | ||
fields := make(map[string]interface{}) | ||
if fieldsQuery != "" { | ||
if strings.HasPrefix(fieldsQuery, "-") { | ||
fieldsQuery = fieldsQuery[1:] | ||
exclude = true | ||
} | ||
for _, field := range strings.Split(fieldsQuery, ",") { | ||
fields[field] = true | ||
} | ||
} | ||
|
||
data := make(map[string]interface{}) | ||
err := json.Unmarshal(dataBytes, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = processItem([]string{}, data, exclude, fields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
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 { | ||
if mapItem, ok := item.(map[string]interface{}); ok { | ||
for k, v := range mapItem { | ||
|
||
fieldPath := strings.Join(append(path, k), ".") | ||
_, pathIn := fields[fieldPath] | ||
parentPathIn := pathIn | ||
if !parentPathIn { | ||
for k := range fields { | ||
if strings.HasPrefix(k, fieldPath) { | ||
parentPathIn = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
if exclude && !pathIn || !exclude && parentPathIn { | ||
if !pathIn { | ||
if err := processItem(append(path, k), v, exclude, fields); err != nil { | ||
return err | ||
} | ||
} | ||
} 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 | ||
} | ||
} | ||
return nil | ||
} | ||
return fmt.Errorf("cannot process item for fields, unknown format") | ||
} |
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,97 @@ | ||
package fields | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1" | ||
) | ||
|
||
var sampleWorkflow = ` | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
name: hello-world-qgpxz | ||
spec: | ||
arguments: {} | ||
entrypoint: whalesay | ||
templates: | ||
- arguments: {} | ||
container: | ||
args: | ||
- hello world | ||
command: | ||
- cowsay | ||
image: docker/whalesay:latest | ||
name: "" | ||
resources: {} | ||
inputs: {} | ||
metadata: {} | ||
name: whalesay | ||
outputs: {} | ||
status: | ||
artifactRepositoryRef: | ||
configMap: artifact-repositories | ||
key: default-v1 | ||
namespace: argo | ||
conditions: | ||
- status: "True" | ||
type: Completed | ||
finishedAt: "2020-12-01T17:30:51Z" | ||
nodes: | ||
hello-world-qgpxz: | ||
displayName: hello-world-qgpxz | ||
phase: Succeeded | ||
progress: 1/1 | ||
resourcesDuration: | ||
cpu: 3 | ||
memory: 1 | ||
startedAt: "2020-12-01T17:30:46Z" | ||
` | ||
|
||
func TestCleanFields(t *testing.T) { | ||
var wf v1alpha1.Workflow | ||
err := yaml.Unmarshal([]byte(sampleWorkflow), &wf) | ||
assert.NoError(t, err) | ||
|
||
jsonWf, err := json.Marshal(wf) | ||
assert.NoError(t, err) | ||
|
||
cleanJsonWf, err := CleanFields("status.phase,metadata.name,spec.entrypoint", jsonWf) | ||
assert.NoError(t, err) | ||
|
||
var cleanWf v1alpha1.Workflow | ||
err = json.Unmarshal(cleanJsonWf, &cleanWf) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, "Succeeded", string(cleanWf.Status.Phase)) | ||
assert.Equal(t, "whalesay", cleanWf.Spec.Entrypoint) | ||
assert.Equal(t, "hello-world-qgpxz", cleanWf.Name) | ||
|
||
assert.Nil(t, cleanWf.Status.Nodes) | ||
} | ||
|
||
func TestCleanFieldsExclude(t *testing.T) { | ||
var wf v1alpha1.Workflow | ||
err := yaml.Unmarshal([]byte(sampleWorkflow), &wf) | ||
assert.NoError(t, err) | ||
|
||
jsonWf, err := json.Marshal(wf) | ||
assert.NoError(t, err) | ||
|
||
cleanJsonWf, err := CleanFields("-status.phase,metadata.name,spec.entrypoint", jsonWf) | ||
assert.NoError(t, err) | ||
|
||
var cleanWf v1alpha1.Workflow | ||
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.NotNil(t, cleanWf.Status.Nodes) | ||
} |
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