-
Notifications
You must be signed in to change notification settings - Fork 3
/
text_serialization_writer.go
286 lines (240 loc) · 9.76 KB
/
text_serialization_writer.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package textserialization
import (
"encoding/base64"
"errors"
"strconv"
"strings"
"time"
"github.com/google/uuid"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)
var NoStructuredDataError = errors.New("text does not support structured data")
var OnlyOneValue = errors.New("text serialization writer can only write one value")
// TextSerializationWriter implements SerializationWriter for JSON.
type TextSerializationWriter struct {
writer []string
onBeforeAssignFieldValues absser.ParsableAction
onAfterAssignFieldValues absser.ParsableAction
onStartObjectSerialization absser.ParsableWriter
}
// NewTextSerializationWriter creates a new instance of the TextSerializationWriter.
func NewTextSerializationWriter() *TextSerializationWriter {
return &TextSerializationWriter{
writer: make([]string, 0),
}
}
func (w *TextSerializationWriter) writeStringValue(key string, value string) error {
if key != "" {
return NoStructuredDataError
}
if value != "" {
if len(w.writer) > 0 {
return OnlyOneValue
}
w.writer = append(w.writer, value)
}
return nil
}
// WriteStringValue writes a String value to underlying the byte array.
func (w *TextSerializationWriter) WriteStringValue(key string, value *string) error {
if value != nil {
return w.writeStringValue(key, *value)
}
return nil
}
// WriteBoolValue writes a Bool value to underlying the byte array.
func (w *TextSerializationWriter) WriteBoolValue(key string, value *bool) error {
if value != nil {
return w.writeStringValue(key, strconv.FormatBool(*value))
}
return nil
}
// WriteByteValue writes a Byte value to underlying the byte array.
func (w *TextSerializationWriter) WriteByteValue(key string, value *byte) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt8Value writes a int8 value to underlying the byte array.
func (w *TextSerializationWriter) WriteInt8Value(key string, value *int8) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt32Value writes a Int32 value to underlying the byte array.
func (w *TextSerializationWriter) WriteInt32Value(key string, value *int32) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt64Value writes a Int64 value to underlying the byte array.
func (w *TextSerializationWriter) WriteInt64Value(key string, value *int64) error {
if value != nil {
return w.writeStringValue(key, strconv.FormatInt(*value, 10))
}
return nil
}
// WriteFloat32Value writes a Float32 value to underlying the byte array.
func (w *TextSerializationWriter) WriteFloat32Value(key string, value *float32) error {
if value != nil {
cast := float64(*value)
return w.WriteFloat64Value(key, &cast)
}
return nil
}
// WriteFloat64Value writes a Float64 value to underlying the byte array.
func (w *TextSerializationWriter) WriteFloat64Value(key string, value *float64) error {
if value != nil {
return w.writeStringValue(key, strconv.FormatFloat(*value, 'f', -1, 64))
}
return nil
}
// WriteTimeValue writes a Time value to underlying the byte array.
func (w *TextSerializationWriter) WriteTimeValue(key string, value *time.Time) error {
if value != nil {
return w.writeStringValue(key, (*value).String())
}
return nil
}
// WriteISODurationValue writes a ISODuration value to underlying the byte array.
func (w *TextSerializationWriter) WriteISODurationValue(key string, value *absser.ISODuration) error {
if value != nil {
return w.writeStringValue(key, (*value).String())
}
return nil
}
// WriteTimeOnlyValue writes a TimeOnly value to underlying the byte array.
func (w *TextSerializationWriter) WriteTimeOnlyValue(key string, value *absser.TimeOnly) error {
if value != nil {
return w.writeStringValue(key, (*value).String())
}
return nil
}
// WriteDateOnlyValue writes a DateOnly value to underlying the byte array.
func (w *TextSerializationWriter) WriteDateOnlyValue(key string, value *absser.DateOnly) error {
if value != nil {
return w.writeStringValue(key, (*value).String())
}
return nil
}
// WriteUUIDValue writes a UUID value to underlying the byte array.
func (w *TextSerializationWriter) WriteUUIDValue(key string, value *uuid.UUID) error {
if value != nil {
return w.writeStringValue(key, (*value).String())
}
return nil
}
// WriteByteArrayValue writes a ByteArray value to underlying the byte array.
func (w *TextSerializationWriter) WriteByteArrayValue(key string, value []byte) error {
if value != nil {
return w.writeStringValue(key, base64.StdEncoding.EncodeToString(value))
}
return nil
}
// WriteObjectValue writes a Parsable value to underlying the byte array.
func (w *TextSerializationWriter) WriteObjectValue(key string, item absser.Parsable, additionalValuesToMerge ...absser.Parsable) error {
return NoStructuredDataError
}
// WriteCollectionOfObjectValues writes a collection of Parsable values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfObjectValues(key string, collection []absser.Parsable) error {
return NoStructuredDataError
}
// WriteCollectionOfStringValues writes a collection of String values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfStringValues(key string, collection []string) error {
return NoStructuredDataError
}
// WriteCollectionOfInt32Values writes a collection of Int32 values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfInt32Values(key string, collection []int32) error {
return NoStructuredDataError
}
// WriteCollectionOfInt64Values writes a collection of Int64 values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfInt64Values(key string, collection []int64) error {
return NoStructuredDataError
}
// WriteCollectionOfFloat32Values writes a collection of Float32 values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfFloat32Values(key string, collection []float32) error {
return NoStructuredDataError
}
// WriteCollectionOfFloat64Values writes a collection of Float64 values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfFloat64Values(key string, collection []float64) error {
return NoStructuredDataError
}
// WriteCollectionOfTimeValues writes a collection of Time values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfTimeValues(key string, collection []time.Time) error {
return NoStructuredDataError
}
// WriteCollectionOfISODurationValues writes a collection of ISODuration values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfISODurationValues(key string, collection []absser.ISODuration) error {
return NoStructuredDataError
}
// WriteCollectionOfTimeOnlyValues writes a collection of TimeOnly values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfTimeOnlyValues(key string, collection []absser.TimeOnly) error {
return NoStructuredDataError
}
// WriteCollectionOfDateOnlyValues writes a collection of DateOnly values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfDateOnlyValues(key string, collection []absser.DateOnly) error {
return NoStructuredDataError
}
// WriteCollectionOfUUIDValues writes a collection of UUID values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfUUIDValues(key string, collection []uuid.UUID) error {
return NoStructuredDataError
}
// WriteCollectionOfBoolValues writes a collection of Bool values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfBoolValues(key string, collection []bool) error {
return NoStructuredDataError
}
// WriteCollectionOfByteValues writes a collection of Byte values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfByteValues(key string, collection []byte) error {
return NoStructuredDataError
}
// WriteCollectionOfInt8Values writes a collection of int8 values to underlying the byte array.
func (w *TextSerializationWriter) WriteCollectionOfInt8Values(key string, collection []int8) error {
return NoStructuredDataError
}
// GetSerializedContent returns the resulting byte array from the serialization writer.
func (w *TextSerializationWriter) GetSerializedContent() ([]byte, error) {
resultStr := strings.Join(w.writer, "")
return []byte(resultStr), nil
}
// WriteAdditionalData writes additional data to underlying the byte array.
func (w *TextSerializationWriter) WriteAdditionalData(value map[string]interface{}) error {
return NoStructuredDataError
}
// WriteAnyValue an unknown value as a parameter.
func (w *TextSerializationWriter) WriteAnyValue(key string, value interface{}) error {
return NoStructuredDataError
}
// Close clears the internal buffer.
func (w *TextSerializationWriter) Close() error {
return nil
}
func (w *TextSerializationWriter) WriteNullValue(key string) error {
return NoStructuredDataError
}
func (w *TextSerializationWriter) GetOnBeforeSerialization() absser.ParsableAction {
return w.onBeforeAssignFieldValues
}
func (w *TextSerializationWriter) SetOnBeforeSerialization(action absser.ParsableAction) error {
w.onBeforeAssignFieldValues = action
return nil
}
func (w *TextSerializationWriter) GetOnAfterObjectSerialization() absser.ParsableAction {
return w.onAfterAssignFieldValues
}
func (w *TextSerializationWriter) SetOnAfterObjectSerialization(action absser.ParsableAction) error {
w.onAfterAssignFieldValues = action
return nil
}
func (w *TextSerializationWriter) GetOnStartObjectSerialization() absser.ParsableWriter {
return w.onStartObjectSerialization
}
func (w *TextSerializationWriter) SetOnStartObjectSerialization(writer absser.ParsableWriter) error {
w.onStartObjectSerialization = writer
return nil
}