From ddf2165cc22fb0bdbffce97d2b83eafdb7df5f8d Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 9 Mar 2016 17:34:39 +0100 Subject: [PATCH] Patch up Matrix.Event.Base * Require event_type to be present --- src/matrix-event-base.vala | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/matrix-event-base.vala b/src/matrix-event-base.vala index c62abaa..e707dc8 100644 --- a/src/matrix-event-base.vala +++ b/src/matrix-event-base.vala @@ -144,7 +144,16 @@ public abstract class Matrix.Event.Base : GLib.Object, GLib.Initable { public virtual void from_json(Json.Node json_data) throws Matrix.Error - {} + { + var root = json_data.get_object(); + Json.Node? node; + + if ((node = root.get_member("type")) != null) { + _event_type = node.get_string(); + } else if (Config.DEBUG) { + warning("type is not present in an event"); + } + } /** * Subclasses should implement this to export their data to JSON. @@ -153,11 +162,14 @@ public abstract class Matrix.Event.Base : GLib.Object, GLib.Initable { to_json(Json.Node json_data) throws Matrix.Error { - Json.Object root = json_data.get_object(); + var root = json_data.get_object(); - if (event_type != null) { - root.set_string_member("type", event_type); + if (_event_type == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate an event without type"); } + + root.set_string_member("type", _event_type); } public static Base?