Skip to content

Commit

Permalink
Merge pull request #197 from nyaruka/events
Browse files Browse the repository at this point in the history
Restructure event output so that events aren't wrapped in log entries
  • Loading branch information
rowanseymour authored Mar 21, 2018
2 parents e24e85e + 1686f74 commit c0d6b01
Show file tree
Hide file tree
Showing 30 changed files with 2,738 additions and 3,078 deletions.
6 changes: 3 additions & 3 deletions cmd/docgen/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func eventsForAction(actionJSON []byte) (json.RawMessage, error) {
return nil, err
}

eventLog := session.Log()
eventLog := session.Events()
eventJSON := make([]json.RawMessage, len(eventLog))
for i, logEntry := range eventLog {
typed, err := utils.EnvelopeFromTyped(logEntry.Event())
for i, event := range eventLog {
typed, err := utils.EnvelopeFromTyped(event)
if err != nil {
return nil, err
}
Expand Down
29 changes: 8 additions & 21 deletions cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (

type Output struct {
Session json.RawMessage `json:"session"`
Log []json.RawMessage `json:"log"`
Events []json.RawMessage `json:"events"`
}

type FlowTest struct {
Expand All @@ -34,20 +34,7 @@ type FlowTest struct {
Outputs []json.RawMessage `json:"outputs"`
}

func envelopesForEvents(events []flows.Event) []*utils.TypedEnvelope {
envelopes := make([]*utils.TypedEnvelope, len(events))
for i := range events {
envelope, err := utils.EnvelopeFromTyped(events[i])
if err != nil {
log.Fatalf("Error creating envelope for %s: %s", events[i], err)
}

envelopes[i] = envelope
}
return envelopes
}

func marshalEventLog(eventLog []flows.LogEntry) []json.RawMessage {
func marshalEventLog(eventLog []flows.Event) []json.RawMessage {
envelopes := make([]json.RawMessage, len(eventLog))
for i := range eventLog {
envelope, err := json.Marshal(eventLog[i])
Expand Down Expand Up @@ -217,12 +204,12 @@ func main() {
log.Fatal("Error marshalling output: ", err)
}
fmt.Printf("%s\n", outJSON)
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Log())})
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Events())})

// print any msg_created events
for _, e := range session.Log() {
if e.Event().Type() == events.TypeMsgCreated {
fmt.Printf(">>> %s\n", e.Event().(*events.MsgCreatedEvent).Msg.Text())
for _, event := range session.Events() {
if event.Type() == events.TypeMsgCreated {
fmt.Printf(">>> %s\n", event.(*events.MsgCreatedEvent).Msg.Text())
}
}

Expand Down Expand Up @@ -255,7 +242,7 @@ func main() {
log.Fatal("Error marshalling output: ", err)
}
fmt.Printf("%s\n", outJSON)
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Log())})
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Events())})

// write out our test file
if *writePtr {
Expand All @@ -264,7 +251,7 @@ func main() {

callerEventEnvelopes := make([][]*utils.TypedEnvelope, len(callerEvents))
for i := range callerEvents {
callerEventEnvelopes[i] = envelopesForEvents(callerEvents[i])
callerEventEnvelopes[i], _ = events.EventsToEnvelopes(callerEvents[i])
}

rawOutputs := make([]json.RawMessage, len(outputs))
Expand Down
14 changes: 7 additions & 7 deletions cmd/flowrunner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func runFlow(assetsFilename string, triggerEnvelope *utils.TypedEnvelope, caller
if err != nil {
return runResult{}, fmt.Errorf("Error marshalling output: %s", err)
}
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Log())})
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Events())})

session, err = engine.ReadSession(assetCache, engine.NewMockAssetServer(), outJSON)
if err != nil {
Expand All @@ -138,7 +138,7 @@ func runFlow(assetsFilename string, triggerEnvelope *utils.TypedEnvelope, caller
if err != nil {
return runResult{}, fmt.Errorf("Error marshalling output: %s", err)
}
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Log())})
outputs = append(outputs, &Output{outJSON, marshalEventLog(session.Events())})

return runResult{assetCache, session, outputs}, nil
}
Expand Down Expand Up @@ -272,13 +272,13 @@ func TestFlows(t *testing.T) {
}
}

if len(actualOutput.Log) != len(expectedOutput.Log) {
t.Errorf("Actual events:\n%#v\n do not match expected:\n%#v\n for flow '%s'\n", actualOutput.Log, expectedOutput.Log, test.assets)
if len(actualOutput.Events) != len(expectedOutput.Events) {
t.Errorf("Actual events:\n%#v\n do not match expected:\n%#v\n for flow '%s'\n", actualOutput.Events, expectedOutput.Events, test.assets)
}

for j := range actualOutput.Log {
event := actualOutput.Log[j]
expected := expectedOutput.Log[j]
for j := range actualOutput.Events {
event := actualOutput.Events[j]
expected := expectedOutput.Events[j]

// write our events as json
eventJSON, err := rawMessageAsJSON(event)
Expand Down
Loading

0 comments on commit c0d6b01

Please sign in to comment.