Skip to content

Commit

Permalink
fix mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Aug 20, 2020
2 parents 0e8e7cd + 42a93b8 commit 3a589f7
Show file tree
Hide file tree
Showing 12 changed files with 692 additions and 105 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ lint-examples:
golangci-lint run -v -E ${LINT_SETTINGS}

lint: | lint-examples
golangci-lint run -v -E ${LINT_SETTINGS},gomnd;
golangci-lint run -v -E ${LINT_SETTINGS},gomnd; \
make check-comments;

format:
gofmt -s -w -l .
Expand Down
25 changes: 25 additions & 0 deletions constructor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ package constructor
import "errors"

var (
// ErrInvalidJSON is returned when a populated value is not valid JSON.
ErrInvalidJSON = errors.New("populated input is not valid JSON")

// ErrVariableNotFound is returned when a variable is not
// present in a Job's state.
ErrVariableNotFound = errors.New("variable not found")

// ErrVariableIncorrectFormat is returned when a variable
// is in the incorrect format (i.e. when we find an int
// instead of a string).
ErrVariableIncorrectFormat = errors.New("variable in incorrect format")

// ErrJobComplete is returned when there are no more scenarios
// to process in a Job.
ErrJobComplete = errors.New("job complete")
Expand All @@ -44,4 +52,21 @@ var (

// ErrActionFailed is returned when Action exeuction fails with a valid input.
ErrActionFailed = errors.New("action execution failed")

// ErrOperationFormat is returned when []*types.Operation cannot be unmarshaled
// from <scenario_name>.operations.
ErrOperationFormat = errors.New("operation format")

// ErrConfirmationDepthInvalid is returned when <scenario_name>.operations
// are populated, but confirmation depth is missing or invalid.
ErrConfirmationDepthInvalid = errors.New("invalid confirmation depth")

// ErrNetworkInvalid is returned when <scenario_name>.operations
// are populated, but network is missing or invalid.
ErrNetworkInvalid = errors.New("network invalid")

// ErrMetadataInvalid is returned when <scenario_name>.operations
// are populated, but construction preprocess metadata is
// invalid (ok to be missing).
ErrMetadataInvalid = errors.New("metadata invalid")
)
48 changes: 34 additions & 14 deletions constructor/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package constructor

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"

"github.com/coinbase/rosetta-sdk-go/types"
"github.com/tidwall/gjson"
Expand All @@ -37,7 +37,26 @@ func NewJob(workflow *Workflow) *Job {
}
}

func (j *Job) unmarshalReserved(
func (j *Job) unmarshalNumber(
scenarioName string,
reservedVariable ReservedVariable,
) (*big.Int, error) {
variable := fmt.Sprintf("%s.%s", scenarioName, reservedVariable)

value := gjson.Get(j.State, variable)
if !value.Exists() {
return nil, ErrVariableNotFound
}

i, ok := new(big.Int).SetString(value.String(), 10)
if !ok {
return nil, ErrVariableIncorrectFormat
}

return i, nil
}

func (j *Job) unmarshalStruct(
scenarioName string,
reservedVariable ReservedVariable,
output interface{},
Expand All @@ -49,11 +68,7 @@ func (j *Job) unmarshalReserved(
return ErrVariableNotFound
}

if err := json.Unmarshal([]byte(value.Raw), &output); err != nil {
return fmt.Errorf("%w: could not unmarshal variable %s", err, variable)
}

return nil
return unmarshalInput([]byte(value.Raw), output)
}

func (j *Job) checkComplete() bool {
Expand Down Expand Up @@ -85,7 +100,7 @@ func (j *Job) Process(
scenario := j.Scenarios[broadcastIndex]

var operations []*types.Operation
err := j.unmarshalReserved(scenario.Name, Operations, &operations)
err := j.unmarshalStruct(scenario.Name, Operations, &operations)
if errors.Is(err, ErrVariableNotFound) {
// If <scenario.Name>.operations are not provided, no broadcast
// is required.
Expand All @@ -97,27 +112,32 @@ func (j *Job) Process(
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal operations", err)
return nil, fmt.Errorf("%w: %s", ErrOperationFormat, err.Error())
}

confirmationDepth, err := j.unmarshalNumber(scenario.Name, ConfirmationDepth)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrConfirmationDepthInvalid, err.Error())
}

var network types.NetworkIdentifier
err = j.unmarshalReserved(scenario.Name, Network, &network)
err = j.unmarshalStruct(scenario.Name, Network, &network)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal network", err)
return nil, fmt.Errorf("%w: %s", ErrNetworkInvalid, err.Error())
}

var metadata map[string]interface{}
err = j.unmarshalReserved(scenario.Name, PreprocessMetadata, &metadata)
err = j.unmarshalStruct(scenario.Name, PreprocessMetadata, &metadata)
if err != nil && !errors.Is(err, ErrVariableNotFound) {
return nil, fmt.Errorf("%w: could not unmarshal preprocess metadata", err)
return nil, fmt.Errorf("%w: %s", ErrMetadataInvalid, err.Error())
}

j.Status = Broadcasting
return &Broadcast{
Network: &network,
Intent: operations,
Metadata: metadata,
ConfirmationDepth: scenario.ConfirmationDepth,
ConfirmationDepth: confirmationDepth.Int64(),
}, nil
}

Expand Down

0 comments on commit 3a589f7

Please sign in to comment.