-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
legacy.go
88 lines (75 loc) · 2.12 KB
/
legacy.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
package rescache
import (
"bytes"
"encoding/json"
"github.com/resgateio/resgate/server/codec"
)
// Legacy120Model marshals a model compatible with version 1.2.0
// (versionSoftResourceReference) and below.
type Legacy120Model Model
// Legacy120Collection marshals a collection compatible with version 1.2.0
// (versionSoftResourceReference) and below.
type Legacy120Collection Collection
// Legacy120Value marshals a value compatible with version 1.2.0
// (versionSoftResourceReference) and below.
type Legacy120Value codec.Value
// Legacy120ValueMap marshals a map of values compatible with version 1.2.0
// (versionSoftResourceReference) and below.
type Legacy120ValueMap map[string]codec.Value
// MarshalJSON creates a JSON encoded representation of the model
func (m *Legacy120Model) MarshalJSON() ([]byte, error) {
for _, v := range m.Values {
if v.Type == codec.ValueTypeSoftReference {
return Legacy120ValueMap(m.Values).MarshalJSON()
}
}
return (*Model)(m).MarshalJSON()
}
// MarshalJSON creates a JSON encoded representation of the model
func (c *Legacy120Collection) MarshalJSON() ([]byte, error) {
for _, v := range c.Values {
if v.Type == codec.ValueTypeSoftReference {
goto LegacyMarshal
}
}
return (*Collection)(c).MarshalJSON()
LegacyMarshal:
vs := c.Values
lvs := make([]Legacy120Value, len(vs))
for i, v := range vs {
lvs[i] = Legacy120Value(v)
}
return json.Marshal(lvs)
}
// MarshalJSON creates a JSON encoded representation of the value
func (v Legacy120Value) MarshalJSON() ([]byte, error) {
if v.Type == codec.ValueTypeSoftReference {
return json.Marshal(v.RID)
}
return v.RawMessage, nil
}
// MarshalJSON creates a JSON encoded representation of the map
func (m Legacy120ValueMap) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
notfirst := false
b.WriteByte('{')
for k, v := range m {
if notfirst {
b.WriteByte(',')
}
notfirst = true
dta, err := json.Marshal(k)
if err != nil {
return nil, err
}
b.Write(dta)
b.WriteByte(':')
dta, err = Legacy120Value(v).MarshalJSON()
if err != nil {
return nil, err
}
b.Write(dta)
}
b.WriteByte('}')
return b.Bytes(), nil
}