-
Notifications
You must be signed in to change notification settings - Fork 7
/
messagetype.go
106 lines (89 loc) · 3.25 KB
/
messagetype.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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"strconv"
)
//go:generate go install golang.org/x/tools/cmd/stringer@latest
//go:generate stringer -type=MessageType
// MessageType indicates the kind of WRP message
type MessageType int64
const (
Invalid0MessageType MessageType = iota
Invalid1MessageType
AuthorizationMessageType
SimpleRequestResponseMessageType
SimpleEventMessageType
CreateMessageType
RetrieveMessageType
UpdateMessageType
DeleteMessageType
ServiceRegistrationMessageType
ServiceAliveMessageType
UnknownMessageType
LastMessageType
)
// RequiresTransaction tests if messages of this type are allowed to participate in transactions.
// If this method returns false, the TransactionUUID field should be ignored (but passed through
// where applicable). If this method returns true, TransactionUUID must be included in request.
func (mt MessageType) RequiresTransaction() bool {
switch mt {
case SimpleRequestResponseMessageType, CreateMessageType, RetrieveMessageType, UpdateMessageType, DeleteMessageType:
return true
default:
return false
}
}
// SupportsQOSAck tests if messages of this type are allowed to participate in QOS Ack
// as specified in https://xmidt.io/docs/wrp/basics/#qos-description-qos .
// If this method returns false, QOS Ack is foregone.
func (mt MessageType) SupportsQOSAck() bool {
switch mt {
case SimpleEventMessageType:
return true
default:
return false
}
}
// FriendlyName is just the String version of this type minus the "MessageType" suffix.
// This is used in most textual representations, such as HTTP headers.
func (mt MessageType) FriendlyName() string {
return friendlyNames[mt]
}
var (
// stringToMessageType is a simple map of allowed strings which uniquely indicate MessageType values.
// Included in this map are integral string keys. Keys are assumed to be case insensitive.
stringToMessageType map[string]MessageType
// friendlyNames are the string representations of each message type without the "MessageType" suffix
friendlyNames map[MessageType]string
)
func init() {
stringToMessageType = make(map[string]MessageType, LastMessageType-1)
friendlyNames = make(map[MessageType]string, LastMessageType-1)
suffixLength := len("MessageType")
// for each MessageType, allow the following string representations:
//
// The integral value of the constant
// The String() value
// The String() value minus the MessageType suffix
for v := SimpleRequestResponseMessageType; v < LastMessageType; v++ {
stringToMessageType[strconv.Itoa(int(v))] = v
vs := v.String()
f := vs[0 : len(vs)-suffixLength]
stringToMessageType[vs] = v
stringToMessageType[f] = v
friendlyNames[v] = f
}
stringToMessageType["event"] = SimpleEventMessageType
}
// StringToMessageType converts a string into an enumerated MessageType constant.
// If the value equals the friendly name of a type, e.g. "Auth" for AuthMessageType,
// that type is returned. Otherwise, the value is converted to an integer and looked up,
// with an error being returned in the event the integer value is not valid.
func StringToMessageType(value string) MessageType {
mt, ok := stringToMessageType[value]
if !ok {
return LastMessageType
}
return mt
}