-
Notifications
You must be signed in to change notification settings - Fork 7
/
header_wrp.go
168 lines (143 loc) · 3.86 KB
/
header_wrp.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
// Map string to MessageType int
/*
func StringToMessageType(str string) MessageType {
switch str {
case "Auth":
return AuthMessageType
case "SimpleRequestResponse":
return SimpleRequestResponseMessageType
case "SimpleEvent":
return SimpleEventMessageType
case "Create":
return CreateMessageType
case "Retrieve":
return RetrieveMessageType
case "Update":
return UpdateMessageType
case "Delete":
return DeleteMessageType
case "ServiceRegistration":
return ServiceRegistrationMessageType
case "ServiceAlive":
return ServiceAliveMessageType
default:
return -1
}
}
*/
// Convert HTTP header to WRP generic Message
/*
func HeaderToWRP(header http.Header) (*Message, error) {
msg := new(Message)
// MessageType is mandatory
msgType := header.Get(MsgTypeHeader)
if !strings.EqualFold(msgType, "") && StringToMessageType(msgType) != MessageType(-1) {
msg.Type = StringToMessageType(msgType)
} else {
return nil, ErrInvalidMsgType
}
if src := header.Get(SourceHeader); !strings.EqualFold(src, "") {
msg.Source = src
}
if transUuid := header.Get(TransactionUuidHeader); !strings.EqualFold(transUuid, "") {
msg.TransactionUUID = transUuid
}
if status := header.Get(StatusHeader); !strings.EqualFold(status, "") {
if statusInt, err := strconv.ParseInt(status, 10, 64); err == nil {
msg.SetStatus(statusInt)
} else {
return nil, err
}
}
if rdr := header.Get(RDRHeader); !strings.EqualFold(rdr, "") {
if rdrInt, err := strconv.ParseInt(rdr, 10, 64); err == nil {
msg.SetRequestDeliveryResponse(rdrInt)
} else {
return nil, err
}
}
if path := header.Get(PathHeader); !strings.EqualFold(path, "") {
msg.Path = path
}
if includeSpans := header.Get(IncludeSpansHeader); !strings.EqualFold(includeSpans, "") {
if spansBool, err := strconv.ParseBool(includeSpans); err == nil {
msg.SetIncludeSpans(spansBool)
} else {
return nil, err
}
}
// Handle Headers and Spans which contain multiple values
for key, value := range header {
if strings.EqualFold(key, HeadersArrHeader) {
if msg.Headers == nil {
msg.Headers = []string{}
}
for item := range value {
msg.Headers = append(msg.Headers, value[item])
}
}
// Each span element will look like this {"name" , "start_time" , "duration"}
if strings.EqualFold(key, SpansHeader) {
if msg.Spans == nil {
msg.Spans = make([][]string, len(value))
}
j := 0
for i := 0; i < len(value); i++ {
msg.Spans[j] = append(msg.Spans[j], value[i])
if (i+1)%3 == 0 {
j++
}
}
}
}
return msg, nil
}
*/
// Convert WRP generic Message to HTTP header
/*
func WRPToHeader(msg *Message) (header http.Header, err error) {
header = make(map[string][]string)
// Message Type is mandatory
if strings.EqualFold(msg.Type.String(), InvalidMessageTypeString) {
return nil, ErrInvalidMsgType
} else {
header.Add(MsgTypeHeader, msg.Type.String())
}
if msg.Status != nil {
header.Add(StatusHeader, strconv.FormatInt(*msg.Status, 10))
}
if !strings.EqualFold(msg.Source, "") {
header.Add(SourceHeader, msg.Source)
}
if !strings.EqualFold(msg.TransactionUUID, "") {
header.Add(TransactionUuidHeader, msg.TransactionUUID)
}
if !strings.EqualFold(msg.Path, "") {
header.Add(PathHeader, msg.Path)
}
if msg.RequestDeliveryResponse != nil {
header.Add(RDRHeader, strconv.FormatInt(*msg.RequestDeliveryResponse, 10))
}
if msg.IncludeSpans != nil {
header.Add(IncludeSpansHeader, strconv.FormatBool(*msg.IncludeSpans))
}
if msg.Spans != nil {
for i := 0; i < len(msg.Spans); i++ {
for _, span := range msg.Spans[i] {
header.Add(SpansHeader, span)
}
}
}
if msg.Headers != nil {
if msg.Headers != nil {
for _, hdr := range msg.Headers {
header.Add(HeadersArrHeader, hdr)
}
}
}
return
}
*/