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

Remove and refactor rest protobuf types #1296

Merged
merged 2 commits into from
Sep 4, 2019
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
11 changes: 2 additions & 9 deletions event/event.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package event

import (
"github.com/gogo/protobuf/types"
"github.com/mesg-foundation/engine/hash"
)

// Event stores all informations about Events.
type Event struct {
Hash hash.Hash `hash:"-"`
InstanceHash hash.Hash `hash:"name:instanceHash"`
Key string `hash:"name:key"`
Data map[string]interface{} `hash:"name:data"`
}

// Create creates an event eventKey with eventData for service s.
func Create(instanceHash hash.Hash, eventKey string, eventData map[string]interface{}) *Event {
func Create(instanceHash hash.Hash, eventKey string, eventData *types.Struct) *Event {
e := &Event{
InstanceHash: instanceHash,
Key: eventKey,
Expand Down
48 changes: 24 additions & 24 deletions protobuf/types/event.pb.go → event/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 13 additions & 56 deletions execution/execution.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,12 @@
package execution

import (
"github.com/gogo/protobuf/types"
"github.com/mesg-foundation/engine/hash"
)

// Status stores the state of an execution
type Status int

// Status for an execution
// Created => The execution is created but not yet processed
// InProgress => The execution is being processed
// Completed => The execution is completed
const (
Created Status = iota + 1
InProgress
Completed
Failed
)

func (s Status) String() (r string) {
switch s {
case Created:
r = "created"
case InProgress:
r = "in progress"
case Completed:
r = "completed"
case Failed:
r = "failed"
}
return r
}

// Execution stores all information about executions.
type Execution struct {
Hash hash.Hash `hash:"-"`
ProcessHash hash.Hash `hash:"name:processHash"`
ParentHash hash.Hash `hash:"name:parentHash"`
EventHash hash.Hash `hash:"name:eventHash"`
Status Status `hash:"-"`
InstanceHash hash.Hash `hash:"name:instanceHash"`
TaskKey string `hash:"name:taskKey"`
StepID string `hash:"name:stepID"`
Tags []string `hash:"name:tags"`
Inputs map[string]interface{} `hash:"name:inputs"`
Outputs map[string]interface{} `hash:"-"`
Error string `hash:"-"`
}

// New returns a new execution. It returns an error if inputs are invalid.
func New(processHash, instanceHash, parentHash, eventHash hash.Hash, stepID string, taskKey string, inputs map[string]interface{}, tags []string) *Execution {
func New(processHash, instanceHash, parentHash, eventHash hash.Hash, stepID string, taskKey string, inputs *types.Struct, tags []string) *Execution {
exec := &Execution{
ProcessHash: processHash,
EventHash: eventHash,
Expand All @@ -59,7 +16,7 @@ func New(processHash, instanceHash, parentHash, eventHash hash.Hash, stepID stri
TaskKey: taskKey,
StepID: stepID,
Tags: tags,
Status: Created,
Status: Status_Created,
}
exec.Hash = hash.Dump(exec)
return exec
Expand All @@ -68,42 +25,42 @@ func New(processHash, instanceHash, parentHash, eventHash hash.Hash, stepID stri
// Execute changes executions status to in progres and update its execute time.
// It returns an error if the status is different then Created.
func (execution *Execution) Execute() error {
if execution.Status != Created {
if execution.Status != Status_Created {
return StatusError{
ExpectedStatus: Created,
ExpectedStatus: Status_Created,
ActualStatus: execution.Status,
}
}
execution.Status = InProgress
execution.Status = Status_InProgress
return nil
}

// Complete changes execution status to completed. It verifies the output.
// It returns an error if the status is different then InProgress or verification fails.
func (execution *Execution) Complete(outputs map[string]interface{}) error {
if execution.Status != InProgress {
func (execution *Execution) Complete(outputs *types.Struct) error {
if execution.Status != Status_InProgress {
return StatusError{
ExpectedStatus: InProgress,
ExpectedStatus: Status_InProgress,
ActualStatus: execution.Status,
}
}

execution.Outputs = outputs
execution.Status = Completed
execution.Status = Status_Completed
return nil
}

// Failed changes execution status to failed and puts error information to execution.
// It returns an error if the status is different then InProgress.
func (execution *Execution) Failed(err error) error {
if execution.Status != InProgress {
if execution.Status != Status_InProgress {
return StatusError{
ExpectedStatus: InProgress,
ExpectedStatus: Status_InProgress,
ActualStatus: execution.Status,
}
}

execution.Error = err.Error()
execution.Status = Failed
execution.Status = Status_Failed
return nil
}
82 changes: 41 additions & 41 deletions protobuf/types/execution.pb.go → execution/execution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 11 additions & 17 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"testing/quick"

"github.com/gogo/protobuf/types"
"github.com/mesg-foundation/engine/hash"
"github.com/stretchr/testify/require"
)
Expand All @@ -24,26 +25,26 @@ func TestNewFromService(t *testing.T) {
require.Equal(t, parentHash, execution.ParentHash)
require.Equal(t, eventHash, execution.EventHash)
require.Equal(t, taskKey, execution.TaskKey)
require.Equal(t, map[string]interface{}(nil), execution.Inputs)
require.Equal(t, (*types.Struct)(nil), execution.Inputs)
require.Equal(t, tags, execution.Tags)
require.Equal(t, Created, execution.Status)
require.Equal(t, Status_Created, execution.Status)
}

func TestExecute(t *testing.T) {
e := New(nil, nil, nil, nil, "", "", nil, nil)
require.NoError(t, e.Execute())
require.Equal(t, InProgress, e.Status)
require.Equal(t, Status_InProgress, e.Status)
require.Error(t, e.Execute())
}

func TestComplete(t *testing.T) {
output := map[string]interface{}{"foo": "bar"}
var output types.Struct
e := New(nil, nil, nil, nil, "", "", nil, nil)

e.Execute()
require.NoError(t, e.Complete(output))
require.Equal(t, Completed, e.Status)
require.Equal(t, output, e.Outputs)
require.NoError(t, e.Complete(&output))
require.Equal(t, Status_Completed, e.Status)
require.Equal(t, &output, e.Outputs)
require.Error(t, e.Complete(nil))
}

Expand All @@ -52,23 +53,16 @@ func TestFailed(t *testing.T) {
e := New(nil, nil, nil, nil, "", "", nil, nil)
e.Execute()
require.NoError(t, e.Failed(err))
require.Equal(t, Failed, e.Status)
require.Equal(t, Status_Failed, e.Status)
require.Equal(t, err.Error(), e.Error)
require.Error(t, e.Failed(err))
}

func TestStatus(t *testing.T) {
require.Equal(t, "created", Created.String())
require.Equal(t, "in progress", InProgress.String())
require.Equal(t, "completed", Completed.String())
require.Equal(t, "failed", Failed.String())
}

func TestExecutionHash(t *testing.T) {
ids := make(map[string]bool)

f := func(instanceHash, parentHash, eventID []byte, taskKey, input string, tags []string) bool {
e := New(nil, instanceHash, parentHash, eventID, "", taskKey, map[string]interface{}{"input": input}, tags)
f := func(instanceHash, parentHash, eventID []byte, taskKey string, tags []string) bool {
e := New(nil, instanceHash, parentHash, eventID, "", taskKey, nil, tags)
if ids[string(e.Hash)] {
return false
}
Expand Down
Loading