Skip to content

Commit

Permalink
Merge pull request #114 from alltilla/filterx-otel-dict
Browse files Browse the repository at this point in the history
filterx: otel dict
  • Loading branch information
alltilla authored May 21, 2024
2 parents e492095 + 7808171 commit edfed2c
Show file tree
Hide file tree
Showing 19 changed files with 1,042 additions and 166 deletions.
21 changes: 21 additions & 0 deletions lib/filterx/object-dict-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ filterx_dict_iter(FilterXObject *s, FilterXDictIterFunc func, gpointer user_data
return self->iter(self, func, user_data);
}

static gboolean
_add_elem_to_dict(FilterXObject *key_obj, FilterXObject *value_obj, gpointer user_data)
{
FilterXObject *dict = (FilterXObject *) user_data;

FilterXObject *new_value = filterx_object_ref(value_obj);
gboolean success = filterx_object_set_subscript(dict, key_obj, &new_value);
filterx_object_unref(new_value);

return success;
}

gboolean
filterx_dict_merge(FilterXObject *s, FilterXObject *other)
{
FilterXDict *self = (FilterXDict *) s;

g_assert(filterx_object_is_type(other, &FILTERX_TYPE_NAME(dict)));
return filterx_dict_iter(other, _add_elem_to_dict, self);
}

static gboolean
_len(FilterXObject *s, guint64 *len)
{
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/object-dict-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct FilterXDict_
};

gboolean filterx_dict_iter(FilterXObject *s, FilterXDictIterFunc func, gpointer user_data);
gboolean filterx_dict_merge(FilterXObject *s, FilterXObject *other);

void filterx_dict_init_instance(FilterXDict *self, FilterXType *type);

Expand Down
24 changes: 24 additions & 0 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "filterx/object-null.h"
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-list-interface.h"
#include "filterx/object-message-value.h"
#include "filterx/filterx-weakrefs.h"
#include "filterx/filterx-eval.h"
Expand Down Expand Up @@ -167,6 +169,28 @@ filterx_json_new_from_args(GPtrArray *args)
filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_object)))
return filterx_object_ref(arg);

if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(dict)))
{
FilterXObject *self = filterx_json_object_new_empty();
if (!filterx_dict_merge(self, arg))
{
filterx_object_unref(self);
return NULL;
}
return self;
}

if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(list)))
{
FilterXObject *self = filterx_json_array_new_empty();
if (!filterx_list_merge(self, arg))
{
filterx_object_unref(self);
return NULL;
}
return self;
}

if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(message_value)))
{
FilterXObject *unmarshalled = filterx_object_unmarshal(arg);
Expand Down
22 changes: 22 additions & 0 deletions lib/filterx/object-list-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ filterx_list_unset_index(FilterXObject *s, gint64 index)
return result;
}

gboolean
filterx_list_merge(FilterXObject *s, FilterXObject *other)
{
g_assert(filterx_object_is_type(other, &FILTERX_TYPE_NAME(list)));

guint64 len;
g_assert(filterx_object_len(other, &len));

for (guint64 i = 0; i < len; i++)
{
FilterXObject *value_obj = filterx_list_get_subscript(other, (gint64) MIN(i, G_MAXINT64));
gboolean success = filterx_list_append(s, &value_obj);

filterx_object_unref(value_obj);

if (!success)
return FALSE;
}

return TRUE;
}

static gboolean
_len(FilterXObject *s, guint64 *len)
{
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/object-list-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ FilterXObject *filterx_list_get_subscript(FilterXObject *s, gint64 index);
gboolean filterx_list_set_subscript(FilterXObject *s, gint64 index, FilterXObject **new_value);
gboolean filterx_list_append(FilterXObject *s, FilterXObject **new_value);
gboolean filterx_list_unset_index(FilterXObject *s, gint64 index);
gboolean filterx_list_merge(FilterXObject *s, FilterXObject *other);

void filterx_list_init_instance(FilterXList *self, FilterXType *type);

Expand Down
22 changes: 19 additions & 3 deletions modules/grpc/otel/filterx/object-otel-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,27 @@ filterx_otel_array_new_from_args(GPtrArray *args)
try
{
if (!args || args->len == 0)
self->cpp = new Array(self);
{
self->cpp = new Array(self);
}
else if (args->len == 1)
self->cpp = new Array(self, (FilterXObject *) g_ptr_array_index(args, 0));
{
FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(list)))
{
self->cpp = new Array(self);
if (!filterx_list_merge(&self->super.super, arg))
throw std::runtime_error("Failed to merge list");
}
else
{
self->cpp = new Array(self, arg);
}
}
else
throw std::runtime_error("Invalid number of arguments");
{
throw std::runtime_error("Invalid number of arguments");
}
}
catch (const std::runtime_error &e)
{
Expand Down
22 changes: 19 additions & 3 deletions modules/grpc/otel/filterx/object-otel-kvlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,27 @@ filterx_otel_kvlist_new_from_args(GPtrArray *args)
try
{
if (!args || args->len == 0)
self->cpp = new KVList(self);
{
self->cpp = new KVList(self);
}
else if (args->len == 1)
self->cpp = new KVList(self, (FilterXObject *) g_ptr_array_index(args, 0));
{
FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(dict)))
{
self->cpp = new KVList(self);
if (!filterx_dict_merge(&self->super.super, arg))
throw std::runtime_error("Failed to merge dict");
}
else
{
self->cpp = new KVList(self, arg);
}
}
else
throw std::runtime_error("Invalid number of arguments");
{
throw std::runtime_error("Invalid number of arguments");
}
}
catch (const std::runtime_error &e)
{
Expand Down
Loading

0 comments on commit edfed2c

Please sign in to comment.