Skip to content

Commit

Permalink
Added helper methods for creating commands and notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebires committed Jan 4, 2022
1 parent 8552875 commit f7338da
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
45 changes: 42 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ func (c *Command) populate(raw *rawEnvelope) error {
return nil
}

// IsRequest indicates if the current command is a request and should have a response.
func (c *Command) IsRequest() bool {
return c.ID != "" && c.Status == ""
}

// SuccessResponse creates a success response Command for the current request.
func (c *Command) SuccessResponse() *Command {
return &Command{
EnvelopeBase: EnvelopeBase{
ID: c.ID,
From: c.To,
To: c.Sender(),
},
Method: c.Method,
Status: CommandStatusSuccess,
}
}

// SuccessResponseWithResource creates a success response Command for the current request.
func (c *Command) SuccessResponseWithResource(resource Document) *Command {
respCmd := c.SuccessResponse()
respCmd.Resource = resource
return respCmd
}

// FailureResponse creates a failure response Command for the current request.
func (c *Command) FailureResponse(reason *Reason) *Command {
return &Command{
EnvelopeBase: EnvelopeBase{
ID: c.ID,
From: c.To,
To: c.Sender(),
},
Method: c.Method,
Status: CommandStatusFailure,
Reason: reason,
}
}

// CommandMethod Defines methods for the manipulation of resources.
type CommandMethod string

Expand All @@ -145,7 +184,7 @@ const (
CommandMethodMerge = CommandMethod("merge")
)

func (m CommandMethod) IsValid() error {
func (m CommandMethod) Validate() error {
switch m {
case CommandMethodGet, CommandMethodSet, CommandMethodDelete, CommandMethodSubscribe, CommandMethodUnsubscribe, CommandMethodObserve, CommandMethodMerge:
return nil
Expand All @@ -155,7 +194,7 @@ func (m CommandMethod) IsValid() error {
}

func (m CommandMethod) MarshalText() ([]byte, error) {
err := m.IsValid()
err := m.Validate()
if err != nil {
return []byte{}, err
}
Expand All @@ -164,7 +203,7 @@ func (m CommandMethod) MarshalText() ([]byte, error) {

func (m *CommandMethod) UnmarshalText(text []byte) error {
method := CommandMethod(text)
err := method.IsValid()
err := method.Validate()
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ type EnvelopeBase struct {
Metadata map[string]string
}

func (env *EnvelopeBase) GetMetadata() map[string]string {
return env.Metadata
// Sender returns the envelope sender Node.
func (env *EnvelopeBase) Sender() Node {
if env.PP == (Node{}) {
return env.PP
} else {
return env.From
}
}

func (env *EnvelopeBase) toRawEnvelope() (*rawEnvelope, error) {
Expand Down
11 changes: 6 additions & 5 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
)

// Identity Represents a member of a domain.
// Identity represents a member of a domain.
type Identity struct {
// Name Identity unique name on his domain.
// Name represents the Identity unique name on its domain.
Name string
// Domain Network domain name of the Identity.
// Domain represents the network domain name of the Identity.
Domain string
}

Expand All @@ -25,7 +25,7 @@ func (i Identity) String() string {
return fmt.Sprintf("%v@%v", i.Name, i.Domain)
}

// ParseIdentity Parses the string To a valid Identity.
// ParseIdentity parses the string To a valid Identity.
func ParseIdentity(s string) (Identity, error) {
var name, domain string
values := strings.Split(s, "@")
Expand All @@ -49,12 +49,13 @@ func (i *Identity) UnmarshalText(text []byte) error {
return nil
}

// ToNode Creates a Node instance based on the identity, with an
// ToNode creates a Node instance based on the identity, with an
// empty value for the instance property.
func (i Identity) ToNode() Node {
return Node{i, ""}
}

// IsComplete indicates if all Identity fields has values.
func (i *Identity) IsComplete() bool {
return i.Name != "" && i.Domain != ""
}
20 changes: 20 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,23 @@ func (m *Message) populate(raw *rawEnvelope) error {
m.Content = document
return nil
}

// Notification creates a notification for the current message.
func (m *Message) Notification(event NotificationEvent) *Notification {
return &Notification{
EnvelopeBase: EnvelopeBase{
ID: m.ID,
From: m.To,
To: m.Sender(),
},
Event: event,
}
}

// FailedNotification creates a notification for the current message with
// the 'failed' event.
func (m *Message) FailedNotification(reason *Reason) *Notification {
not := m.Notification(NotificationEventFailed)
not.Reason = reason
return not
}
5 changes: 3 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"strings"
)

// Node Represents an element of a network.
// Node represents an element of a network.
type Node struct {
Identity
// Instance The name of the instance used by the node to connect to the network.
// Instance represents the name of the instance used by the node to connect to the network.
Instance string
}

Expand Down Expand Up @@ -51,6 +51,7 @@ func (n *Node) UnmarshalText(text []byte) error {
return nil
}

// IsComplete indicates if all Node fields has values.
func (n *Node) IsComplete() bool {
return n.Identity.IsComplete() && n.Instance != ""
}
6 changes: 3 additions & 3 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const (
NotificationEventFailed = NotificationEvent("failed")
)

func (e *NotificationEvent) IsValid() error {
func (e *NotificationEvent) Validate() error {
switch *e {
case NotificationEventAccepted, NotificationEventDispatched, NotificationEventReceived, NotificationEventConsumed, NotificationEventFailed:
return nil
Expand All @@ -103,7 +103,7 @@ func (e *NotificationEvent) IsValid() error {
}

func (e NotificationEvent) MarshalText() ([]byte, error) {
err := e.IsValid()
err := e.Validate()
if err != nil {
return []byte{}, err
}
Expand All @@ -112,7 +112,7 @@ func (e NotificationEvent) MarshalText() ([]byte, error) {

func (e *NotificationEvent) UnmarshalText(text []byte) error {
event := NotificationEvent(text)
err := event.IsValid()
err := event.Validate()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const (
SessionStateFailed = SessionState("failed")
)

func (s SessionState) IsValid() error {
func (s SessionState) Validate() error {
switch s {
case SessionStateNew, SessionStateNegotiating, SessionStateAuthenticating, SessionStateEstablished, SessionStateFinishing, SessionStateFinished, SessionStateFailed:
return nil
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s SessionState) Step() int {
}

func (s SessionState) MarshalText() ([]byte, error) {
err := s.IsValid()
err := s.Validate()
if err != nil {
return []byte{}, err
}
Expand All @@ -234,7 +234,7 @@ func (s SessionState) MarshalText() ([]byte, error) {

func (s *SessionState) UnmarshalText(text []byte) error {
state := SessionState(text)
err := state.IsValid()
err := state.Validate()
if err != nil {
return err
}
Expand Down

0 comments on commit f7338da

Please sign in to comment.