Port _matrix_json_node_deep_copy() to C

This commit is contained in:
Gergely Polonkai 2017-11-11 08:41:50 +01:00
parent 532bb0d153
commit 89376c0890
8 changed files with 87 additions and 84 deletions

1
.gitignore vendored
View File

@ -51,7 +51,6 @@ Makefile.in
/src/matrix-client.c /src/matrix-client.c
/src/matrix-http-api.c /src/matrix-http-api.c
/src/matrix-http-client.c /src/matrix-http-client.c
/src/matrix-compacts.c
/src/matrix-event-base.c /src/matrix-event-base.c
/src/matrix-event-presence.c /src/matrix-event-presence.c
/src/matrix-event-room-base.c /src/matrix-event-room-base.c

View File

@ -21,7 +21,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-client.vala \ matrix-client.vala \
matrix-http-api.vala \ matrix-http-api.vala \
matrix-http-client.vala \ matrix-http-client.vala \
matrix-compacts.vala \
matrix-event-base.vala \ matrix-event-base.vala \
matrix-event-room-base.vala \ matrix-event-room-base.vala \
matrix-event-state-base.vala \ matrix-event-state-base.vala \
@ -113,7 +112,7 @@ bin_PROGRAMS = test-api-client test-client
INST_H_SRC_FILES = \ INST_H_SRC_FILES = \
matrix-types.h \ matrix-types.h \
matrix-c-compacts.h \ matrix-compacts.h \
utils.h \ utils.h \
matrix-profile.h \ matrix-profile.h \
$(NULL) $(NULL)
@ -135,7 +134,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-event-types.c \ matrix-event-types.c \
matrix-version.c \ matrix-version.c \
matrix-types.c \ matrix-types.c \
matrix-c-compacts.c \ matrix-compacts.c \
matrix-profile.c \ matrix-profile.c \
utils.c \ utils.c \
matrix-enumtypes.c \ matrix-enumtypes.c \

View File

@ -18,7 +18,7 @@
#include <string.h> #include <string.h>
#include <gobject/gvaluecollector.h> #include <gobject/gvaluecollector.h>
#include "matrix-c-compacts.h" #include "matrix-compacts.h"
#include "matrix-enumtypes.h" #include "matrix-enumtypes.h"
#include "matrix-types.h" #include "matrix-types.h"
#include "utils.h" #include "utils.h"

View File

@ -1,68 +0,0 @@
/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
namespace Matrix {
public Json.Node?
_json_node_deep_copy(Json.Node? node)
{
Json.Node ret;
if (node == null) {
return null;
}
ret = new Json.Node(node.get_node_type());
switch (node.get_node_type()) {
case Json.NodeType.OBJECT:
var new_obj = new Json.Object();
node.get_object().foreach_member(
(old_obj, member_name, member_node) => {
new_obj.set_member(
member_name,
_json_node_deep_copy(member_node));
});
ret.set_object(new_obj);
break;
case Json.NodeType.ARRAY:
var new_ary = new Json.Array();
node.get_array().foreach_element(
(old_ary, idx, element_node) => {
new_ary.add_element(
_json_node_deep_copy(element_node));
});
break;
case Json.NodeType.VALUE:
ret.set_value(node.get_value());
break;
case Json.NodeType.NULL:
break;
}
return ret;
}
}

View File

@ -106,3 +106,71 @@ _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error)
return ret; return ret;
} }
static void
_copy_obj_member(JsonObject *old_obj, const gchar *member_name, JsonNode *member_node, gpointer user_data)
{
JsonObject *new_obj = user_data;
json_object_set_member(new_obj, member_name, _matrix_json_node_deep_copy(member_node));
}
static void
_copy_ary_member(JsonArray *old_ary, guint idx, JsonNode *element_node, gpointer user_data)
{
JsonArray *new_ary = user_data;
json_array_add_element(new_ary, _matrix_json_node_deep_copy(element_node));
}
JsonNode *
_matrix_json_node_deep_copy(JsonNode *node)
{
JsonNode *ret;
JsonNodeType node_type;
g_return_val_if_fail(node != NULL, NULL);
node_type = json_node_get_node_type(node);
ret = json_node_new(node_type);
switch (node_type) {
case JSON_NODE_OBJECT:
{
JsonObject *new_obj = json_object_new();
JsonObject *old_obj = json_node_get_object(node);
json_object_foreach_member(old_obj, _copy_obj_member, new_obj);
json_node_set_object(ret, new_obj);
break;
}
case JSON_NODE_ARRAY:
{
JsonArray *new_ary = json_array_new();
JsonArray *old_ary = json_node_get_array(node);
json_array_foreach_element(old_ary, _copy_ary_member, new_ary);
json_node_set_array(ret, new_ary);
break;
}
case JSON_NODE_VALUE:
{
GValue node_value = G_VALUE_INIT;
json_node_get_value(node, &node_value);
json_node_set_value(ret, &node_value);
g_value_unset(&node_value);
break;
}
case JSON_NODE_NULL:
break;
}
return ret;
}

