Skip to content

Commit e8951ff

Browse files
authored
Allow defining nested structure in wrap type (#417)
Allow to define a nested structure for wrap-only type
1 parent c9d1b82 commit e8951ff

38 files changed

+1261
-257
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# v1.7.7-alpha1
1+
# v1.8.0-alpha2
22

33
* Added `TI_PROTO_CLIENT_REQ_EMIT_PEER` protocol, pr #414.
4-
* Changed `copy()` behavior of wrapped type, pr #415.
4+
* Changed `copy()` behavior for wrapped type, pr #415.
55
* Fixed missing wrap-prefix flags after renaming type, issue #416.
6+
* Allow defining nested structure in wrap type, pr #417.
67

78
# v1.7.6
89

inc/ti/condition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int ti_condition_field_rel_init(
2626
ti_field_t * ofield,
2727
ex_t * e);
2828
int ti_condition_init_enum(ti_field_t * field, ti_member_t * member, ex_t * e);
29+
int ti_condition_init_type(ti_field_t * field, ex_t * e);
2930

3031
void ti_condition_destroy(ti_condition_via_t condition, uint16_t spec);
3132

inc/ti/condition.t.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ typedef struct ti_condition_irange_s ti_condition_irange_t;
1212
typedef struct ti_condition_drange_s ti_condition_drange_t;
1313
typedef struct ti_condition_rel_s ti_condition_rel_t;
1414

15+
#include <stdint.h>
16+
#include <stdlib.h>
17+
#include <ti/field.t.h>
18+
#include <ti/raw.t.h>
19+
#include <ti/regex.h>
20+
#include <ti/thing.t.h>
21+
#include <ti/type.t.h>
22+
#include <ti/val.t.h>
23+
1524
union ti_condition_via_u
1625
{
26+
ti_type_t * type; /* nested type (wrap-only) */
1727
ti_condition_re_t * re; /* str, utf8 */
1828
ti_condition_srange_t * srange; /* str, utf8 */
1929
ti_condition_irange_t * irange; /* int, float */
@@ -22,14 +32,6 @@ union ti_condition_via_u
2232
ti_condition_t * none; /* NULL */
2333
};
2434

25-
#include <stdint.h>
26-
#include <stdlib.h>
27-
#include <ti/raw.t.h>
28-
#include <ti/regex.h>
29-
#include <ti/val.t.h>
30-
#include <ti/field.t.h>
31-
#include <ti/thing.t.h>
32-
3335
typedef void (*ti_condition_rel_cb) (ti_field_t *, ti_thing_t *, ti_thing_t *);
3436

3537
struct ti_condition_s

inc/ti/field.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int ti_field_make_assignable(
4646
ex_t * e);
4747
_Bool ti_field_maps_to_val(ti_field_t * field, ti_val_t * val);
4848
_Bool ti_field_maps_to_field(ti_field_t * t_field, ti_field_t * f_field);
49+
ti_raw_t * ti_field_nested_to_mpdata(ti_field_t * field);
4950
ti_field_t * ti_field_by_strn_e(
5051
ti_type_t * type,
5152
const char * str,
@@ -83,24 +84,31 @@ static inline _Bool ti_field_has_relation(ti_field_t * field)
8384
* is a non-nillable set or a nillable type.
8485
*/
8586
return field->condition.none && (
86-
field->spec == TI_SPEC_SET ||
87-
(field->spec & TI_SPEC_MASK_NILLABLE) < TI_SPEC_ANY);
87+
field->spec == TI_SPEC_SET ||
88+
(field->spec & TI_SPEC_MASK_NILLABLE) < TI_SPEC_ANY);
8889
}
8990

9091
static inline uint8_t ti_field_deep(ti_field_t * field, uint8_t deep)
9192
{
92-
return (
93-
(field->flags & TI_FIELD_FLAG_DEEP)
94-
? deep
95-
: (field->flags & TI_FIELD_FLAG_MAX_DEEP)
96-
? TI_MAX_DEEP-1
97-
: 0
98-
) + (field->flags & TI_FIELD_FLAG_SAME_DEEP);
93+
return (
94+
(field->flags & TI_FIELD_FLAG_DEEP)
95+
? deep
96+
: (field->flags & TI_FIELD_FLAG_MAX_DEEP)
97+
? TI_MAX_DEEP-1
98+
: 0
99+
) + (field->flags & TI_FIELD_FLAG_SAME_DEEP);
99100
}
100101

