Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v1.10.4-alpha0
# v1.10.4-alpha1

* Add support for marking properties as read-only, pr #470.
* Added `dict.filter(..)` function, pr #471.

# v1.10.3

Expand Down
1 change: 1 addition & 0 deletions inc/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
#define DOC_DICT_DEL DOC_SEE("data-types/dict/del")
#define DOC_DICT_DUP DOC_SEE("data-types/dict/dup")
#define DOC_DICT_EACH DOC_SEE("data-types/dict/each")
#define DOC_DICT_FILTER DOC_SEE("data-types/dict/filter")
#define DOC_DICT_GET DOC_SEE("data-types/dict/get")
#define DOC_DICT_HAS DOC_SEE("data-types/dict/has")
#define DOC_DICT_KEYS DOC_SEE("data-types/dict/keys")
Expand Down
1 change: 1 addition & 0 deletions inc/doc.inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static inline const char * doc_filter(ti_val_t * val)
case TI_VAL_THING: return DOC_THING_FILTER;
case TI_VAL_ARR: return DOC_LIST_FILTER;
case TI_VAL_SET: return DOC_SET_FILTER;
case TI_VAL_DICT: return DOC_DICT_FILTER;
default: return NULL;
}
}
Expand Down
5 changes: 0 additions & 5 deletions inc/ti/fn/fneach.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ static int do__f_each(ti_query_t * query, cleri_node_t * nd, ex_t * e)
.closure = closure,
.query = query,
};
;

if (ti_closure_arg_used(closure, 0)
? ti_dict_items(
Expand All @@ -183,11 +182,7 @@ static int do__f_each(ti_query_t * query, cleri_node_t * nd, ex_t * e)
(ti_dict_t *) iterval,
(ti_dict_cb) each__walk_dict_values,
&w))
{
if (!e->nr)
ex_set_mem(e);
goto fail2;
}
break;
}
}
Expand Down
67 changes: 67 additions & 0 deletions inc/ti/fn/fnfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ static int filter__walk_i(ti_item_t * item, filter__walk_i_t * w)
return 0;
}

typedef struct
{
ex_t * e;
ti_closure_t * closure;
ti_query_t * query;
ti_dict_t * dict;
} filter__walk_dict_t;

static int filter__walk_dict_items(ti_val_t * k,
ti_val_t * v,
filter__walk_dict_t * w)
{
ti_closure_vars_dict(w->closure, k, v);
if (ti_closure_do_statement(w->closure, w->query, w->e))
return -1;

if (ti_val_as_bool(w->query->rval) && ti_dict_set(w->dict, k, &v, w->e))
return -1;

ti_val_unsafe_drop(w->query->rval);
w->query->rval = NULL;
return 0;
}

static int filter__walk_pair(ti_dict_key_t * k,
ti_val_t * v,
filter__walk_dict_t * w)
{
ti_closure_vars_dict(w->closure, (ti_val_t *) ti_nil_weak_get(), v);
if (ti_closure_do_statement(w->closure, w->query, w->e))
return -1;

if (ti_val_as_bool(w->query->rval) && ti_dict_setr(w->dict, k, v))
return -1;

ti_val_unsafe_drop(w->query->rval);
w->query->rval = NULL;
return 0;
}

static int do__f_filter(ti_query_t * query, cleri_node_t * nd, ex_t * e)
{
const char * doc;
Expand Down Expand Up @@ -198,6 +238,33 @@ static int do__f_filter(ti_query_t * query, cleri_node_t * nd, ex_t * e)
(imap_cb) filter__walk_set,
&w))
goto fail2;
break;
}
case TI_VAL_DICT:
{
filter__walk_dict_t w = {
.e = e,
.closure = closure,
.query = query,
.dict = ti_dict_create(),
};

if (!w.dict)
goto fail2;

retval = (ti_val_t *) w.dict;

if (ti_closure_arg_used(closure, 0)
? ti_dict_items(
(ti_dict_t *) iterval,
(ti_dict_item_cb) filter__walk_dict_items,
&w)
: ti_dict_pairs(
(ti_dict_t *) iterval,
(ti_dict_pair_cb) filter__walk_pair,
&w))
goto fail2;
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion inc/ti/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* "-rc0"
* ""
*/
#define TI_VERSION_PRE_RELEASE "-alpha0"
#define TI_VERSION_PRE_RELEASE "-alpha1"

#define TI_MAINTAINER \
"Jeroen van der Heijden <[email protected]>"
Expand Down
20 changes: 18 additions & 2 deletions itest/test_collection_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ async def test_extend(self, client):
await client.query('a = .list; a.extend([123]);')

async def test_filter(self, client):
await client.query(r'''
await client.query(r"""//ti
.iris = {
name: 'Iris',
age: 6,
Expand All @@ -1026,7 +1026,8 @@ async def test_filter(self, client):
age: 5,
};
.girls = set([.iris, .cato]);
''')
.people = dict(.girls.map(|v| [v.name, v]));
""")
with self.assertRaisesRegex(
LookupError,
'type `nil` has no function `filter`'):
Expand Down Expand Up @@ -1069,11 +1070,26 @@ async def test_filter(self, client):
'.girls.filter(|_v, i|(i == .cato.id()))'))[0]['age'],
5)

# pr #471
res = await client.query("""//ti
.people.filter(|k, v| v.age == 5);
""")
self.assertEqual(len(res), 1)
self.assertEqual(res[0][0], 'Cato')

# pr #471
res = await client.query("""//ti
.people.filter(|k, v| k == 'Iris');
""")
self.assertEqual(len(res), 1)
self.assertEqual(res[0][0], 'Iris')

self.assertEqual(await client.query('.iris.filter(||nil);'), {})
self.assertEqual(await client.query('.iris.likes.filter(||nil);'), [])
self.assertEqual(await client.query(r'{}.filter(||true)'), {})
self.assertEqual(await client.query(r'[].filter(||true)'), [])
self.assertEqual(await client.query(r'set().filter(||1)'), [])
self.assertEqual(await client.query(r'dict().filter(||1)'), [])

async def test_reverse(self, client):
await client.query(r'''
Expand Down
Loading