-
Notifications
You must be signed in to change notification settings - Fork 19
/
lazy_value.go
50 lines (43 loc) · 920 Bytes
/
lazy_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
package v1
import (
"encoding/json"
)
// VariableValue variable value which can be app, config or secret
type LazyValue struct {
Value interface{}
doc []byte
}
// UnmarshalJSON unmarshal from json data
func (v *LazyValue) UnmarshalJSON(b []byte) error {
v.doc = b
return nil
}
// SetJSON set the json doc
func (v *LazyValue) SetJSON(doc []byte) {
v.doc = doc
}
// GetJSON get the json doc
func (v *LazyValue) GetJSON() []byte {
return v.doc
}
// MarshalJSON marshal to json data
func (v LazyValue) MarshalJSON() ([]byte, error) {
if v.doc != nil {
return v.doc, nil
}
return json.Marshal(v.Value)
}
// Unmarshal unmarshal from json data to obj
func (v *LazyValue) Unmarshal(obj interface{}) error {
if v.doc != nil {
return json.Unmarshal(v.doc, obj)
}
if v.Value != nil {
bs, err := json.Marshal(v.Value)
if err != nil {
return err
}
return json.Unmarshal(bs, obj)
}
return nil
}