mongory 2.1.1-aarch64-linux → 2.2.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +45 -74
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +28 -1
  6. data/lib/core/2.6/mongory_ext.so +0 -0
  7. data/lib/core/2.7/mongory_ext.so +0 -0
  8. data/lib/core/3.0/mongory_ext.so +0 -0
  9. data/lib/core/3.1/mongory_ext.so +0 -0
  10. data/lib/core/3.2/mongory_ext.so +0 -0
  11. data/lib/core/3.3/mongory_ext.so +0 -0
  12. data/lib/core/3.4/mongory_ext.so +0 -0
  13. data/lib/core/4.0/mongory_ext.so +0 -0
  14. data/lib/mongory/converters/abstract_converter.rb +5 -5
  15. data/lib/mongory/converters/converted.rb +0 -4
  16. data/lib/mongory/converters/key_converter.rb +13 -36
  17. data/lib/mongory/converters/value_converter.rb +4 -4
  18. data/lib/mongory/matchers.rb +8 -7
  19. data/lib/mongory/query_builder.rb +9 -0
  20. data/lib/mongory/utils/context.rb +0 -6
  21. data/lib/mongory/utils/singleton_builder.rb +1 -4
  22. data/lib/mongory/version.rb +1 -1
  23. data/lib/mongory.rb +0 -1
  24. data/mongory.gemspec +13 -12
  25. data/script/coverage.rb +84 -0
  26. metadata +9 -54
  27. data/ext/mongory_ext/extconf.rb +0 -115
  28. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  36. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  37. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  38. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  39. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  40. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  41. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  42. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  44. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  46. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  47. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  49. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  50. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  52. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  54. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  56. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  58. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  60. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  62. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -483
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  64. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  69. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  71. data/ext/mongory_ext/mongory_ext.c +0 -66
  72. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  73. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  74. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  75. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,529 +0,0 @@
