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

Add support for jsonpb-encoded workflow specs #193

Merged
merged 1 commit into from
Aug 23, 2018
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
16 changes: 11 additions & 5 deletions pkg/parse/protobuf/parser.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package protobuf

import (
"bytes"
"fmt"
"io"
"io/ioutil"

"github.com/fission/fission-workflows/pkg/types"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

Expand All @@ -27,11 +30,14 @@ func (p *Parser) Parse(r io.Reader) (*types.WorkflowSpec, error) {
if err != nil {
return nil, err
}
var wf *types.WorkflowSpec
err = proto.Unmarshal(bs, wf)
if err != nil {
return nil, err
wf := &types.WorkflowSpec{}
protoErr := proto.Unmarshal(bs, wf)
if protoErr != nil {
// Fallback: it might be protobuf serialized into json.
jsonErr := jsonpb.Unmarshal(bytes.NewReader(bs), wf)
if jsonErr != nil {
return nil, fmt.Errorf("failed to parse protobuf-encoded workflow (proto: %v, jsonpb: %v)", protoErr, jsonErr)
}
}

return wf, nil
}
71 changes: 71 additions & 0 deletions pkg/parse/protobuf/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package protobuf

import (
"bytes"
"strings"
"testing"

"github.com/fission/fission-workflows/pkg/types"
"github.com/fission/fission-workflows/pkg/types/typedvalues"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)

func TestParseProto(t *testing.T) {
originalWfSpec := &types.WorkflowSpec{
ApiVersion: types.WorkflowAPIVersion,
OutputTask: "fakeFinalTask",
Tasks: map[string]*types.TaskSpec{
"fakeFinalTask": {
FunctionRef: "noop",
Inputs: map[string]*types.TypedValue{
types.InputMain: typedvalues.MustParse("{$.Tasks.FirstTask.Output}"),
},
Requires: map[string]*types.TaskDependencyParameters{
"FirstTask": {},
},
},
"FirstTask": {
FunctionRef: "noop",
Inputs: map[string]*types.TypedValue{
types.InputMain: typedvalues.MustParse("{$.Invocation.Inputs.default.toUpperCase()}"),
},
},
},
}
msg, err := proto.Marshal(originalWfSpec)
assert.NoError(t, err)
parsedWfSpec, err := Parse(bytes.NewReader(msg))
assert.NoError(t, err)
assert.Equal(t, originalWfSpec, parsedWfSpec)
}

func TestParseJson(t *testing.T) {
originalWfSpec := &types.WorkflowSpec{
ApiVersion: types.WorkflowAPIVersion,
OutputTask: "fakeFinalTask",
Tasks: map[string]*types.TaskSpec{
"fakeFinalTask": {
FunctionRef: "noop",
Inputs: map[string]*types.TypedValue{
types.InputMain: typedvalues.MustParse("{$.Tasks.FirstTask.Output}"),
},
Requires: map[string]*types.TaskDependencyParameters{
"FirstTask": {},
},
},
"FirstTask": {
FunctionRef: "noop",
Inputs: map[string]*types.TypedValue{
types.InputMain: typedvalues.MustParse("{$.Invocation.Inputs.default.toUpperCase()}"),
},
},
},
}
msg, err := (&jsonpb.Marshaler{}).MarshalToString(originalWfSpec)
assert.NoError(t, err)
parsedWfSpec, err := Parse(strings.NewReader(msg))
assert.NoError(t, err)
assert.Equal(t, originalWfSpec, parsedWfSpec)
}