-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
value.go
154 lines (132 loc) · 3.15 KB
/
value.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
package store
import (
"bytes"
"encoding/json"
"errors"
res "github.com/jirenius/go-res"
)
// ValueType is an enum reprenting the value type
type ValueType byte
// Value type constants
const (
ValueTypeNone ValueType = iota
ValueTypePrimitive
ValueTypeReference
ValueTypeSoftReference
ValueTypeData
ValueTypeDelete
// Deprecated and replaced with ValueTypeReference.
ValueTypeResource = ValueTypeReference
)
// valueObject represents a resource reference or an action
type valueObject struct {
RID *string `json:"rid"`
Soft bool `json:"soft"`
Action *string `json:"action"`
Data json.RawMessage `json:"data"`
}
// DeleteValue is a predeclared delete action value
var DeleteValue = Value{
RawMessage: json.RawMessage(`{"action":"delete"}`),
Type: ValueTypeDelete,
}
// Value represents a RES value
// https://github.com/resgateio/resgate/blob/master/docs/res-protocol.md#values
type Value struct {
json.RawMessage
Type ValueType
RID string
Inner json.RawMessage
}
var errInvalidValue = errors.New("invalid value")
const (
actionDelete = "delete"
)
// UnmarshalJSON sets *v to the RES value represented by the JSON encoded data
func (v *Value) UnmarshalJSON(data []byte) error {
err := v.RawMessage.UnmarshalJSON(data)
if err != nil {
return err
}
// Get first non-whitespace character
var c byte
i := 0
for {
c = v.RawMessage[i]
if c != 0x20 && c != 0x09 && c != 0x0A && c != 0x0D {
break
}
i++
}
switch c {
case '{':
var mvo valueObject
err = json.Unmarshal(v.RawMessage, &mvo)
if err != nil {
return err
}
switch {
case mvo.RID != nil:
// Invalid to have both RID and Action or Data set, or if RID is empty
if mvo.Action != nil || mvo.Data != nil || *mvo.RID == "" {
return errInvalidValue
}
v.RID = *mvo.RID
if !res.Ref(v.RID).IsValid() {
return errInvalidValue
}
if mvo.Soft {
v.Type = ValueTypeSoftReference
} else {
v.Type = ValueTypeReference
}
case mvo.Action != nil:
// Invalid to have both Action and Data set, or if action is not actionDelete
if mvo.Data != nil || *mvo.Action != actionDelete {
return errInvalidValue
}
v.Type = ValueTypeDelete
case mvo.Data != nil:
v.Inner = mvo.Data
dc := mvo.Data[0]
// Is data containing a primitive?
if dc == '{' || dc == '[' {
v.Type = ValueTypeData
} else {
v.RawMessage = mvo.Data
v.Type = ValueTypePrimitive
}
default:
return errInvalidValue
}
case '[':
return errInvalidValue
default:
v.Type = ValueTypePrimitive
}
return nil
}
// MarshalJSON returns the embedded json.RawMessage as the JSON encoding.
func (v Value) MarshalJSON() ([]byte, error) {
if v.RawMessage == nil {
return []byte("null"), nil
}
return v.RawMessage, nil
}
// Equal reports whether v and w is equal in type and value
func (v Value) Equal(w Value) bool {
if v.Type != w.Type {
return false
}
switch v.Type {
case ValueTypeData:
return bytes.Equal(v.Inner, w.Inner)
case ValueTypePrimitive:
return bytes.Equal(v.RawMessage, w.RawMessage)
case ValueTypeReference:
fallthrough
case ValueTypeSoftReference:
return v.RID == w.RID
}
return true
}