101102
static inline int ti_field_ret_flags(ti_field_t * field)
102103
{
103-
return field->flags & TI_FIELD_FLAG_NO_IDS;
104+
return field->flags & TI_FIELD_FLAG_NO_IDS;
105+
}
106+
107+
static inline _Bool ti_field_is_nested(ti_field_t * field)
108+
{
109+
return (
110+
(field->spec & TI_SPEC_MASK_NILLABLE) == TI_SPEC_TYPE ||
111+
(field->spec & TI_SPEC_MASK_NILLABLE) == TI_SPEC_ARR_TYPE);
104112
}
105113

106114

inc/ti/fmt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ typedef struct ti_fmt_s ti_fmt_t;
1111
#include <ti/raw.t.h>
1212

1313
/* When 0, use TAB, else the number of spaces */
14-
#define TI_FMT_SPACES 0
14+
#define TI_FMT_TAB 0
1515

1616
struct ti_fmt_s
1717
{

inc/ti/fn/fnjsondump.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static int do__f_json_dump(ti_query_t * query, cleri_node_t * nd, ex_t * e)
4444
size_t total_n;
4545
ti_vp_t vp = {
4646
.query=query,
47+
.size_limit=ti.cfg->result_size_limit,
4748
};
4849
json_dumps__options_t options = {
4950
.deep = query->qbind.deep,

inc/ti/fn/fnmodtype.h

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -409,56 +409,66 @@ static void type__add(
409409

410410
return;
411411
}
412-
else if (!ti_val_is_str(query->rval))
413-
{
414-
ex_set(e, EX_TYPE_ERROR,
415-
"function `%s` expects argument 4 to be of "
416-
"type `"TI_VAL_STR_S"` or type `"TI_VAL_CLOSURE_S"` "
417-
"but got type `%s` instead"DOC_MOD_TYPE_ADD,
418-
fnname, ti_val_str(query->rval));
412+
413+
spec_raw = ti_type_nested_from_val(type, query->rval, e);
414+
if (e->nr)
419415
return;
420-
}
421416

422-
spec_raw = (ti_raw_t *) query->rval;
423-
if (spec_raw->n == 1 && spec_raw->data[0] == '#')
417+
if (spec_raw)
418+
ti_val_unsafe_gc_drop(query->rval);
419+
else
424420
{
425-
if (nargs == 5)
421+
if (!ti_val_is_str(query->rval))
426422
{
427-
ex_set(e, EX_NUM_ARGUMENTS,
428-
"function `%s` takes at most 4 arguments when adding "
429-
"an Id ('#') definition"DOC_MOD_TYPE_ADD,
430-
fnname);
423+
ex_set(e, EX_TYPE_ERROR,
424+
"function `%s` expects argument 4 to be of "
425+
"type `"TI_VAL_STR_S"` or type `"TI_VAL_CLOSURE_S"` "
426+
"but got type `%s` instead"DOC_MOD_TYPE_ADD,
427+
fnname, ti_val_str(query->rval));
431428
return;
432429
}
430+
spec_raw = (ti_raw_t *) query->rval;
433431

434-
if (type->idname)
432+
if (spec_raw->n == 1 && spec_raw->data[0] == '#')
435433
{
436-
ex_set(e, EX_LOOKUP_ERROR,
437-
"multiple Id ('#') definitions on type `%s`",
438-
type->name);
439-
return;
440-
}
434+
if (nargs == 5)
435+
{
436+
ex_set(e, EX_NUM_ARGUMENTS,
437+
"function `%s` takes at most 4 arguments when adding "
438+
"an Id ('#') definition"DOC_MOD_TYPE_ADD,
439+
fnname);
440+
return;
441+
}
441442

442-
task = ti_task_get_task(query->change, query->collection->root);
443-
if (!task)
444-
{
445-
ex_set_mem(e);
446-
return;
447-
}
443+
if (type->idname)
444+
{
445+
ex_set(e, EX_LOOKUP_ERROR,
446+
"multiple Id ('#') definitions on type `%s`",
447+
type->name);
448+
return;
449+
}
450+
451+
task = ti_task_get_task(query->change, query->collection->root);
452+
if (!task)
453+
{
454+
ex_set_mem(e);
455+
return;
456+
}
448457

449-
/* update modified time-stamp */
450-
type->modified_at = util_now_usec();
451-
type->idname = name;
452-
ti_incref(name);
458+
/* update modified time-stamp */
459+
type->modified_at = util_now_usec();
460+
type->idname = name;
461+
ti_incref(name);
453462

454-
if (ti_task_add_mod_type_add_idname(task, type))
455-
{
456-
/* modified is wrong; the rest we can revert */
457-
type->idname = NULL;
458-
ti_decref(name);
459-
ex_set_mem(e);
463+
if (ti_task_add_mod_type_add_idname(task, type))
464+
{
465+
/* modified is wrong; the rest we can revert */
466+
type->idname = NULL;
467+
ti_decref(name);
468+
ex_set_mem(e);
469+
}
470+
return;
460471
}
461-
return;
462472
}
463473

464474
query->rval = NULL;
@@ -857,7 +867,7 @@ static void type__mod(
857867
const int nargs = fn_get_nargs(nd);
858868
ti_field_t * field = ti_field_by_name(type, name);
859869
ti_method_t * method = field ? NULL : ti_type_get_method(type, name);
860-
870+
ti_raw_t * spec_raw = NULL;
861871
cleri_node_t * child;
862872

863873
if (fn_nargs_range(fnname, DOC_MOD_TYPE_MOD, 4, 5, nargs, e))
@@ -935,10 +945,12 @@ static void type__mod(
935945
return;
936946
}
937947

948+
spec_raw = ti_type_nested_from_val(type, query->rval, e);
949+
if (e->nr)
950+
return;
951+
938952
if (ti_val_is_str(query->rval))
939953
{
940-
ti_raw_t * spec_raw;
941-
942954
if (!field)
943955
{
944956
ex_set(e, EX_TYPE_ERROR,
@@ -956,7 +968,10 @@ static void type__mod(
956968
}
957969

958970
query->rval = NULL;
971+
}
959972

973+
if (spec_raw)
974+
{
960975
if (nargs == 4)
961976
{
962977
ti_task_t * task;
@@ -1453,7 +1468,7 @@ static void type__wpo(
14531468
if (wrap_only && ti_type_required_by_non_wpo(type, e))
14541469
return;
14551470

1456-
if (!wrap_only && ti_type_uses_wpo(type, e))
1471+
if (!wrap_only && ti_type_requires_wpo(type, e))
14571472
return;
14581473

14591474
task = ti_task_get_task(query->change, query->collection->root);

inc/ti/fn/fnsettype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int do__f_set_type(ti_query_t * query, cleri_node_t * nd, ex_t * e)
5656
type = ti_type_create(
5757
query->collection->types,
5858
type_id,
59-
TI_TYPE_FLAG_WRAP_ONLY, /* prevents looking for instances */
59+
TI_TYPE_FLAG_WRAP_ONLY,
6060
(const char *) rname->data,
6161
rname->n,
6262
ts_now,

inc/ti/raw.inline.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ static inline _Bool ti_raw_is_name(ti_raw_t * raw)
3232
return raw->tp == TI_VAL_NAME;
3333
}
3434

35+
static inline _Bool ti_raw_is_mpdata(ti_raw_t * raw)
36+
{
37+
return raw->tp == TI_VAL_MPDATA;
38+
}
39+
3540
static inline _Bool ti_raw_is_reserved_key(ti_raw_t * raw)
3641
{
3742
return raw->n == 1 && (*raw->data >> 4 == 2);

inc/ti/spec.t.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ typedef enum
4545
TI_SPEC_FLOAT_RANGE, /* `float<:> */
4646
TI_SPEC_STR_RANGE, /* `str<:> */
4747
TI_SPEC_UTF8_RANGE, /* `utf8<:> */
48+
TI_SPEC_TYPE, /* `{...} */
49+
TI_SPEC_ARR_TYPE, /* `[{...}] */
4850
} ti_spec_enum_t;
4951

5052
typedef enum

0 commit comments

Comments
 (0)