-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
122 lines (105 loc) · 3.19 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gomdb
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
var (
// ErrInvalidMessageID is returned when the proposed message ID is not a
// valid UUID.
ErrInvalidMessageID = errors.New("proposed message ID must be a valid UUID")
// ErrMissingType is returned when the proposed message is missing the
// message type.
ErrMissingType = errors.New("proposed message must include Type")
// ErrMissingData is returned when the proposed message is missing any
// data.
ErrMissingData = errors.New("proposed message must include Data")
// ErrMissingCategory is returned when the stream identifier category is
// missing.
ErrMissingCategory = errors.New("category cannot be blank")
// ErrInvalidCategory is returned when the stream identifier category
// contains the reserved stream name seperator character.
ErrInvalidCategory = fmt.Errorf("category cannot contain separator (%s)", StreamNameSeparator)
// ErrMissingStreamID is returned when the stream identifier ID is missing.
ErrMissingStreamID = errors.New("ID cannot be blank")
)
// StreamNameSeparator is the character used to separate the stream category
// from the stream ID in a stream name.
const StreamNameSeparator = "-"
// Message represents a message that was stored in message-db.
type Message struct {
ID string
Stream StreamIdentifier
Type string
Version int64
GlobalPosition int64
Timestamp time.Time
data []byte
metadata []byte
}
type scanner interface {
Scan(...interface{}) error
}
func deserialiseMessage(row scanner) (*Message, error) {
var (
msg = &Message{}
streamName string
)
err := row.Scan(&msg.ID, &streamName, &msg.Type, &msg.Version, &msg.GlobalPosition, &msg.data, &msg.metadata, &msg.Timestamp)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return msg, nil
}
// UnmarshalData attempts to unmarshall the Message's data into the provided
// object.
func (m *Message) UnmarshalData(i interface{}) error {
return json.Unmarshal(m.data, i)
}
// UnmarshalMetadata attempts to unmarshall the Message's metadata into the
// provided object.
func (m *Message) UnmarshalMetadata(i interface{}) error {
return json.Unmarshal(m.metadata, i)
}
// ProposedMessage proposes a messages to be written to message-db.
type ProposedMessage struct {
ID string
Type string
Data interface{}
Metadata interface{}
}
func (pm *ProposedMessage) validate() error {
if pm.ID == "" {
return ErrInvalidMessageID
} else if pm.Type == "" {
return ErrMissingType
} else if pm.Data == nil {
return ErrMissingData
}
return nil
}
// StreamIdentifier captures the two components of a message-db stream name.
type StreamIdentifier struct {
Category string
ID string
}
// String returns the string respresentation of a StreamIdentifier.
func (si StreamIdentifier) String() string {
return si.Category + StreamNameSeparator + si.ID
}
func (si StreamIdentifier) validate() error {
if si.Category == "" {
return ErrMissingCategory
} else if strings.Contains(si.Category, StreamNameSeparator) {
return ErrInvalidCategory
} else if si.ID == "" {
return ErrMissingStreamID
}
return nil
}