phoneof/adapters/messaging/messaging.go

62 lines
1.4 KiB
Go
Raw Normal View History

2025-01-03 04:47:07 -05:00
package messaging
2025-01-05 18:16:26 -05:00
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)
}
2025-01-03 04:47:07 -05:00
}
2025-01-05 18:16:26 -05:00
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