-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
outbox_message.go
53 lines (43 loc) · 1.12 KB
/
outbox_message.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
package outboxer
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
)
// ErrFailedToDecodeType is returned when the type of the value is not supported.
var ErrFailedToDecodeType = errors.New("failed to decode type")
// OutboxMessage represents a message that will be sent.
type OutboxMessage struct {
Options DynamicValues
Headers DynamicValues
DispatchedAt sql.NullTime
Payload []byte
ID int64
Dispatched bool
}
// DynamicValues is a map that can be serialized.
type DynamicValues map[string]interface{}
// Value return a driver.Value representation of the order items.
func (p DynamicValues) Value() (driver.Value, error) {
if len(p) == 0 {
return nil, nil
}
return json.Marshal(p)
}
// Scan scans a database json representation into a []Item.
func (p *DynamicValues) Scan(src interface{}) error {
if src == nil {
return nil
}
v := reflect.ValueOf(src)
if !v.IsValid() {
return nil
}
if data, ok := src.([]byte); ok {
return json.Unmarshal(data, &p)
}
return fmt.Errorf("could not not decode type %T -> %T: %w", src, p, ErrFailedToDecodeType)
}