-
Notifications
You must be signed in to change notification settings - Fork 48
/
helper.go
127 lines (118 loc) · 3.31 KB
/
helper.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package queries
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
kubemq "github.com/kubemq-io/kubemq-go"
"strconv"
)
type object struct {
Id string `json:"id"`
Channel string `json:"channel,omitempty"`
ClientId string `json:"client_id,omitempty"`
Metadata string `json:"metadata,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Timeout string `json:"timeout,omitempty"`
BodyJson json.RawMessage `json:"body_json,omitempty"`
BodyString string `json:"body_string,omitempty"`
Executed string `json:"executed,omitempty"`
ExecutedAt string `json:"executed_at,omitempty"`
Error string `json:"error,omitempty"`
CacheHit string `json:"cache_hit,omitempty"`
}
func newObjectWithQueryReceive(query *kubemq.QueryReceive) *object {
obj := &object{
Id: query.Id,
Channel: query.Channel,
ClientId: query.ClientId,
Metadata: query.Metadata,
Tags: query.Tags,
BodyJson: json.RawMessage{},
BodyString: "",
Executed: "",
ExecutedAt: "",
Error: "",
CacheHit: "",
Timeout: "",
}
var js json.RawMessage
if err := json.Unmarshal(query.Body, &js); err == nil {
obj.BodyJson = js
} else {
sDec, err := b64.StdEncoding.DecodeString(string(query.Body))
if err != nil {
obj.BodyString = string(query.Body)
} else {
obj.BodyString = string(sDec)
}
}
return obj
}
func newObjectWithQueryResponse(response *kubemq.QueryResponse) *object {
obj := &object{
Id: response.QueryId,
Channel: "",
ClientId: response.ResponseClientId,
Metadata: response.Metadata,
Tags: response.Tags,
BodyJson: json.RawMessage{},
BodyString: "",
Executed: strconv.FormatBool(response.Executed),
ExecutedAt: response.ExecutedAt.Format("2006-01-02 15:04:05.999"),
Error: response.Error,
CacheHit: strconv.FormatBool(response.CacheHit),
}
var js json.RawMessage
if err := json.Unmarshal(response.Body, &js); err == nil {
obj.BodyJson = js
} else {
sDec, err := b64.StdEncoding.DecodeString(string(response.Body))
if err != nil {
obj.BodyString = string(response.Body)
} else {
obj.BodyString = string(sDec)
}
}
return obj
}
func newObjectWithCommand(query *kubemq.Query) *object {
obj := &object{
Id: query.Id,
Channel: query.Channel,
ClientId: query.ClientId,
Metadata: query.Metadata,
Tags: query.Tags,
Timeout: query.Timeout.String(),
BodyJson: json.RawMessage{},
BodyString: "",
Executed: "",
ExecutedAt: "",
Error: "",
CacheHit: "",
}
var js json.RawMessage
if err := json.Unmarshal(query.Body, &js); err == nil {
obj.BodyJson = js
} else {
sDec, err := b64.StdEncoding.DecodeString(string(query.Body))
if err != nil {
obj.BodyString = string(query.Body)
} else {
obj.BodyString = string(sDec)
}
}
return obj
}
func (o *object) String() string {
data, _ := json.MarshalIndent(o, "", " ")
return string(data)
}
func printQueryReceive(query *kubemq.QueryReceive) {
fmt.Println(newObjectWithQueryReceive(query))
}
func printQueryResponse(response *kubemq.QueryResponse) {
fmt.Println(newObjectWithQueryResponse(response))
}
func printQuery(query *kubemq.Query) {
fmt.Println(newObjectWithCommand(query))
}