-
Notifications
You must be signed in to change notification settings - Fork 12
/
datastore_req_update_info.go
221 lines (171 loc) · 7.22 KB
/
datastore_req_update_info.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Package types implements all the types used by the DataStore protocol
package types
import (
"bytes"
"fmt"
"strings"
"github.com/PretendoNetwork/nex-go"
)
// DataStoreReqUpdateInfo is a data structure used by the DataStore protocol
type DataStoreReqUpdateInfo struct {
nex.Structure
Version uint32
URL string
RequestHeaders []*DataStoreKeyValue
FormFields []*DataStoreKeyValue
RootCACert []byte
}
// ExtractFromStream extracts a DataStoreReqUpdateInfo structure from a stream
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) ExtractFromStream(stream *nex.StreamIn) error {
datastoreVersion := stream.Server.DataStoreProtocolVersion()
var err error
if datastoreVersion.GreaterOrEqual("3.0.0") {
version, err := stream.ReadUInt32LE()
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.Version. %s", err.Error())
}
dataStoreReqUpdateInfo.Version = version
} else {
version, err := stream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.Version. %s", err.Error())
}
dataStoreReqUpdateInfo.Version = uint32(version)
}
dataStoreReqUpdateInfo.URL, err = stream.ReadString()
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.URL. %s", err.Error())
}
requestHeaders, err := stream.ReadListStructure(NewDataStoreKeyValue())
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.RequestHeaders. %s", err.Error())
}
dataStoreReqUpdateInfo.RequestHeaders = requestHeaders.([]*DataStoreKeyValue)
formFields, err := stream.ReadListStructure(NewDataStoreKeyValue())
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.FormFields. %s", err.Error())
}
dataStoreReqUpdateInfo.FormFields = formFields.([]*DataStoreKeyValue)
dataStoreReqUpdateInfo.RootCACert, err = stream.ReadBuffer()
if err != nil {
return fmt.Errorf("Failed to extract DataStoreReqUpdateInfo.RootCACert. %s", err.Error())
}
return nil
}
// Bytes encodes the DataStoreReqUpdateInfo and returns a byte array
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) Bytes(stream *nex.StreamOut) []byte {
datastoreVersion := stream.Server.DataStoreProtocolVersion()
if datastoreVersion.GreaterOrEqual("3.0.0") {
stream.WriteUInt32LE(dataStoreReqUpdateInfo.Version)
} else {
stream.WriteUInt16LE(uint16(dataStoreReqUpdateInfo.Version))
}
stream.WriteString(dataStoreReqUpdateInfo.URL)
stream.WriteListStructure(dataStoreReqUpdateInfo.RequestHeaders)
stream.WriteListStructure(dataStoreReqUpdateInfo.FormFields)
stream.WriteBuffer(dataStoreReqUpdateInfo.RootCACert)
return stream.Bytes()
}
// Copy returns a new copied instance of DataStoreReqUpdateInfo
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) Copy() nex.StructureInterface {
copied := NewDataStoreReqUpdateInfo()
copied.SetStructureVersion(dataStoreReqUpdateInfo.StructureVersion())
copied.Version = dataStoreReqUpdateInfo.Version
copied.URL = dataStoreReqUpdateInfo.URL
copied.RequestHeaders = make([]*DataStoreKeyValue, len(dataStoreReqUpdateInfo.RequestHeaders))
for i := 0; i < len(dataStoreReqUpdateInfo.RequestHeaders); i++ {
copied.RequestHeaders[i] = dataStoreReqUpdateInfo.RequestHeaders[i].Copy().(*DataStoreKeyValue)
}
copied.FormFields = make([]*DataStoreKeyValue, len(dataStoreReqUpdateInfo.FormFields))
for i := 0; i < len(dataStoreReqUpdateInfo.FormFields); i++ {
copied.FormFields[i] = dataStoreReqUpdateInfo.FormFields[i].Copy().(*DataStoreKeyValue)
}
copied.RootCACert = make([]byte, len(dataStoreReqUpdateInfo.RootCACert))
copy(copied.RootCACert, dataStoreReqUpdateInfo.RootCACert)
return copied
}
// Equals checks if the passed Structure contains the same data as the current instance
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) Equals(structure nex.StructureInterface) bool {
other := structure.(*DataStoreReqUpdateInfo)
if dataStoreReqUpdateInfo.StructureVersion() != other.StructureVersion() {
return false
}
if dataStoreReqUpdateInfo.Version != other.Version {
return false
}
if dataStoreReqUpdateInfo.URL != other.URL {
return false
}
if len(dataStoreReqUpdateInfo.RequestHeaders) != len(other.RequestHeaders) {
return false
}
for i := 0; i < len(dataStoreReqUpdateInfo.RequestHeaders); i++ {
if dataStoreReqUpdateInfo.RequestHeaders[i] != other.RequestHeaders[i] {
return false
}
}
if len(dataStoreReqUpdateInfo.FormFields) != len(other.FormFields) {
return false
}
for i := 0; i < len(dataStoreReqUpdateInfo.FormFields); i++ {
if dataStoreReqUpdateInfo.FormFields[i] != other.FormFields[i] {
return false
}
}
return bytes.Equal(dataStoreReqUpdateInfo.RootCACert, other.RootCACert)
}
// String returns a string representation of the struct
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) String() string {
return dataStoreReqUpdateInfo.FormatToString(0)
}
// FormatToString pretty-prints the struct data using the provided indentation level
func (dataStoreReqUpdateInfo *DataStoreReqUpdateInfo) FormatToString(indentationLevel int) string {
indentationValues := strings.Repeat("\t", indentationLevel+1)
indentationListValues := strings.Repeat("\t", indentationLevel+2)
indentationEnd := strings.Repeat("\t", indentationLevel)
var b strings.Builder
b.WriteString("DataStoreReqUpdateInfo{\n")
b.WriteString(fmt.Sprintf("%sstructureVersion: %d,\n", indentationValues, dataStoreReqUpdateInfo.StructureVersion()))
b.WriteString(fmt.Sprintf("%sURL: %q,\n", indentationValues, dataStoreReqUpdateInfo.URL))
if len(dataStoreReqUpdateInfo.RequestHeaders) == 0 {
b.WriteString(fmt.Sprintf("%sRequestHeaders: [],\n", indentationValues))
} else {
b.WriteString(fmt.Sprintf("%sRequestHeaders: [\n", indentationValues))
for i := 0; i < len(dataStoreReqUpdateInfo.RequestHeaders); i++ {
str := dataStoreReqUpdateInfo.RequestHeaders[i].FormatToString(indentationLevel + 2)
if i == len(dataStoreReqUpdateInfo.RequestHeaders)-1 {
b.WriteString(fmt.Sprintf("%s%s\n", indentationListValues, str))
} else {
b.WriteString(fmt.Sprintf("%s%s,\n", indentationListValues, str))
}
}
b.WriteString(fmt.Sprintf("%s],\n", indentationValues))
}
if len(dataStoreReqUpdateInfo.FormFields) == 0 {
b.WriteString(fmt.Sprintf("%sFormFields: [],\n", indentationValues))
} else {
b.WriteString(fmt.Sprintf("%sFormFields: [\n", indentationValues))
for i := 0; i < len(dataStoreReqUpdateInfo.FormFields); i++ {
str := dataStoreReqUpdateInfo.FormFields[i].FormatToString(indentationLevel + 2)
if i == len(dataStoreReqUpdateInfo.FormFields)-1 {
b.WriteString(fmt.Sprintf("%s%s\n", indentationListValues, str))
} else {
b.WriteString(fmt.Sprintf("%s%s,\n", indentationListValues, str))
}
}
b.WriteString(fmt.Sprintf("%s],\n", indentationValues))
}
b.WriteString(fmt.Sprintf("%sRootCACert: %x\n", indentationValues, dataStoreReqUpdateInfo.RootCACert))
b.WriteString(fmt.Sprintf("%s}", indentationEnd))
return b.String()
}
// NewDataStoreReqUpdateInfo returns a new DataStoreReqUpdateInfo
func NewDataStoreReqUpdateInfo() *DataStoreReqUpdateInfo {
return &DataStoreReqUpdateInfo{
Version: 0,
URL: "",
RequestHeaders: make([]*DataStoreKeyValue, 0),
FormFields: make([]*DataStoreKeyValue, 0),
RootCACert: make([]byte, 0),
}
}