Elizabeth Hunt
2984a715b8
All checks were successful
continuous-integration/drone/push Build is passing
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package messaging
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Message struct {
|
|
FrenName string
|
|
Message string
|
|
}
|
|
|
|
func (m *Message) Encode() string {
|
|
return m.FrenName + " " + m.Message
|
|
}
|
|
|
|
func Decode(message string) (*Message, error) {
|
|
content := strings.SplitN(message, " ", 2)
|
|
if len(content) < 2 {
|
|
return nil, fmt.Errorf("no space delimiter")
|
|
}
|
|
return &Message{
|
|
FrenName: content[0],
|
|
Message: content[1],
|
|
}, nil
|
|
}
|
|
|
|
func IdContinuation(message Message) ContinuationChain {
|
|
return func(success Continuation, _failure Continuation) ContinuationChain {
|
|
return success(message)
|
|
}
|
|
}
|
|
|
|
func FailurePassingContinuation(message Message) ContinuationChain {
|
|
return func(_success Continuation, failure Continuation) ContinuationChain {
|
|
return failure(message)
|
|
}
|
|
}
|
|
|
|
func LogContinuation(message Message) ContinuationChain {
|
|
return func(success Continuation, _failure Continuation) ContinuationChain {
|
|
now := time.Now().UTC()
|
|
|
|
log.Println(now, message)
|
|
return success(message)
|
|
}
|
|
}
|
|
|
|
// basically b(a(message)) if and only if b is successful
|
|
func Compose(a Continuation, b Continuation) Continuation {
|
|
return func(message Message) ContinuationChain {
|
|
return func(success Continuation, failure Continuation) ContinuationChain {
|
|
return b(message)(a, FailurePassingContinuation)(success, failure)
|
|
}
|
|
}
|
|
}
|
|
|
|
type Continuation func(Message) ContinuationChain
|
|
type ContinuationChain func(Continuation, Continuation) ContinuationChain
|