1
- /**
2
- * @file matcher.c
3
- * @brief Implements the generic mongory_matcher constructor.
4
- *
5
- * This file provides the implementation for the top-level matcher creation
6
- * function.
7
- */
8
- #include <stdio.h>
9
- #include <string.h>
10
- #include "mongory-core/matchers/matcher.h" // Public API
11
- #include "../foundations/utils.h" // For mongory_string_cpyf
12
- // Required internal headers for delegation
13
- #include "../foundations/config_private.h" // Potentially for global settings
14
- #include "../foundations/memory_pool_private.h"
15
- #include "base_matcher.h" // For mongory_matcher_base_new if used directly
16
- #include "composite_matcher.h" // For mongory_matcher_condition_new
17
- #include "value_matcher.h" // Potentially for other default constructions
18
- #include "mongory-core/foundations/array.h"
19
- #include "../foundations/string_buffer.h"
20
- #include "mongory-core/foundations/memory_pool.h"
21
- #include "mongory-core/foundations/value.h"
22
- #include <mongory-core.h> // General include, might not be strictly necessary here
23
-
24
-
25
- /**
26
- * @brief Creates a new matcher based on the provided condition.
27
- *
28
- * This is the primary public entry point for creating a matcher. The library
29
- * uses a factory pattern where this function determines the appropriate
30
- * specific matcher to create based on the structure of the `condition` value.
31
- *
32
- * Currently, it always delegates to `mongory_matcher_condition_new`,
33
- * which handles query documents (tables). This is the most common use case,
34
- * where the condition is a table like `{ "field": { "$op": "value" } }`.
35
- *
36
- * @param pool The memory pool to be used for allocating the matcher.
37
- * @param condition A `mongory_value` defining the matching criteria. This is
38
- * typically a `mongory_table`.
39
- * @return mongory_matcher* A pointer to the newly constructed matcher, or NULL
40
- * if allocation fails or the condition is invalid.
41
- */
42
- mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx) {
43
- // The core logic is delegated to a more specific constructor.
44
- // This design allows for easy extension; for example, a different constructor
45
- // could be chosen here based on the `condition->type`.
46
- mongory_compile_budget saved = mongory_compile_budget_begin();
47
- mongory_matcher *matcher = mongory_matcher_condition_new(pool, condition, extern_ctx);
48
- mongory_compile_budget_end(saved);
49
- if (matcher == NULL) {
50
- return NULL;
51
- }
52
-
53
- return matcher;
54
- }
55
-
56
- /**
57
- * @brief Executes the matching logic for the given matcher.
58
- *
59
- * This function is a polymorphic wrapper. It invokes the `match` function
60
- * pointer on the specific `mongory_matcher` instance, which will be one of
61
- * the internal matching functions (e.g., from a compare_matcher or
62
- * composite_matcher).
63
- *
64
- * @param matcher The matcher to use.
65
- * @param value The value to check against the matcher's condition.
66
- * @return True if the value satisfies the matcher's condition, false otherwise.
67
- */
68
- bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value) { return matcher->match(matcher, value); }
69
-
70
- static mongory_value *mongory_matcher_build_explain_node(
71
- mongory_matcher *matcher,
72
- mongory_memory_pool *pool,
73
- bool as_skip
74
- ) {
75
- MONGORY_VALIDATE_PTR(pool, matcher);
76
- if (pool->error != NULL) {
77
- return NULL;
78
- }
79
- mongory_table *node = mongory_table_new(pool);
80
- mongory_array *children = mongory_array_new(pool);
81
- if (!node || !children || pool->error != NULL) {
82
- return NULL;
83
- }
84
- const char *name = matcher->name ? matcher->name : "";
85
- node->set(node, "name", mongory_value_wrap_s(pool, (char *)name));
86
- node->set(node, "condition", matcher->condition ? matcher->condition : mongory_value_wrap_n(pool, NULL));
87
- node->set(node, "priority", mongory_value_wrap_d(pool, matcher->priority));
88
- if (strcmp(name, "Field") == 0) {
89
- mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
90
- if (field_matcher->field) {
91
- node->set(node, "field", mongory_value_wrap_s(pool, field_matcher->field));
92
- }
93
- }
94
- if (as_skip) {
95
- node->set(node, "result", mongory_value_wrap_s(pool, "skip"));
96
- }
97
-
98
- if (matcher->traverse == mongory_matcher_composite_traverse) {
99
- mongory_composite_matcher *composite = (mongory_composite_matcher *)matcher;
100
- mongory_array *src_children = mongory_atomic_load_ptr_acquire(&composite->children);
101
- int total = src_children ? (int)src_children->count : 0;
102
- for (int i = 0; i < total; i++) {
103
- mongory_matcher *child = (mongory_matcher *)src_children->get(src_children, i);
104
- mongory_value *child_node = mongory_matcher_build_explain_node(child, pool, as_skip);
105
- if (!child_node || pool->error != NULL) {
106
- return NULL;
107
- }
108
- children->push(children, child_node);
109
- }
110
- } else if (matcher->traverse == mongory_matcher_value_traverse) {
111
- mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
112
- mongory_matcher *next = literal->implicit_array_matcher ? literal->implicit_array_matcher : literal->delegate_matcher;
113
- if (next) {
114
- mongory_value *child_node = mongory_matcher_build_explain_node(next, pool, as_skip);
115
- if (!child_node || pool->error != NULL) {
116
- return NULL;
117
- }
118
- children->push(children, child_node);
119
- }
120
- }
121
-
122
- node->set(node, "children", mongory_value_wrap_a(pool, children));
123
- return mongory_value_wrap_t(pool, node);
124
- }
125
-
126
- static void mongory_explain_tree_print_node(
127
- mongory_value *node_val,
128
- mongory_memory_pool *pool,
129
- const char *prefix,
130
- int count,
131
- int total
132
- ) {
133
- if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
134
- return;
135
- }
136
- mongory_table *node = node_val->data.t;
137
- mongory_value *name_val = node->get(node, "name");
138
- mongory_value *cond_val = node->get(node, "condition");
139
- mongory_value *field_val = node->get(node, "field");
140
- mongory_value *children_val = node->get(node, "children");
141
-
142
- const char *connection = "";
143
- const char *indent = "";
144
- if (total > 0) {
145
- if (total - count == 1) {
146
- connection = "└─ ";
147
- indent = " ";
148
- } else {
149
- connection = "├─ ";
150
- indent = "│ ";
151
- }
152
- }
153
-
154
- const char *name = (name_val && name_val->type == MONGORY_TYPE_STRING && name_val->data.s) ? name_val->data.s : "";
155
- char *cond_str = (cond_val && cond_val->to_str) ? cond_val->to_str(cond_val, pool) : "null";
156
- if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
157
- printf("%s%sField: \"%s\", to match: %s\n", prefix, connection, field_val->data.s, cond_str);
158
- } else {
159
- printf("%s%s%s: %s\n", prefix, connection, name, cond_str);
160
- }
161
-
162
- if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
163
- mongory_array *children = children_val->data.a;
164
- int child_total = (int)children->count;
165
- char *next_prefix = mongory_string_cpyf(pool, "%s%s", prefix, indent);
166
- if (!next_prefix) {
167
- return;
168
- }
169
- for (int i = 0; i < child_total; i++) {
170
- mongory_explain_tree_print_node(children->get(children, i), pool, next_prefix, i, child_total);
171
- }
172
- }
173
- }
174
-
175
- mongory_value *mongory_matcher_explain_tree(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
176
- MONGORY_VALIDATE_PTR(temp_pool, matcher);
177
- if (temp_pool->error != NULL) {
178
- return NULL;
179
- }
180
- return mongory_matcher_build_explain_node(matcher, temp_pool, false);
181
- }
182
-
183
- void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
184
- mongory_value *tree = mongory_matcher_explain_tree(matcher, temp_pool);
185
- if (tree != NULL) {
186
- mongory_explain_tree_print_node(tree, temp_pool, "", 0, 0);
187
- }
188
- }
189
-
190
- typedef struct mongory_matcher_traced_match_context {
191
- mongory_matcher *matcher;
192
- mongory_value *record;
193
- bool matched;
194
- int level;
195
- } mongory_matcher_traced_match_context;
196
-
197
- static bool mongory_matcher_traced_match(mongory_matcher *matcher, mongory_value *value) {
198
- bool matched = matcher->original_match(matcher, value);
199
- mongory_array *trace_stack = matcher->trace_stack;
200
- mongory_memory_pool *pool = trace_stack->pool;
201
- MONGORY_VALIDATE_PTR(pool, matcher->name);
202
- if (pool->error != NULL) {
203
- return false;
204
- }
205
-
206
- mongory_matcher_traced_match_context *trace_result = MG_ALLOC_PTR(pool, mongory_matcher_traced_match_context);
207
- if (!trace_result) {
208
- return matched;
209
- }
210
- trace_result->matcher = matcher;
211
- trace_result->record = value;
212
- trace_result->matched = matched;
213
- trace_result->level = matcher->trace_level;
214
- trace_stack->push(trace_stack, mongory_value_wrap_ptr(pool, (void *)trace_result));
215
-
216
- return matched;
217
- }
218
-
219
- static bool mongory_matcher_enable_trace_cb(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
220
- matcher->trace_stack = (mongory_array *)ctx->acc;
221
- matcher->trace_level = ctx->level;
222
- matcher->match = mongory_matcher_traced_match;
223
- if (matcher->traverse == mongory_matcher_value_traverse) {
224
- mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
225
- if (literal->implicit_array_matcher && literal->delegate_matcher) {
226
- mongory_matcher_traverse_context delegate_ctx = {
227
- .pool = ctx->pool,
228
- .level = ctx->level + 1,
229
- .count = 0,
230
- .total = 1,
231
- .acc = ctx->acc,
232
- .callback = ctx->callback,
233
- };
234
- literal->delegate_matcher->traverse(literal->delegate_matcher, &delegate_ctx);
235
- }
236
- }
237
- return true;
238
- }
239
-
240
- static bool mongory_matcher_disable_trace_cb(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
241
- matcher->match = matcher->original_match;
242
- matcher->trace_stack = NULL;
243
- if (matcher->traverse == mongory_matcher_value_traverse) {
244
- mongory_value_matcher *literal = (mongory_value_matcher *)matcher;
245
- if (literal->implicit_array_matcher && literal->delegate_matcher) {
246
- mongory_matcher_traverse_context delegate_ctx = {
247
- .level = ctx->level + 1,
248
- .count = 0,
249
- .total = 1,
250
- .callback = ctx->callback,
251
- };
252
- literal->delegate_matcher->traverse(literal->delegate_matcher, &delegate_ctx);
253
- }
254
- }
255
- return true;
256
- }
257
-
258
- static bool mongory_matcher_ptr_in_array(mongory_array *arr, mongory_matcher *target) {
259
- int total = arr ? (int)arr->count : 0;
260
- for (int i = 0; i < total; i++) {
261
- mongory_value *v = arr->get(arr, i);
262
- if (v && v->data.ptr == (void *)target) {
263
- return true;
264
- }
265
- }
266
- return false;
267
- }
268
-
269
- static mongory_array *mongory_matcher_build_trace_nodes(
270
- mongory_array *traces,
271
- int level,
272
- mongory_memory_pool *pool,
273
- mongory_array *executed_matchers_out
274
- ) {
275
- MONGORY_VALIDATE_PTR(pool, traces) && MONGORY_VALIDATE_PTR(pool, traces->get);
276
- if (pool->error != NULL) {
277
- return NULL;
278
- }
279
- mongory_array *nodes = mongory_array_new(pool);
280
- mongory_array *group = mongory_array_new(pool);
281
- if (!nodes || !group || pool->error != NULL) {
282
- return NULL;
283
- }
284
- int total = (int)traces->count;
285
- for (int i = 0; i < total; i++) {
286
- mongory_value *item = traces->get(traces, i);
287
- MONGORY_VALIDATE_PTR(pool, item) && MONGORY_VALIDATE_PTR(pool, item->data.ptr);
288
- if (pool->error != NULL) {
289
- return NULL;
290
- }
291
- mongory_matcher_traced_match_context *entry = (mongory_matcher_traced_match_context *)item->data.ptr;
292
- if (entry->level == level) {
293
- if (executed_matchers_out) {
294
- mongory_value *ptr_val = mongory_value_wrap_ptr(pool, (void *)entry->matcher);
295
- if (!ptr_val || !executed_matchers_out->push(executed_matchers_out, ptr_val)) {
296
- return NULL;
297
- }
298
- }
299
- mongory_array *executed_children = mongory_array_new(pool);
300
- if (!executed_children || pool->error != NULL) {
301
- return NULL;
302
- }
303
- mongory_array *children = mongory_matcher_build_trace_nodes(group, level + 1, pool, executed_children);
304
- if (!children || pool->error != NULL) {
305
- return NULL;
306
- }
307
-
308
- mongory_matcher *m = entry->matcher;
309
- if (m->traverse == mongory_matcher_composite_traverse) {
310
- mongory_composite_matcher *comp = (mongory_composite_matcher *)m;
311
- mongory_array *src_children = mongory_atomic_load_ptr_acquire(&comp->children);
312
- int child_total = src_children ? (int)src_children->count : 0;
313
- for (int j = 0; j < child_total; j++) {
314
- mongory_matcher *child = (mongory_matcher *)src_children->get(src_children, j);
315
- if (!mongory_matcher_ptr_in_array(executed_children, child)) {
316
- mongory_value *skip_node = mongory_matcher_build_explain_node(child, pool, true);
317
- if (!skip_node || pool->error != NULL) {
318
- return NULL;
319
- }
320
- children->push(children, skip_node);
321
- }
322
- }
323
- } else if (m->traverse == mongory_matcher_value_traverse) {
324
- mongory_value_matcher *lit = (mongory_value_matcher *)m;
325
- mongory_matcher *next = lit->implicit_array_matcher ? lit->implicit_array_matcher : lit->delegate_matcher;
326
- if (next && children->count == 0) {
327
- mongory_value *skip_node = mongory_matcher_build_explain_node(next, pool, true);
328
- if (!skip_node || pool->error != NULL) {
329
- return NULL;
330
- }
331
- children->push(children, skip_node);
332
- }
333
- }
334
-
335
- mongory_table *node = mongory_table_new(pool);
336
- if (!node || pool->error != NULL) {
337
- return NULL;
338
- }
339
- const char *name = m->name ? m->name : "";
340
- node->set(node, "name", mongory_value_wrap_s(pool, (char *)name));
341
- node->set(node, "condition", m->condition ? m->condition : mongory_value_wrap_n(pool, NULL));
342
- node->set(node, "priority", mongory_value_wrap_d(pool, m->priority));
343
- if (strcmp(name, "Field") == 0) {
344
- mongory_field_matcher *field_matcher = (mongory_field_matcher *)m;
345
- if (field_matcher->field) {
346
- node->set(node, "field", mongory_value_wrap_s(pool, field_matcher->field));
347
- }
348
- }
349
- if (entry->record != NULL) {
350
- node->set(node, "record", entry->record);
351
- }
352
- node->set(node, "result", mongory_value_wrap_s(pool, entry->matched ? "pass" : "fail"));
353
- node->set(node, "children", mongory_value_wrap_a(pool, children));
354
-
355
- mongory_value *wrapped_node = mongory_value_wrap_t(pool, node);
356
- if (!wrapped_node || !nodes->push(nodes, wrapped_node) || pool->error != NULL) {
357
- return NULL;
358
- }
359
- group = mongory_array_new(pool);
360
- if (!group) {
361
- return NULL;
362
- }
363
- } else {
364
- if (!group->push(group, item)) {
365
- return NULL;
366
- }
367
- }
368
- }
369
- return nodes;
370
- }
371
-
372
- void mongory_matcher_enable_trace_at(mongory_matcher *matcher, mongory_array *trace_stack, int level) {
373
- if (!matcher || !trace_stack) {
374
- return;
375
- }
376
- MONGORY_VALIDATE_PTR(trace_stack->pool, matcher->traverse);
377
- if (trace_stack->pool->error != NULL) {
378
- return;
379
- }
380
- mongory_matcher_traverse_context ctx = {
381
- .pool = trace_stack->pool,
382
- .level = level,
383
- .count = 0,
384
- .total = 0,
385
- .acc = (void *)trace_stack,
386
- .callback = mongory_matcher_enable_trace_cb,
387
- };
388
- matcher->traverse(matcher, &ctx);
389
- }
390
-
391
- void mongory_matcher_enable_trace(mongory_matcher *matcher, mongory_memory_pool *temp_pool) {
392
- MONGORY_VALIDATE_PTR(temp_pool, matcher) && MONGORY_VALIDATE_PTR(temp_pool, matcher->traverse);
393
- if (temp_pool->error != NULL) {
394
- return;
395
- }
396
- mongory_array *trace_stack = mongory_array_new(temp_pool);
397
- if (!trace_stack) {
398
- return;
399
- }
400
- mongory_matcher_enable_trace_at(matcher, trace_stack, 0);
401
- }
402
-
403
- void mongory_matcher_disable_trace(mongory_matcher *matcher) {
404
- MONGORY_VALIDATE_PTR(matcher->pool, matcher) && MONGORY_VALIDATE_PTR(matcher->pool, matcher->traverse);
405
- if (matcher->pool->error != NULL) {
406
- return;
407
- }
408
- mongory_matcher_traverse_context ctx = {
409
- .level = 0,
410
- .count = 0,
411
- .total = 0,
412
- .callback = mongory_matcher_disable_trace_cb,
413
- };
414
- matcher->traverse(matcher, &ctx);
415
- }
416
-
417
- static void mongory_matcher_print_trace_node(mongory_value *node_val, mongory_memory_pool *pool, int level) {
418
- if (!node_val || node_val->type != MONGORY_TYPE_TABLE || !node_val->data.t) {
419
- return;
420
- }
421
- mongory_table *node = node_val->data.t;
422
- mongory_value *name_val = node->get(node, "name");
423
- mongory_value *cond_val = node->get(node, "condition");
424
- mongory_value *field_val = node->get(node, "field");
425
- mongory_value *record_val = node->get(node, "record");
426
- mongory_value *result_val = node->get(node, "result");
427
- mongory_value *children_val = node->get(node, "children");
428
-
429
- const char *name = (name_val && name_val->type == MONGORY_TYPE_STRING && name_val->data.s) ? name_val->data.s : "";
430
- const char *res_raw = (result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s) ? result_val->data.s : "fail";
431
- const char *res_label = NULL;
432
- bool is_skip = strcmp(res_raw, "skip") == 0;
433
- bool is_pass = strcmp(res_raw, "pass") == 0;
434
- if (mongory_matcher_trace_result_colorful) {
435
- if (is_skip) {
436
- res_label = "\e[30;43mSkipped\e[0m";
437
- } else if (is_pass) {
438
- res_label = "\e[30;42mMatched\e[0m";
439
- } else {
440
- res_label = "\e[30;41mDismatch\e[0m";
441
- }
442
- } else {
443
- if (is_skip) {
444
- res_label = "Skipped";
445
- } else if (is_pass) {
446
- res_label = "Matched";
447
- } else {
448
- res_label = "Dismatch";
449
- }
450
- }
451
-
452
- char *cdtn = (cond_val && cond_val->to_str) ? cond_val->to_str(cond_val, pool) : "null";
453
- char *rcd = (record_val && record_val->to_str) ? record_val->to_str(record_val, pool) : "Nothing";
454
-
455
- for (int i = 0; i < level * 2; i++) {
456
- putchar(' ');
457
- }
458
- if (field_val && field_val->type == MONGORY_TYPE_STRING && field_val->data.s) {
459
- if (is_skip) {
460
- printf("%s: %s, field: \"%s\", condition: %s\n", name, res_label, field_val->data.s, cdtn);
461
- } else {
462
- printf("%s: %s, field: \"%s\", condition: %s, record: %s\n", name, res_label, field_val->data.s, cdtn, rcd);
463
- }
464
- } else {
465
- if (is_skip) {
466
- printf("%s: %s, condition: %s\n", name, res_label, cdtn);
467
- } else {
468
- printf("%s: %s, condition: %s, record: %s\n", name, res_label, cdtn, rcd);
469
- }
470
- }
471
-
472
- if (children_val && children_val->type == MONGORY_TYPE_ARRAY && children_val->data.a) {
473
- mongory_array *children = children_val->data.a;
474
- int child_total = (int)children->count;
475
- for (int i = 0; i < child_total; i++) {
476
- mongory_matcher_print_trace_node(children->get(children, i), pool, level + 1);
477
- }
478
- }
479
- }
480
-
481
- void mongory_matcher_print_trace(mongory_matcher *matcher) {
482
- if (!matcher || matcher->trace_stack == NULL) {
483
- return;
484
- }
485
- mongory_memory_pool *pool = matcher->trace_stack->pool;
486
- mongory_array *roots = mongory_matcher_build_trace_nodes(matcher->trace_stack, 0, pool, NULL);
487
- if (!roots || pool->error != NULL) {
488
- return;
489
- }
490
- int total = (int)roots->count;
491
- for (int i = 0; i < total; i++) {
492
- mongory_matcher_print_trace_node(roots->get(roots, i), pool, 0);
493
- }
494
- }
495
-
496
- mongory_value *mongory_matcher_trace_tree(mongory_matcher *matcher, mongory_value *value) {
497
- mongory_memory_pool *pool = value ? value->pool : (matcher ? matcher->pool : NULL);
498
- MONGORY_VALIDATE_PTR(pool, matcher) && MONGORY_VALIDATE_PTR(pool, matcher->match);
499
- if (!pool || pool->error != NULL) {
500
- return NULL;
501
- }
502
- mongory_matcher_enable_trace(matcher, pool);
503
- if (pool->error != NULL) {
504
- mongory_matcher_disable_trace(matcher);
505
- return NULL;
506
- }
507
- matcher->match(matcher, value);
508
- mongory_array *roots = mongory_matcher_build_trace_nodes(matcher->trace_stack, 0, pool, NULL);
509
- mongory_matcher_disable_trace(matcher);
510
- if (!roots || roots->count == 0 || pool->error != NULL) {
511
- return NULL;
512
- }
513
- return roots->get(roots, 0);
514
- }
515
-
516
- bool mongory_matcher_trace(mongory_matcher *matcher, mongory_value *value) {
517
- mongory_memory_pool *pool = value ? value->pool : (matcher ? matcher->pool : NULL);
518
- MONGORY_VALIDATE_PTR(pool, matcher) && MONGORY_VALIDATE_PTR(pool, matcher->match);
519
- if (!pool || pool->error != NULL) {
520
- return false;
521
- }
522
- mongory_value *tree = mongory_matcher_trace_tree(matcher, value);
523
- if (!tree || tree->type != MONGORY_TYPE_TABLE || !tree->data.t) {
524
- return false;
525
- }
526
- mongory_matcher_print_trace_node(tree, pool, 0);
527
- mongory_value *result_val = tree->data.t->get(tree->data.t, "result");
528
- return result_val && result_val->type == MONGORY_TYPE_STRING && result_val->data.s && strcmp(result_val->data.s, "pass") == 0;
529
- }
@@ -1,117 +0,0 @@
1
- #ifndef MONGORY_MATCHER_EXPLAINABLE_C
2
- #define MONGORY_MATCHER_EXPLAINABLE_C
3
-
4
- #include "matcher_explainable.h"
5
- #include "../foundations/string_buffer.h"
6
- #include "base_matcher.h"
7
- #include "composite_matcher.h"
8
- #include "value_matcher.h"
9
- #include "../foundations/utils.h"
10
- #include <stdio.h>
11
-
12
- char *mongory_matcher_title(mongory_matcher *matcher, mongory_memory_pool *pool) {
13
- mongory_value *condition = matcher->condition;
14
- char *cond_str = (condition && condition->to_str) ? condition->to_str(condition, pool) : NULL;
15
- if (!cond_str) {
16
- return NULL;
17
- }
18
- return mongory_string_cpyf(pool, "%s: %s",
19
- matcher->name ? matcher->name : "",
20
- cond_str
21
- );
22
- }
23
-
24
- char *mongory_matcher_title_with_field(mongory_matcher *matcher, mongory_memory_pool *pool) {
25
- mongory_field_matcher *field_matcher = (mongory_field_matcher *)matcher;
26
- mongory_value *condition = matcher->condition;
27
- char *cond_str = (condition && condition->to_str) ? condition->to_str(condition, pool) : NULL;
28
- if (!cond_str) {
29
- return NULL;
30
- }
31
- return mongory_string_cpyf(pool, "Field: \"%s\", to match: %s",
32
- field_matcher->field ? field_matcher->field : "",
33
- cond_str
34
- );
35
- }
36
-
37
- static inline char *mongory_matcher_tail_connection(int count, int total) {
38
- if (total == 0) {
39
- return "";
40
- }
41
- if (total - count == 1) {
42
- return "└─ ";
43
- }
44
- return "├─ ";
45
- }
46
-
47
- static inline char *mongory_matcher_indent_connection(int count, int total) {
48
- if (total == 0) {
49
- return "";
50
- }
51
- if (total - count == 1) {
52
- return " ";
53
- }
54
- return "│ ";
55
- }
56
-
57
- static mongory_explain_acc *mongory_explain_acc_child(
58
- mongory_memory_pool *pool,
59
- mongory_explain_acc *parent_acc,
60
- int count,
61
- int total
62
- ) {
63
- char *indent = mongory_matcher_indent_connection(count, total);
64
- mongory_explain_acc *next = MG_ALLOC_PTR(pool, mongory_explain_acc);
65
- if (!next) {
66
- return NULL;
67
- }
68
- next->prefix = mongory_string_cpyf(pool, "%s%s", parent_acc->prefix, indent);
69
- if (!next->prefix) {
70
- return NULL;
71
- }
72
- next->buf = parent_acc->buf;
73
- return next;
74
- }
75
-
76
- static bool mongory_matcher_append_explain_line(
77
- mongory_matcher_traverse_context *ctx,
78
- char *title
79
- ) {
80
- mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
81
- char *connection = mongory_matcher_tail_connection(ctx->count, ctx->total);
82
- if (!acc || !acc->buf || !title) {
83
- return false;
84
- }
85
- mongory_string_buffer_appendf(acc->buf, "%s%s%s\n", acc->prefix, connection, title);
86
- return ctx->pool->error == NULL;
87
- }
88
-
89
- bool mongory_matcher_base_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
90
- char *title = mongory_matcher_title(matcher, ctx->pool);
91
- return mongory_matcher_append_explain_line(ctx, title);
92
- }
93
-
94
- bool mongory_matcher_composite_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
95
- if (!mongory_matcher_base_explain(matcher, ctx)) {
96
- return false;
97
- }
98
- mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
99
- ctx->acc = mongory_explain_acc_child(ctx->pool, acc, ctx->count, ctx->total);
100
- return ctx->acc != NULL;
101
- }
102
-
103
- bool mongory_matcher_value_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
104
- return mongory_matcher_composite_explain(matcher, ctx);
105
- }
106
-
107
- bool mongory_matcher_field_explain(mongory_matcher *matcher, mongory_matcher_traverse_context *ctx) {
108
- char *title = mongory_matcher_title_with_field(matcher, ctx->pool);
109
- if (!mongory_matcher_append_explain_line(ctx, title)) {
110
- return false;
111
- }
112
- mongory_explain_acc *acc = (mongory_explain_acc *)ctx->acc;
113
- ctx->acc = mongory_explain_acc_child(ctx->pool, acc, ctx->count, ctx->total);
114
- return ctx->acc != NULL;
115
- }
116
-
117
- #endif /* MONGORY_MATCHER_EXPLAINABLE_C */
@@ -1,29 +0,0 @@
1
- #ifndef MONGORY_MATCHER_EXPLAINABLE_H
2
- #define MONGORY_MATCHER_EXPLAINABLE_H
3
-
4
- #include "mongory-core/matchers/matcher.h"
5
- #include "../foundations/string_buffer.h"
6
- #include "matcher_traversable.h"
7
- #include <mongory-core.h>
8
-
9
- typedef struct mongory_explain_acc {
10
- char *prefix;
11
- mongory_string_buffer *buf;
12
- } mongory_explain_acc;
13
-
14
-
15
- /**
16
- * @brief Explains a matcher.
17
- * @param matcher The matcher to explain.
18
- * @param ctx The context to use for the explanation.
19
- */
20
-
21
- bool mongory_matcher_base_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
22
-
23
- bool mongory_matcher_composite_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
24
-
25
- bool mongory_matcher_field_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
26
-
27
- bool mongory_matcher_value_explain(struct mongory_matcher *matcher, mongory_matcher_traverse_context *ctx);
28
-
29
- #endif /* MONGORY_MATCHER_EXPLAINABLE_H */