Skip to content

Commit

Permalink
emit support for room name
Browse files Browse the repository at this point in the history
  • Loading branch information
joente committed Dec 5, 2024
1 parent 0684a46 commit dd2df41
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
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 "-alpha2"
#define TI_VERSION_PRE_RELEASE "-alpha3"

#define TI_MAINTAINER \
"Jeroen van der Heijden <[email protected]>"
Expand Down
26 changes: 26 additions & 0 deletions itest/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from thingsdb.exceptions import NumArgumentsError
from thingsdb.exceptions import LookupError
from thingsdb.exceptions import OperationError
from thingsdb.exceptions import BadDataError
from thingsdb.room import Room, event
from thingsdb.client import Client


class ORoom(Room):
Expand Down Expand Up @@ -268,6 +270,30 @@ async def test_room_name(self, cl0, cl1, cl2):
self.assertTrue(isinstance(res[0], int))
self.assertIs(res[1], None)

res = await cl0._emit("a", "Test")
self.assertIs(res, None)

with self.assertRaisesRegex(
BadDataError,
'emit request only accepts an integer '
'room id or string room name;'):
await cl0._emit(3.4, "Test")

with self.assertRaisesRegex(
ValueError,
'room name must follow the naming rules;'):
await cl0._emit("", "Test")

with self.assertRaisesRegex(
LookupError,
'collection `stuff` has no `room` with name `not_here`'):
await cl0._emit("not_here", "Test")

with self.assertRaisesRegex(
LookupError,
'collection `stuff` has no `room` with id 12345'):
await cl0._emit(12345, "Test")

await cl0.query('.room_a.set_name("A");')

res = await cl0.query('[.room_a.name(), .room_b.name()];')
Expand Down
57 changes: 50 additions & 7 deletions src/ti/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ int ti_room_emit_from_pkg(
mp_skip(&up); /* scope */

if (obj.via.sz < 3 ||
mp_next(&up, &mp_id) != MP_U64 ||
mp_next(&up, &mp_id) <= 0 ||
mp_next(&up, &mp_event) != MP_STR)
{
ex_set(e, EX_BAD_DATA, "invalid emit request");
Expand All @@ -277,7 +277,22 @@ int ti_room_emit_from_pkg(

uv_mutex_lock(collection->lock);

room = ti_collection_room_by_id(collection, mp_id.via.u64);
if (mp_id.tp == MP_STR)
{
room = ti_collection_room_by_strn(
collection,
mp_id.via.str.data,
mp_id.via.str.n);
}
else if (mp_cast_u64(&mp_id) == 0)
{
room = ti_collection_room_by_id(collection, mp_id.via.u64);
}
else
{
room = NULL;
}

if (room)
{
while (nargs--)
Expand All @@ -290,11 +305,39 @@ int ti_room_emit_from_pkg(
}
}
else
ex_set(e, EX_LOOKUP_ERROR,
"collection `%.*s` has no `room` with id %"PRIu64,
collection->name->n,
(char *) collection->name->data,
mp_id.via.u64);
{
if (mp_id.tp == MP_STR)
{
if (ti_name_is_valid_strn(mp_id.via.str.data, mp_id.via.str.n))
{
ex_set(e, EX_LOOKUP_ERROR,
"collection `%.*s` has no `room` with name `%.*s`",
collection->name->n,
(char *) collection->name->data,
mp_id.via.str.n,
mp_id.via.str.data);
}
else
{
ex_set(e, EX_VALUE_ERROR,
"room name must follow the naming rules"DOC_NAMES);
}
}
else if (mp_id.tp == MP_U64)
{
ex_set(e, EX_LOOKUP_ERROR,
"collection `%.*s` has no `room` with id %"PRIu64,
collection->name->n,
(char *) collection->name->data,
mp_id.via.u64);
}
else
{
ex_set(e, EX_BAD_DATA,
"emit request only accepts an integer room id "
"or string room name"DOC_LISTENING);
}
}

uv_mutex_unlock(collection->lock);

Expand Down

0 comments on commit dd2df41

Please sign in to comment.