forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_api_response.go
88 lines (80 loc) · 2.33 KB
/
feature_api_response.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 growthbook
import (
"encoding/json"
"errors"
"time"
)
const dateLayout = "2006-01-02T15:04:05.000Z"
type FeatureAPIResponse struct {
Status int `json:"status"`
Features map[string]*Feature `json:"features"`
DateUpdated time.Time `json:"dateUpdated"`
EncryptedFeatures string `json:"encryptedFeatures"`
}
// Implement normal JSON marshalling interfaces for convenience.
func (r *FeatureAPIResponse) MarshalJSON() ([]byte, error) {
type Alias FeatureAPIResponse
return json.Marshal(&struct {
*Alias
DateUpdated string `json:"dateUpdated"`
}{
Alias: (*Alias)(r),
DateUpdated: r.DateUpdated.Format(dateLayout),
})
}
func (r *FeatureAPIResponse) UnmarshalJSON(data []byte) error {
parsed := ParseFeatureAPIResponse(data)
if parsed == nil {
return errors.New("failed to parse feature API response")
}
r.Features = parsed.Features
r.DateUpdated = parsed.DateUpdated
r.EncryptedFeatures = parsed.EncryptedFeatures
return nil
}
// ParseFeature creates a single Feature value from raw JSON input.
func ParseFeatureAPIResponse(data []byte) *FeatureAPIResponse {
dict := make(map[string]interface{})
err := json.Unmarshal(data, &dict)
if err != nil {
logError("Failed parsing JSON input", "FeatureAPIResponse")
return nil
}
return BuildFeatureAPIResponse(dict)
}
// BuildFeatureAPIResponse creates a FeatureAPIResponse value from a
// generic JSON value.
func BuildFeatureAPIResponse(dict map[string]interface{}) *FeatureAPIResponse {
apiResponse := FeatureAPIResponse{}
for k, v := range dict {
switch k {
case "status":
status, ok := jsonInt(v, "FeatureAPIResponse", "status")
if !ok {
return nil
}
apiResponse.Status = status
case "features":
apiResponse.Features = BuildFeatures(v)
case "dateUpdated":
dateUpdated, ok := jsonString(v, "FeatureAPIResponse", "dateUpdated")
if !ok {
return nil
}
var err error
apiResponse.DateUpdated, err = time.Parse(dateLayout, dateUpdated)
if err != nil {
return nil
}
case "encryptedFeatures":
encryptedFeatures, ok := jsonString(v, "FeatureAPIResponse", "encryptedFeatures")
if !ok {
return nil
}
apiResponse.EncryptedFeatures = encryptedFeatures
default:
logWarn("Unknown key in JSON data", "FeatureAPIResponse", k)
}
}
return &apiResponse
}