-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynamic.go
135 lines (121 loc) · 2.8 KB
/
dynamic.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
/*
Package dynamic provides support for unmarshal dynamic JSON objects.
*/
package dynamic
import (
"encoding/json"
"errors"
)
// Supported types
var types = make(map[string]func() interface{})
// Register associates factory method for a type name.
// The factory method must return a pointer to a struct it's going to create.
func Register(name string, f func() interface{}) {
if _, existed := types[name]; existed {
panic("polytype: type \"" + name + "\" has already been added")
}
types[name] = f
}
// Type represents objects that have their properties at top level along with
// `Type' property. The `Type' property is not compulsory when unmarshalling
// but needed when marshalling to JSON. For example:
//
// {
// "Type": "Point",
// "X": 21,
// "Y": 3
// }
//
// is the JSON representation of:
//
// type Point struct {
// X int
// Y int
// }
//
type Type [1]interface{}
// MarshalJSON marshals the t.Value.
func (t *Type) MarshalJSON() ([]byte, error) {
return json.Marshal(t[0])
}
// UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON
// to the instance created by respective factory method.
func (t *Type) UnmarshalJSON(data []byte) error {
var envelope struct {
Type string
}
if err := json.Unmarshal(data, &envelope); err != nil {
return err
}
if envelope.Type == "" {
return errors.New("type must be specified")
}
f, ok := types[envelope.Type]
if !ok {
return errors.New("type \"" + envelope.Type + "\" is not supported")
}
value := f()
if err := json.Unmarshal(data, value); err != nil {
return err
}
t[0] = value
return nil
}
// Value returns the value marshalled in t.
func (t *Type) Value() interface{} {
return t[0]
}
// SetValue sets value to t.
func (t *Type) SetValue(v interface{}) {
t[0] = v
}
// Data represents JSON objects that have a `Type' and `Data' property for
// underlining data structure. For example:
//
// {
// "Type": "Point",
// "Data": {
// "X": 21,
// "Y": 3
// }
// }
//
// is the JSON representation of:
//
// type Point struct {
// X int
// Y int
// }
//
type Data struct {
Type string
Data interface{}
}
// UnmarshalJSON first reads Type property in JSON object, then unmarshals JSON
// to the instance created by respective factory method.
func (d *Data) UnmarshalJSON(data []byte) error {
var envelope struct {
Type string
Data json.RawMessage
}
if err := json.Unmarshal(data, &envelope); err != nil {
return err
}
if envelope.Type == "" {
return errors.New("type must be specified")
}
f, ok := types[envelope.Type]
if !ok {
return errors.New("type \"" + envelope.Type + "\" is not supported")
}
value := f()
if err := json.Unmarshal(envelope.Data, value); err != nil {
return err
}
d.Type = envelope.Type
d.Data = value
return nil
}
func (d *Data) Value() interface{} {
return d.Data
}