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 marshal issue with process filter #1286

Merged
merged 3 commits into from
Sep 2, 2019
Merged
Changes from 2 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
87 changes: 52 additions & 35 deletions process/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (t Task) MarshalJSON() ([]byte, error) {
})
}

// MarshalJSON for the task
// MarshalJSON for the result
func (r Result) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "result",
Expand All @@ -27,7 +27,7 @@ func (r Result) MarshalJSON() ([]byte, error) {
})
}

// MarshalJSON for the task
// MarshalJSON for the event
func (e Event) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "event",
Expand All @@ -37,19 +37,24 @@ func (e Event) MarshalJSON() ([]byte, error) {
})
}

// MarshalJSON for the task
// MarshalJSON for the map
func (m Map) MarshalJSON() ([]byte, error) {
// outputs, err := json.Marshal(m.Outputs)
// if err != nil {
// return nil, err
// }
return json.Marshal(map[string]interface{}{
"type": "map",
"key": m.Key,
"outputs": m.Outputs,
})
}

// MarshalJSON for the filter
func (f Filter) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "filter",
"key": f.Key,
"conditions": f.Conditions,
})
}

// UnmarshalJSON unmashals a process
func (w *Process) UnmarshalJSON(b []byte) error {
var objMap map[string]*json.RawMessage
Expand Down Expand Up @@ -94,39 +99,51 @@ func (w *Process) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
nodeType := nodeInfo["type"]
switch nodeType {
case "task":
var node Task
if err := json.Unmarshal(marshalData, &node); err != nil {
return err
}
w.Graph.Nodes[i] = &node
case "event":
var node Event
if err := json.Unmarshal(marshalData, &node); err != nil {
return err
}
w.Graph.Nodes[i] = &node
case "result":
var node Result
if err := json.Unmarshal(marshalData, &node); err != nil {
return err
}
w.Graph.Nodes[i] = &node
case "map":
var node Map
if err := json.Unmarshal(marshalData, &node); err != nil {
return err
}
w.Graph.Nodes[i] = &node
default:
return fmt.Errorf("type %q not supported", nodeType)
w.Graph.Nodes[i], err = w.unmarshalNode(nodeInfo["type"].(string), marshalData)
if err != nil {
return err
}
}
return nil
}

func (w *Process) unmarshalNode(nodeType string, marshalData []byte) (Node, error) {
switch nodeType {
case "task":
var node Task
if err := json.Unmarshal(marshalData, &node); err != nil {
return nil, err
}
return &node, nil
case "event":
var node Event
if err := json.Unmarshal(marshalData, &node); err != nil {
return nil, err
}
return &node, nil
case "result":
var node Result
if err := json.Unmarshal(marshalData, &node); err != nil {
return nil, err
}
return &node, nil
case "map":
var node Map
if err := json.Unmarshal(marshalData, &node); err != nil {
return nil, err
}
return &node, nil
case "filter":
var node Filter
if err := json.Unmarshal(marshalData, &node); err != nil {
return nil, err
}
return &node, nil
default:
return nil, fmt.Errorf("type %q not supported", nodeType)
}
}

func (w *Process) preprocessUnmashalNode(nodeInfo map[string]interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
for key, value := range nodeInfo {
Expand Down