From 66d4bb250f8b1a212c90be41c9cfb24d862668ec Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 15 Aug 2023 14:28:04 -0500 Subject: [PATCH] Rename flowRun to just run --- flows/engine/session.go | 14 +++--- flows/runs/legacy.go | 2 +- flows/runs/run.go | 95 ++++++++++++++++++++--------------------- 3 files changed, 55 insertions(+), 56 deletions(-) diff --git a/flows/engine/session.go b/flows/engine/session.go index 82ff832cc..e354f0b16 100644 --- a/flows/engine/session.go +++ b/flows/engine/session.go @@ -134,13 +134,13 @@ func (s *session) CurrentContext() *types.XObject { // looks through this session's run for the one that was last modified func (s *session) currentRun() flows.Run { - var lastRun flows.Run - for _, run := range s.runs { - if lastRun == nil || run.ModifiedOn().After(lastRun.ModifiedOn()) { - lastRun = run + var last flows.Run + for _, r := range s.runs { + if last == nil || r.ModifiedOn().After(last.ModifiedOn()) { + last = r } } - return lastRun + return last } // looks through this session's run for the one that is waiting @@ -220,11 +220,11 @@ func (s *session) prepareForSprint() error { // if we have a trigger with a parent run, load that triggerWithRun, hasRun := s.trigger.(flows.TriggerWithRun) if hasRun { - run, err := runs.ReadRunSummary(s.Assets(), triggerWithRun.RunSummary(), assets.IgnoreMissing) + r, err := runs.ReadRunSummary(s.Assets(), triggerWithRun.RunSummary(), assets.IgnoreMissing) if err != nil { return errors.Wrap(err, "error reading parent run from trigger") } - s.parentRun = run + s.parentRun = r } } return nil diff --git a/flows/runs/legacy.go b/flows/runs/legacy.go index 62e37466a..b92cbed37 100644 --- a/flows/runs/legacy.go +++ b/flows/runs/legacy.go @@ -98,7 +98,7 @@ func arrayToObject(array *types.XArray) *types.XObject { } // finds the last webhook response that was saved as extra on a result -func lastWebhookSavedAsExtra(r *flowRun) types.XValue { +func lastWebhookSavedAsExtra(r *run) types.XValue { for i := len(r.events) - 1; i >= 0; i-- { switch typed := r.events[i].(type) { case *events.WebhookCalledEvent: diff --git a/flows/runs/run.go b/flows/runs/run.go index 3d0bb9336..bda39aab9 100644 --- a/flows/runs/run.go +++ b/flows/runs/run.go @@ -15,11 +15,10 @@ import ( "github.com/nyaruka/goflow/flows" "github.com/nyaruka/goflow/flows/events" "github.com/nyaruka/goflow/utils" - "github.com/pkg/errors" ) -type flowRun struct { +type run struct { uuid flows.RunUUID session flows.Session @@ -43,7 +42,7 @@ type flowRun struct { // NewRun initializes a new context and flow run for the passed in flow and contact func NewRun(session flows.Session, flow flows.Flow, parent flows.Run) flows.Run { now := dates.Now() - r := &flowRun{ + r := &run{ uuid: flows.RunUUID(uuids.New()), session: session, flow: flow, @@ -62,16 +61,16 @@ func NewRun(session flows.Session, flow flows.Flow, parent flows.Run) flows.Run return r } -func (r *flowRun) UUID() flows.RunUUID { return r.uuid } -func (r *flowRun) Session() flows.Session { return r.session } +func (r *run) UUID() flows.RunUUID { return r.uuid } +func (r *run) Session() flows.Session { return r.session } -func (r *flowRun) Flow() flows.Flow { return r.flow } -func (r *flowRun) FlowReference() *assets.FlowReference { return r.flowRef } -func (r *flowRun) Contact() *flows.Contact { return r.session.Contact() } -func (r *flowRun) Events() []flows.Event { return r.events } +func (r *run) Flow() flows.Flow { return r.flow } +func (r *run) FlowReference() *assets.FlowReference { return r.flowRef } +func (r *run) Contact() *flows.Contact { return r.session.Contact() } +func (r *run) Events() []flows.Event { return r.events } -func (r *flowRun) Results() flows.Results { return r.results } -func (r *flowRun) SaveResult(result *flows.Result) { +func (r *run) Results() flows.Results { return r.results } +func (r *run) SaveResult(result *flows.Result) { // truncate value if necessary result.Value = stringsx.Truncate(result.Value, r.session.MergedEnvironment().MaxValueLength()) @@ -81,48 +80,48 @@ func (r *flowRun) SaveResult(result *flows.Result) { r.legacyExtra.addResult(result) } -func (r *flowRun) Exit(status flows.RunStatus) { +func (r *run) Exit(status flows.RunStatus) { now := dates.Now() r.status = status r.exitedOn = &now r.modifiedOn = now } -func (r *flowRun) Status() flows.RunStatus { return r.status } -func (r *flowRun) SetStatus(status flows.RunStatus) { +func (r *run) Status() flows.RunStatus { return r.status } +func (r *run) SetStatus(status flows.RunStatus) { r.status = status r.modifiedOn = dates.Now() } -func (r *flowRun) Webhook() types.XValue { +func (r *run) Webhook() types.XValue { return r.webhook } -func (r *flowRun) SetWebhook(value types.XValue) { +func (r *run) SetWebhook(value types.XValue) { r.webhook = value } // ParentInSession returns the parent of the run within the same session if one exists -func (r *flowRun) ParentInSession() flows.Run { return r.parent } +func (r *run) ParentInSession() flows.Run { return r.parent } // Parent returns either the same session parent or if this session was triggered from a trigger_flow action // in another session, that run -func (r *flowRun) Parent() flows.RunSummary { +func (r *run) Parent() flows.RunSummary { if r.parent == nil { return r.session.ParentRun() } return r.ParentInSession() } -func (r *flowRun) Ancestors() []flows.Run { +func (r *run) Ancestors() []flows.Run { ancestors := make([]flows.Run, 0) if r.parent != nil { - run := r.parent.(*flowRun) - ancestors = append(ancestors, run) + pr := r.parent.(*run) + ancestors = append(ancestors, pr) for { - if run.parent != nil { - run = run.parent.(*flowRun) - ancestors = append(ancestors, run) + if pr.parent != nil { + pr = pr.parent.(*run) + ancestors = append(ancestors, pr) } else { break } @@ -132,7 +131,7 @@ func (r *flowRun) Ancestors() []flows.Run { return ancestors } -func (r *flowRun) LogEvent(s flows.Step, event flows.Event) { +func (r *run) LogEvent(s flows.Step, event flows.Event) { if s != nil { event.SetStepUUID(s.UUID()) } @@ -141,12 +140,12 @@ func (r *flowRun) LogEvent(s flows.Step, event flows.Event) { r.modifiedOn = dates.Now() } -func (r *flowRun) LogError(step flows.Step, err error) { +func (r *run) LogError(step flows.Step, err error) { r.LogEvent(step, events.NewError(err)) } // find the first event matching the given step UUID and type -func (r *flowRun) findEvent(stepUUID flows.StepUUID, eType string) flows.Event { +func (r *run) findEvent(stepUUID flows.StepUUID, eType string) flows.Event { for _, e := range r.events { if (stepUUID == "" || e.StepUUID() == stepUUID) && e.Type() == eType { return e @@ -155,12 +154,12 @@ func (r *flowRun) findEvent(stepUUID flows.StepUUID, eType string) flows.Event { return nil } -func (r *flowRun) ReceivedInput() bool { +func (r *run) ReceivedInput() bool { return r.findEvent("", events.TypeMsgReceived) != nil } -func (r *flowRun) Path() []flows.Step { return r.path } -func (r *flowRun) CreateStep(node flows.Node) flows.Step { +func (r *run) Path() []flows.Step { return r.path } +func (r *run) CreateStep(node flows.Node) flows.Step { now := dates.Now() step := NewStep(node, now) r.path = append(r.path, step) @@ -168,7 +167,7 @@ func (r *flowRun) CreateStep(node flows.Node) flows.Step { return step } -func (r *flowRun) PathLocation() (flows.Step, flows.Node, error) { +func (r *run) PathLocation() (flows.Step, flows.Node, error) { if r.Path() == nil { return nil, nil, errors.Errorf("run has no location as path is empty") } @@ -187,9 +186,9 @@ func (r *flowRun) PathLocation() (flows.Step, flows.Node, error) { return step, node, nil } -func (r *flowRun) CreatedOn() time.Time { return r.createdOn } -func (r *flowRun) ModifiedOn() time.Time { return r.modifiedOn } -func (r *flowRun) ExitedOn() *time.Time { return r.exitedOn } +func (r *run) CreatedOn() time.Time { return r.createdOn } +func (r *run) ModifiedOn() time.Time { return r.modifiedOn } +func (r *run) ExitedOn() *time.Time { return r.exitedOn } // RootContext returns the root context for expression evaluation // @@ -209,7 +208,7 @@ func (r *flowRun) ExitedOn() *time.Time { return r.exitedOn } // resume:resume -> the current resume that continued this session // // @context root -func (r *flowRun) RootContext(env envs.Environment) map[string]types.XValue { +func (r *run) RootContext(env envs.Environment) map[string]types.XValue { var urns, fields, ticket, node types.XValue if r.Contact() != nil { urns = flows.ContextFunc(env, r.Contact().URNs().MapContext) @@ -264,7 +263,7 @@ func (r *flowRun) RootContext(env envs.Environment) map[string]types.XValue { // exited_on:datetime -> the exit date of the run // // @context run -func (r *flowRun) Context(env envs.Environment) map[string]types.XValue { +func (r *run) Context(env envs.Environment) map[string]types.XValue { var exitedOn types.XValue if r.exitedOn != nil { exitedOn = types.NewXDateTime(*r.exitedOn) @@ -289,7 +288,7 @@ func (r *flowRun) Context(env envs.Environment) map[string]types.XValue { // visit_count:number -> the count of visits to the node in this run // // @context node -func (r *flowRun) nodeContext(env envs.Environment) map[string]types.XValue { +func (r *run) nodeContext(env envs.Environment) map[string]types.XValue { _, node, _ := r.PathLocation() visitCount := 0 for _, s := range r.path { @@ -305,14 +304,14 @@ func (r *flowRun) nodeContext(env envs.Environment) map[string]types.XValue { } // EvaluateTemplate evaluates the given template in the context of this run -func (r *flowRun) EvaluateTemplateValue(template string) (types.XValue, error) { +func (r *run) EvaluateTemplateValue(template string) (types.XValue, error) { ctx := types.NewXObject(r.RootContext(r.session.MergedEnvironment())) return excellent.EvaluateTemplateValue(r.session.MergedEnvironment(), ctx, template) } // EvaluateTemplateText evaluates the given template as text in the context of this run -func (r *flowRun) EvaluateTemplateText(template string, escaping excellent.Escaping, truncate bool) (string, error) { +func (r *run) EvaluateTemplateText(template string, escaping excellent.Escaping, truncate bool) (string, error) { ctx := types.NewXObject(r.RootContext(r.session.MergedEnvironment())) value, err := excellent.EvaluateTemplate(r.session.MergedEnvironment(), ctx, template, escaping) @@ -323,12 +322,12 @@ func (r *flowRun) EvaluateTemplateText(template string, escaping excellent.Escap } // EvaluateTemplate is a convenience function for evaluating as text with no escaping -func (r *flowRun) EvaluateTemplate(template string) (string, error) { +func (r *run) EvaluateTemplate(template string) (string, error) { return r.EvaluateTemplateText(template, nil, true) } // get the ordered list of languages to be used for localization in this run -func (r *flowRun) getLanguages() []envs.Language { +func (r *run) getLanguages() []envs.Language { languages := make([]envs.Language, 0, 3) // if contact has an allowed language, it takes priority @@ -349,17 +348,17 @@ func (r *flowRun) getLanguages() []envs.Language { } // GetText is a convenience version of GetTextArray for a single text values -func (r *flowRun) GetText(uuid uuids.UUID, key string, native string) (string, envs.Language) { +func (r *run) GetText(uuid uuids.UUID, key string, native string) (string, envs.Language) { textArray, lang := r.getText(uuid, key, []string{native}, nil) return textArray[0], lang } // GetTextArray returns the localized value for the given flow definition value -func (r *flowRun) GetTextArray(uuid uuids.UUID, key string, native []string, languages []envs.Language) ([]string, envs.Language) { +func (r *run) GetTextArray(uuid uuids.UUID, key string, native []string, languages []envs.Language) ([]string, envs.Language) { return r.getText(uuid, key, native, languages) } -func (r *flowRun) getText(uuid uuids.UUID, key string, native []string, languages []envs.Language) ([]string, envs.Language) { +func (r *run) getText(uuid uuids.UUID, key string, native []string, languages []envs.Language) ([]string, envs.Language) { nativeLang := r.Flow().Language() // if a preferred language list wasn't provided, default to the run preferred languages @@ -383,11 +382,11 @@ func (r *flowRun) getText(uuid uuids.UUID, key string, native []string, language return native, nativeLang } -func (r *flowRun) Snapshot() flows.RunSummary { +func (r *run) Snapshot() flows.RunSummary { return newRunSummaryFromRun(r) } -var _ flows.RunSummary = (*flowRun)(nil) +var _ flows.RunSummary = (*run)(nil) //------------------------------------------------------------------------------------------ // JSON Encoding / Decoding @@ -417,7 +416,7 @@ func ReadRun(session flows.Session, data json.RawMessage, missing assets.Missing return nil, errors.Wrap(err, "unable to read run") } - r := &flowRun{ + r := &run{ session: session, uuid: e.UUID, flowRef: e.Flow, @@ -467,7 +466,7 @@ func ReadRun(session flows.Session, data json.RawMessage, missing assets.Missing } // MarshalJSON marshals this flow run into JSON -func (r *flowRun) MarshalJSON() ([]byte, error) { +func (r *run) MarshalJSON() ([]byte, error) { var err error e := &runEnvelope{