-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
71 lines (57 loc) · 1.36 KB
/
types.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
package gologz
import (
"encoding/json"
)
type request struct {
Endpoint string
Query queryObject
}
type queryObject struct {
From int `json:"from"`
Size int `json:"size"`
QueryType *queryType `json:"query"`
}
type queryType struct {
String *queryString `json:"query_string"`
}
type queryString struct {
Input string `json:"query"`
}
type logBag struct {
When string `json:"when"`
Service string `json:"service"`
Log string `json:"log"`
}
type LogzResponse struct {
Count float64
Logs []logBag `json:"hits"`
}
func (lr *LogzResponse) UnmarshalJSON(bytes []byte) error {
var whatever interface{}
json.Unmarshal(bytes, &whatever)
marshalMap := whatever.(map[string]interface{})
parent := marshalMap["hits"]
if child, ok := parent.(map[string]interface{}); ok {
lr.Count = child["total"].(float64)
for _, value := range child["hits"].([]interface{}) {
mapIndex := value.(map[string]interface{})
source := mapIndex["_source"].(map[string]interface{})
var log, when, service string
if value, ok := source["log"].(string); ok {
log = value
}
if value, ok := source["when"].(string); ok {
when = value
}
if value, ok := source["service"].(string); ok {
service = value
}
lr.Logs = append(lr.Logs, logBag{
Log: log,
When: when,
Service: service,
})
}
}
return nil
}