blob: 356543baf8be93f2f2e8f5a1aeb393cc182b11ff (
plain) (
blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#include "value.h"
#include <yql/essentials/minikql/dom/node.h>
namespace NYql::NJsonPath {
using namespace NUdf;
using namespace NDom;
using namespace NKikimr;
using namespace NKikimr::NBinaryJson;
TArrayIterator::TArrayIterator()
: Iterator(TEmptyMarker())
{
}
TArrayIterator::TArrayIterator(const TUnboxedValue& iterator)
: Iterator(iterator)
{
}
TArrayIterator::TArrayIterator(TUnboxedValue&& iterator)
: Iterator(std::move(iterator))
{
}
TArrayIterator::TArrayIterator(const NBinaryJson::TArrayIterator& iterator)
: Iterator(iterator)
{
}
TArrayIterator::TArrayIterator(NBinaryJson::TArrayIterator&& iterator)
: Iterator(std::move(iterator))
{
}
bool TArrayIterator::Next(TValue& value) {
if (std::holds_alternative<TEmptyMarker>(Iterator)) {
return false;
} else if (auto* iterator = std::get_if<NBinaryJson::TArrayIterator>(&Iterator)) {
if (!iterator->HasNext()) {
return false;
}
value = TValue(iterator->Next());
return true;
} else if (auto* iterator = std::get_if<TUnboxedValue>(&Iterator)) {
TUnboxedValue result;
const bool success = iterator->Next(result);
if (success) {
value = TValue(result);
}
return success;
} else {
Y_ABORT("Unexpected variant case in Next");
}
}
TObjectIterator::TObjectIterator()
: Iterator(TEmptyMarker())
{
}
TObjectIterator::TObjectIterator(const TUnboxedValue& iterator)
: Iterator(iterator)
{
}
TObjectIterator::TObjectIterator(TUnboxedValue&& iterator)
: Iterator(std::move(iterator))
{
}
TObjectIterator::TObjectIterator(const NBinaryJson::TObjectIterator& iterator)
: Iterator(iterator)
{
}
TObjectIterator::TObjectIterator(NBinaryJson::TObjectIterator&& iterator)
: Iterator(std::move(iterator))
{
}
bool TObjectIterator::Next(TValue& key, TValue& value) {
if (std::holds_alternative<TEmptyMarker>(Iterator)) {
return false;
} else if (auto* iterator = std::get_if<NBinaryJson::TObjectIterator>(&Iterator)) {
if (!iterator->HasNext()) {
return false;
}
const auto [itKey, itValue] = iterator->Next();
key = TValue(itKey);
value = TValue(itValue);
return true;
} else if (auto* iterator = std::get_if<TUnboxedValue>(&Iterator)) {
TUnboxedValue itKey;
TUnboxedValue itValue;
const bool success = iterator->NextPair(itKey, itValue);
if (success) {
key = TValue(itKey);
value = TValue(itValue);
}
return success;
} else {
Y_ABORT("Unexpected variant case in Next");
}
}
TValue::TValue()
: Value(MakeEntity())
{
}
TValue::TValue(const TUnboxedValue& value)
: Value(value)
{
}
TValue::TValue(TUnboxedValue&& value)
: Value(std::move(value))
{
}
TValue::TValue(const TEntryCursor& value)
: Value(value)
{
UnpackInnerValue();
}
TValue::TValue(TEntryCursor&& value)
: Value(std::move(value))
{
UnpackInnerValue();
}
TValue::TValue(const TContainerCursor& value)
: Value(value)
{
UnpackInnerValue();
}
TValue::TValue(TContainerCursor&& value)
: Value(std::move(value))
{
UnpackInnerValue();
}
EValueType TValue::GetType() const {
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
switch (value->GetType()) {
case EEntryType::BoolFalse:
case EEntryType::BoolTrue:
return EValueType::Bool;
case EEntryType::Null:
return EValueType::Null;
case EEntryType::Number:
return EValueType::Number;
case EEntryType::String:
return EValueType::String;
case EEntryType::Container:
Y_ABORT("Logical error: TEntryCursor with Container type must be converted to TContainerCursor");
}
} else if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
switch (value->GetType()) {
case EContainerType::Array:
return EValueType::Array;
case EContainerType::Object:
return EValueType::Object;
case EContainerType::TopLevelScalar:
Y_ABORT("Logical error: TContainerCursor with TopLevelScalar type must be converted to TEntryCursor");
}
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
switch (GetNodeType(*value)) {
case ENodeType::Bool:
return EValueType::Bool;
case ENodeType::Double:
case ENodeType::Int64:
case ENodeType::Uint64:
return EValueType::Number;
case ENodeType::Dict:
case ENodeType::Attr:
return EValueType::Object;
case ENodeType::List:
return EValueType::Array;
case ENodeType::String:
return EValueType::String;
case ENodeType::Entity:
return EValueType::Null;
}
} else {
Y_ABORT("Unexpected variant case in GetType");
}
}
bool TValue::Is(EValueType type) const {
return GetType() == type;
}
bool TValue::IsBool() const {
return Is(EValueType::Bool);
}
bool TValue::IsNumber() const {
return Is(EValueType::Number);
}
bool TValue::IsString() const {
return Is(EValueType::String);
}
bool TValue::IsNull() const {
return Is(EValueType::Null);
}
bool TValue::IsObject() const {
return Is(EValueType::Object);
}
bool TValue::IsArray() const {
return Is(EValueType::Array);
}
double TValue::GetNumber() const {
Y_DEBUG_ABORT_UNLESS(IsNumber());
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
return value->GetNumber();
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
if (IsNodeType(*value, ENodeType::Double)) {
return value->Get<double>();
} else if (IsNodeType(*value, ENodeType::Int64)) {
return static_cast<double>(value->Get<i64>());
} else {
return static_cast<double>(value->Get<ui64>());
}
} else {
Y_ABORT("Unexpected variant case in GetNumber");
}
}
bool TValue::GetBool() const {
Y_DEBUG_ABORT_UNLESS(IsBool());
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
return value->GetType() == EEntryType::BoolTrue;
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
return value->Get<bool>();
} else {
Y_ABORT("Unexpected variant case in GetBool");
}
}
const TStringBuf TValue::GetString() const {
Y_DEBUG_ABORT_UNLESS(IsString());
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
return value->GetString();
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
return value->AsStringRef();
} else {
Y_ABORT("Unexpected variant case in GetString");
}
}
ui32 TValue::GetSize() const {
Y_DEBUG_ABORT_UNLESS(IsArray() || IsObject());
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
return value->GetSize();
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
if (value->IsEmbedded()) {
return 0;
}
if (IsNodeType(*value, ENodeType::List)) {
return value->GetListLength();
} else {
return value->GetDictLength();
}
} else {
Y_ABORT("Unexpected variant case in GetSize");
}
}
TValue TValue::GetElement(ui32 index) const {
Y_DEBUG_ABORT_UNLESS(IsArray());
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
return TValue(value->GetElement(index));
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
return TValue(value->Lookup(TUnboxedValuePod(index)));
} else {
Y_ABORT("Unexpected variant case in GetElement");
}
}
TArrayIterator TValue::GetArrayIterator() const {
Y_DEBUG_ABORT_UNLESS(IsArray());
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
return TArrayIterator(value->GetArrayIterator());
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
if (value->IsEmbedded()) {
return TArrayIterator();
}
return TArrayIterator(value->GetListIterator());
} else {
Y_ABORT("Unexpected variant case in GetArrayIterator");
}
}
TMaybe<TValue> TValue::Lookup(const TStringBuf key) const {
Y_DEBUG_ABORT_UNLESS(IsObject());
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
const auto payload = value->Lookup(key);
if (!payload.Defined()) {
return Nothing();
}
return TValue(*payload);
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
if (value->IsEmbedded()) {
return Nothing();
}
// Lookup on TUnboxedValue can be performed only with TUnboxedValue key.
// To avoid allocating new string we use our custom Lookup method defined
// on underlying TMapNode that accepts TStringRef
const auto* dict = static_cast<const TMapNode*>(value->AsBoxed().Get());
if (const auto payload = dict->Lookup(key)) {
return {TValue(payload)};
} else {
return Nothing();
}
} else {
Y_ABORT("Unexpected variant case in Lookup");
}
}
TObjectIterator TValue::GetObjectIterator() const {
Y_DEBUG_ABORT_UNLESS(IsObject());
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
return TObjectIterator(value->GetObjectIterator());
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
if (value->IsEmbedded()) {
return TObjectIterator();
}
return TObjectIterator(value->GetDictIterator());
} else {
Y_ABORT("Unexpected variant case in GetObjectIterator");
}
}
TUnboxedValue TValue::ConvertToUnboxedValue(const NUdf::IValueBuilder* valueBuilder) const {
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
return ReadElementToJsonDom(*value, valueBuilder);
} else if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
return ReadContainerToJsonDom(*value, valueBuilder);
} else if (const auto* value = std::get_if<TUnboxedValue>(&Value)) {
return *value;
} else {
Y_ABORT("Unexpected variant case in ConvertToUnboxedValue");
}
}
void TValue::UnpackInnerValue() {
// If TEntryCursor points to container, we need to extract TContainerCursor
if (const auto* value = std::get_if<TEntryCursor>(&Value)) {
if (value->GetType() == EEntryType::Container) {
Value = value->GetContainer();
}
}
// If TContainerCursor points to top level scalar, we need to extract TEntryCursor
if (const auto* value = std::get_if<TContainerCursor>(&Value)) {
if (value->GetType() == EContainerType::TopLevelScalar) {
Value = value->GetElement(0);
}
}
}
}
|