-
Notifications
You must be signed in to change notification settings - Fork 24
/
cacheexpiry.go
81 lines (74 loc) · 1.99 KB
/
cacheexpiry.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
package apigee
import (
"fmt"
"encoding/json"
)
// CacheExpiry represents the expiry settings on a cache. This struct marshals
// and unmarshals between the json format Edge uses and a reasonably clear
// golang struct.
type CacheExpiry struct {
ExpiryType string
ExpiryValue string
ValuesNull bool
}
// serialization format:
// {
// -one of-
// "expiryDate": { "value": "{mm-dd-yyyy}" },
// "timeoutInSec" : { "value" : "300" },
// "timeOfDay": { "value" : "hh:mm:ss" },
// -and-
// "valuesNull" : false
// }
//
// MarshalJSON implements the json.Marshaler interface. It marshals from
// the form used by Apigee Edge into a CacheExpiry struct. Eg,
//
// {
// "expiryDate": { "value": "{mm-dd-yyyy}" },
// "valuesNull" : false
// }
//
func (ce CacheExpiry) MarshalJSON() ([]byte, error) {
valueMap := map[string]string{}
valueMap["value"] = ce.ExpiryValue
m1 := map[string]interface{}{}
m1["valuesNull"] = ce.ValuesNull
m1[ce.ExpiryType] = valueMap
j,_ := json.Marshal(m1)
return []byte(j), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface. It unmarshals from
// a string like
//
// {
// "expiryDate": { "value": "{mm-dd-yyyy}" },
// "valuesNull" : false
// }
//
// ...into a CacheExpiry struct.
//
func (ce *CacheExpiry) UnmarshalJSON(b []byte) error {
var m1 map[string]interface{}
e := json.Unmarshal(b, &m1)
if e == nil {
entry := CacheExpiry{}
if v, ok := m1["valuesNull"]; ok {
entry.ValuesNull = v.(bool)
} else {
entry.ValuesNull = false
}
candidates := []string{"expiryDate", "timeOfDay", "timeoutInSec"}
for _, k := range candidates {
if v, ok := m1[k]; ok {
entry.ExpiryType = k
entry.ExpiryValue = ((v.(map[string]interface{}))["value"]).(string)
}
}
*ce = entry
}
return nil
}
func (ce CacheExpiry) String() string {
return fmt.Sprintf("CacheExpiry[%s,%s,%t]", ce.ExpiryType,ce.ExpiryValue, ce.ValuesNull)
}