-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
69 lines (55 loc) · 1.54 KB
/
parse.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
package client
import (
"encoding/json"
"fmt"
)
// ParseCollection parses JSON bytes to a slice of generic maps
func ParseCollection(b []byte) ([]map[string]interface{}, error) {
var collection []map[string]interface{}
err := json.Unmarshal(b, &collection)
if err != nil {
return nil, err
}
return collection, nil
}
// ParseObject parses JSON bytes to a generic map
func ParseObject(b []byte) (map[string]interface{}, error) {
var jsonData map[string]interface{}
err := json.Unmarshal(b, &jsonData)
if err != nil {
return nil, err
}
return jsonData, nil
}
// ParseID parses JSON bytes to an ID
func ParseID(b []byte) (ID, error) {
var jsonData []map[string]interface{}
err := json.Unmarshal(b, &jsonData)
if err != nil {
return nil, err
}
if len(jsonData) == 0 {
return nil, fmt.Errorf("not found")
}
return ParseIDFromMap(jsonData[0])
}
// ParseIDFromMap parses a JSON data map to an ID
func ParseIDFromMap(data map[string]interface{}) (ID, error) {
if data["id"] == nil {
return nil, fmt.Errorf("failed to parse ID. Missing attribute 'id' in %+v", data)
}
if data["modelIndex"] == nil {
return nil, fmt.Errorf("failed to parse ID. Missing attribute 'modelIndex' in %+v", data)
}
if data["service"] == nil {
return nil, fmt.Errorf("failed to parse ID. Missing attribute 'service' in %+v", data)
}
uuid := data["id"].(string)
modelIndex := data["modelIndex"].(string)
s := data["service"].(string)
service, err := ParseService(s)
if err != nil {
return nil, err
}
return NewID(service, modelIndex, uuid), nil
}