-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
172 lines (148 loc) · 3.99 KB
/
resource.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package rest
import (
"encoding/json"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"gorm.io/datatypes"
"time"
)
type EventSourced interface {
NewChange(event *Event)
GetNewChanges() []Resource
Persist()
}
type Resource interface {
EventSourced
GetType() string
GetSequenceNo() int64
GetID() string
FromBytes(schema *openapi3.T, data []byte) (Resource, error)
IsValid() bool
GetErrors() []error
}
type BasicResource struct {
Body datatypes.JSONMap `gorm:"default:'[]'; null'"`
Metadata ResourceMetadata `gorm:"embedded"`
newEvents []Resource `gorm:"-"`
}
type ResourceMetadata struct {
ID string `gorm:"primaryKey"`
SequenceNo int64
Type string
Version int
UserID string
AccountID string
}
// MarshalJSON customizes the JSON encoding of BasicResource
func (r *BasicResource) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Body)
}
// UnmarshalJSON customizes the JSON decoding of BasicResource
func (r *BasicResource) UnmarshalJSON(data []byte) (err error) {
// You might want to initialize your map if it's nil
if r.Body == nil {
r.Body = make(map[string]interface{})
r.Body["@context"] = map[string]interface{}{}
}
//created a temporary map to hold the data because the JSONMAP creates an empty map in it's own UnmarshalJSON
var tbody map[string]interface{}
tbody = map[string]interface{}(r.Body)
// Unmarshal data into the map
err = json.Unmarshal(data, &tbody)
if ttype, ok := tbody["@type"].(string); ok {
r.Metadata.Type = ttype
}
if id, ok := tbody["@id"].(string); ok {
r.Metadata.ID = id
}
r.Body = tbody
return err
}
// FromBytes creates a new BasicResource from a schema and data
func (r *BasicResource) FromBytes(schema *openapi3.T, data []byte) (Resource, error) {
err := r.UnmarshalJSON(data)
//TODO use the schema to validate the data
//TODO fill in any missing blanks
if r.GetType() == "" {
return nil, fmt.Errorf("missing type")
}
eventType := "create"
if r.GetSequenceNo() > 0 {
eventType = "update"
}
r.NewChange(NewResourceEvent(eventType, r, r.Body))
return r, err
}
func (r *BasicResource) IsValid() bool {
//TODO this should use the matched schema from the OpenAPI spec to validate the resource
panic("implement me")
}
func (r *BasicResource) AddError(err error) {
//TODO implement me
panic("implement me")
}
func (r *BasicResource) GetErrors() []error {
//TODO implement me
panic("implement me")
}
func (r *BasicResource) GetID() string {
return r.Metadata.ID
}
func (r *BasicResource) GetType() string {
return r.Metadata.Type
}
func (r *BasicResource) GetSequenceNo() int64 {
return r.Metadata.SequenceNo
}
// NewChange adds a new event to the list of new events
func (r *BasicResource) NewChange(event *Event) {
r.Metadata.SequenceNo += 1
event.Meta.SequenceNo = r.Metadata.SequenceNo
r.newEvents = append(r.newEvents, event)
}
// GetNewChanges returns the list of new events
func (r *BasicResource) GetNewChanges() []Resource {
return r.newEvents
}
// Persist clears the new events array
func (r *BasicResource) Persist() {
r.newEvents = nil
}
func (r *BasicResource) GetString(propertyName string) string {
if value, ok := r.Body[propertyName].(string); ok {
return value
}
return ""
}
func (r *BasicResource) GetBool(propertyName string) bool {
if value, ok := r.Body[propertyName].(bool); ok {
return value
}
return false
}
func (r *BasicResource) GetInt(propertyName string) int {
if value, ok := r.Body[propertyName].(int); ok {
return value
}
return 0
}
func (r *BasicResource) GetFloat(propertyName string) float64 {
if value, ok := r.Body[propertyName].(float64); ok {
return value
}
return 0.0
}
func NewResourceEvent(eventType string, resource Resource, tpayload map[string]interface{}) *Event {
var payload json.RawMessage
payload, _ = json.Marshal(tpayload)
return &Event{
Type: eventType,
Payload: payload,
Version: 2,
Meta: EventMeta{
ResourceID: resource.GetID(),
ResourceType: resource.GetType(),
Created: time.Now().Format(time.RFC3339Nano),
},
}
}