From 8d6cf92002fe5432caee3db111a69de3c8b5fcef Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 13 Mar 2018 16:20:02 -0500 Subject: [PATCH] More docstrings... --- cmd/flowserver/server.go | 2 +- flows/channel.go | 7 +++++++ flows/fields.go | 2 +- flows/inputs/envelope.go | 1 + flows/msg.go | 18 ++++++++++++++---- flows/runs/step.go | 2 +- flows/runsummary.go | 2 ++ flows/urns.go | 4 ++++ utils/http.go | 2 +- utils/locations.go | 2 +- 10 files changed, 33 insertions(+), 9 deletions(-) diff --git a/cmd/flowserver/server.go b/cmd/flowserver/server.go index 6a652af42..07ac8bf8f 100644 --- a/cmd/flowserver/server.go +++ b/cmd/flowserver/server.go @@ -114,7 +114,7 @@ type sessionResponse struct { Log []flows.LogEntry `json:"log"` } -// MarshalJSON marshals this session reponse into JSON +// MarshalJSON marshals this session response into JSON func (r *sessionResponse) MarshalJSON() ([]byte, error) { envelope := sessionResponse{ Session: r.Session, diff --git a/flows/channel.go b/flows/channel.go index f478e88a6..829ec89b6 100644 --- a/flows/channel.go +++ b/flows/channel.go @@ -7,8 +7,10 @@ import ( "github.com/nyaruka/goflow/utils" ) +// ChannelRole is a role that a channel can perform type ChannelRole string +// different roles that channels can perform const ( ChannelRoleSend ChannelRole = "send" ChannelRoleReceive ChannelRole = "receive" @@ -25,6 +27,7 @@ type channel struct { roles []ChannelRole } +// NewChannel creates a new channel func NewChannel(uuid ChannelUUID, name string, address string, schemes []string, roles []ChannelRole) Channel { return &channel{ uuid: uuid, @@ -53,6 +56,7 @@ func (c *channel) Roles() []ChannelRole { return c.roles } // Reference returns a reference to this channel func (c *channel) Reference() *ChannelReference { return NewChannelReference(c.uuid, c.name) } +// SupportsScheme returns whether this channel supports the given URN scheme func (c *channel) SupportsScheme(scheme string) bool { for _, s := range c.schemes { if s == scheme { @@ -62,6 +66,7 @@ func (c *channel) SupportsScheme(scheme string) bool { return false } +// HasRole returns whether this channel has the given role func (c *channel) HasRole(role ChannelRole) bool { for _, r := range c.roles { if r == role { @@ -103,6 +108,7 @@ type ChannelSet struct { channelsByUUID map[ChannelUUID]Channel } +// NewChannelSet creates a new channel set func NewChannelSet(channels []Channel) *ChannelSet { s := &ChannelSet{channels: channels, channelsByUUID: make(map[ChannelUUID]Channel, len(channels))} for _, channel := range s.channels { @@ -129,6 +135,7 @@ func (s *ChannelSet) GetForURN(urn *ContactURN) Channel { return nil } +// FindByUUID finds the channel with the given UUID func (s *ChannelSet) FindByUUID(uuid ChannelUUID) Channel { return s.channelsByUUID[uuid] } diff --git a/flows/fields.go b/flows/fields.go index f60db9757..57261dfee 100644 --- a/flows/fields.go +++ b/flows/fields.go @@ -63,7 +63,7 @@ func (f *Field) ParseValue(env utils.Environment, value string) (interface{}, er return nil, err } if locations == nil { - return nil, fmt.Errorf("can't parse field '%s' (type %s) in enviroment which is not location enabled", f.key, f.valueType) + return nil, fmt.Errorf("can't parse field '%s' (type %s) in environment which is not location enabled", f.key, f.valueType) } return locations.FindByID(locationID, locationLevel), nil } diff --git a/flows/inputs/envelope.go b/flows/inputs/envelope.go index 553449daf..fbf8ab86f 100644 --- a/flows/inputs/envelope.go +++ b/flows/inputs/envelope.go @@ -14,6 +14,7 @@ type baseInputEnvelope struct { CreatedOn time.Time `json:"created_on" validate:"required"` } +// ReadInput reads an input from the given typed envelope func ReadInput(session flows.Session, envelope *utils.TypedEnvelope) (flows.Input, error) { switch envelope.Type { diff --git a/flows/msg.go b/flows/msg.go index bbd59565c..3d51cbcc5 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -62,10 +62,20 @@ func NewMsgOut(urn urns.URN, channel Channel, text string, attachments []Attachm } } -func (m *BaseMsg) UUID() MsgUUID { return m.UUID_ } -func (m *BaseMsg) URN() urns.URN { return m.URN_ } +// UUID returns the UUID of this message +func (m *BaseMsg) UUID() MsgUUID { return m.UUID_ } + +// URN returns the URN of this message +func (m *BaseMsg) URN() urns.URN { return m.URN_ } + +// Channel returns the channel of this message func (m *BaseMsg) Channel() *ChannelReference { return m.Channel_ } -func (m *BaseMsg) Text() string { return m.Text_ } -func (m *BaseMsg) Attachments() []Attachment { return m.Attachments_ } +// Text returns the text of this message +func (m *BaseMsg) Text() string { return m.Text_ } + +// Attachments returns the attachments of this message +func (m *BaseMsg) Attachments() []Attachment { return m.Attachments_ } + +// QuickReplies returns the quick replies of this outgoing message func (m *MsgOut) QuickReplies() []string { return m.QuickReplies_ } diff --git a/flows/runs/step.go b/flows/runs/step.go index b8dffa780..552273419 100644 --- a/flows/runs/step.go +++ b/flows/runs/step.go @@ -96,7 +96,7 @@ func (s *step) MarshalJSON() ([]byte, error) { if err != nil { return nil, err } - se.Events[i] = &utils.TypedEnvelope{event.Type(), eventData} + se.Events[i] = &utils.TypedEnvelope{Type: event.Type(), Data: eventData} } return json.Marshal(se) diff --git a/flows/runsummary.go b/flows/runsummary.go index 421b6aa33..d544ded08 100644 --- a/flows/runsummary.go +++ b/flows/runsummary.go @@ -20,6 +20,7 @@ func (r *runSummary) Contact() *Contact { return r.contact } func (r *runSummary) Status() RunStatus { return r.status } func (r *runSummary) Results() Results { return r.results } +// NewRunSummaryFromRun creates a new run summary from the given run func NewRunSummaryFromRun(run FlowRun) RunSummary { return &runSummary{ uuid: run.UUID(), @@ -44,6 +45,7 @@ type runSummaryEnvelope struct { Results Results `json:"results"` } +// ReadRunSummary reads a run summary from the given JSON func ReadRunSummary(session Session, data json.RawMessage) (RunSummary, error) { var err error e := runSummaryEnvelope{} diff --git a/flows/urns.go b/flows/urns.go index 54890c2bd..b56838d5c 100644 --- a/flows/urns.go +++ b/flows/urns.go @@ -68,6 +68,7 @@ func (u *ContactURN) String() string { return string(u.URN) } // URNList is the list of a contact's URNs type URNList []*ContactURN +// ReadURNList parses contact URN list from the given list of raw URNs func ReadURNList(session Session, rawURNs []urns.URN) (URNList, error) { l := make(URNList, len(rawURNs)) @@ -98,6 +99,7 @@ func ReadURNList(session Session, rawURNs []urns.URN) (URNList, error) { return l, nil } +// RawURNs returns the raw URNs with or without channel information func (l URNList) RawURNs(includeChannels bool) []urns.URN { raw := make([]urns.URN, len(l)) for u := range l { @@ -112,12 +114,14 @@ func (l URNList) RawURNs(includeChannels bool) []urns.URN { return raw } +// Clone returns a clone of this URN list func (l URNList) Clone() URNList { urns := make(URNList, len(l)) copy(urns, l) return urns } +// WithScheme returns a new URN list containing of only URNs of the given scheme func (l URNList) WithScheme(scheme string) URNList { var matching URNList for _, u := range l { diff --git a/utils/http.go b/utils/http.go index 40a9b6f2e..39e1d64f5 100644 --- a/utils/http.go +++ b/utils/http.go @@ -246,7 +246,7 @@ func (r *RequestResponse) UnmarshalJSON(data []byte) error { return nil } -// MarshalJSON marshals this request reponse into JSON +// MarshalJSON marshals this request response into JSON func (r *RequestResponse) MarshalJSON() ([]byte, error) { var re rrEnvelope diff --git a/utils/locations.go b/utils/locations.go index 40c176822..1d49d7cb1 100644 --- a/utils/locations.go +++ b/utils/locations.go @@ -147,7 +147,7 @@ func FindLocations(env Environment, name string, level LocationLevel, parent *Lo return nil, err } if locations == nil { - return nil, fmt.Errorf("can't find locations in enviroment which is not location enabled") + return nil, fmt.Errorf("can't find locations in environment which is not location enabled") } return locations.FindByName(name, level, parent), nil