View File

@ -20,11 +20,13 @@
# define __MATRIX_GLIB_SDK_UTILS_H__ # define __MATRIX_GLIB_SDK_UTILS_H__
# include <glib-object.h> # include <glib-object.h>
# include <json-glib/json-glib.h>
G_BEGIN_DECLS G_BEGIN_DECLS
gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gchar convert_dashes); gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gchar convert_dashes);
gint _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error); gint _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error);
JsonNode *_matrix_json_node_deep_copy(JsonNode *node);
G_END_DECLS G_END_DECLS

View File

@ -285,7 +285,7 @@ namespace Matrix {
} }
/* Compact classes */ /* Compact classes */
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public abstract class JsonCompact { public abstract class JsonCompact {
public JsonCompact(); public JsonCompact();
@ -296,7 +296,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class FilterRules : JsonCompact { public class FilterRules : JsonCompact {
public uint limit { get; set; } public uint limit { get; set; }
public string[] types { get; set; } public string[] types { get; set; }
@ -309,7 +309,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class RoomFilter : JsonCompact { public class RoomFilter : JsonCompact {
public bool include_leave { get; set; default=true; } public bool include_leave { get; set; default=true; }
public FilterRules? ephemeral { get; set; default = null; } public FilterRules? ephemeral { get; set; default = null; }
@ -320,7 +320,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class Filter : JsonCompact { public class Filter : JsonCompact {
public string[] event_fields { get; set; } public string[] event_fields { get; set; }
public EventFormat event_format { get; set; default = Matrix.EventFormat.CLIENT; } public EventFormat event_format { get; set; default = Matrix.EventFormat.CLIENT; }
@ -330,7 +330,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class @3PidCredential : JsonCompact { public class @3PidCredential : JsonCompact {
public string? id_server { get; set; default = null; } public string? id_server { get; set; default = null; }
public string? session_id { get; set; default = null; } public string? session_id { get; set; default = null; }
@ -340,7 +340,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class Pusher : JsonCompact { public class Pusher : JsonCompact {
public string? device_display_name { get; set; default = null; } public string? device_display_name { get; set; default = null; }
public string? app_display_name { get; set; default = null; } public string? app_display_name { get; set; default = null; }
@ -355,7 +355,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class EventContext : JsonCompact { public class EventContext : JsonCompact {
public int before_limit { get; set; default = -1; } public int before_limit { get; set; default = -1; }
public int after_limit { get; set; default = -1; } public int after_limit { get; set; default = -1; }
@ -365,7 +365,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class SearchGrouping : JsonCompact { public class SearchGrouping : JsonCompact {
public SearchGroupBy key { get; set; default = SearchGroupBy.NONE; } public SearchGroupBy key { get; set; default = SearchGroupBy.NONE; }
@ -373,7 +373,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class SearchGroupings : JsonCompact { public class SearchGroupings : JsonCompact {
public SearchGrouping[] group_by { get; set; } public SearchGrouping[] group_by { get; set; }
@ -382,7 +382,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class SearchRoomEvents : JsonCompact { public class SearchRoomEvents : JsonCompact {
public SearchOrder order_by { get; set; default = SearchOrder.RECENT; } public SearchOrder order_by { get; set; default = SearchOrder.RECENT; }
public SearchKey[] keys { get; set; } public SearchKey[] keys { get; set; }
@ -397,7 +397,7 @@ namespace Matrix {
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-c-compacts.h")] [CCode (cheader_filename = "matrix-compacts.h")]
public class SearchCategories : JsonCompact { public class SearchCategories : JsonCompact {
public SearchRoomEvents? room_events { get; set; default = null; } public SearchRoomEvents? room_events { get; set; default = null; }
@ -413,6 +413,9 @@ namespace Matrix {
public int _g_enum_nick_to_value(GLib.Type enum_type, string nick) public int _g_enum_nick_to_value(GLib.Type enum_type, string nick)
throws Matrix.Error; throws Matrix.Error;
[CCode (cheader_filename = "utils.h", cname = "_matrix_json_node_deep_copy")]
public Json.Node? _json_node_deep_copy(Json.Node? node);
[CCode (cheader_filename = "matrix-profile.h")] [CCode (cheader_filename = "matrix-profile.h")]
public class Profile : GLib.Object { public class Profile : GLib.Object {
public GLib.TimeSpan age {get;} public GLib.TimeSpan age {get;}