Entity: Improve spec patching code

This commit is contained in:
Bleak Grey 2020-10-31 16:11:12 +03:00
parent b23bd09206
commit cc9404aabc
1 changed files with 16 additions and 27 deletions

View File

@ -2,29 +2,31 @@ using Json;
public class Tootle.Entity : GLib.Object, Widgetizable, Json.Serializable { public class Tootle.Entity : GLib.Object, Widgetizable, Json.Serializable {
public static string[] ignore_props = {"formal", "handle", "domain", "has-spoiler"};
public virtual bool is_local (InstanceAccount account) { public virtual bool is_local (InstanceAccount account) {
return true; return true;
} }
public new ParamSpec[] list_properties () { static bool is_spec_valid (ref ParamSpec spec) {
ParamSpec[] specs = {}; return ParamFlags.WRITABLE in spec.flags;
foreach (ParamSpec spec in get_class ().list_properties ()) { }
if (!(spec.name in ignore_props))
specs += spec; public override unowned ParamSpec? find_property (string name) {
switch (name) {
case "type":
return get_class ().find_property ("kind");
case "value":
return get_class ().find_property ("val");
default:
return get_class ().find_property (name);
} }
return specs;
} }
public void patch (GLib.Object with) { public void patch (GLib.Object with) {
var props = with.get_class ().list_properties (); foreach (ParamSpec spec in with.get_class ().list_properties ()) {
foreach (var prop in props) { var name = spec.get_name ();
var name = prop.get_name ();
var defined = get_class ().find_property (name) != null; var defined = get_class ().find_property (name) != null;
var forbidden = name in ignore_props; if (defined && is_spec_valid (ref spec)) {
if (defined && !forbidden) { var val = Value (spec.value_type);
var val = Value (prop.value_type);
with.get_property (name, ref val); with.get_property (name, ref val);
base.set_property (name, val); base.set_property (name, val);
} }
@ -39,19 +41,6 @@ public class Tootle.Entity : GLib.Object, Widgetizable, Json.Serializable {
if (obj == null) if (obj == null)
throw new Oopsie.PARSING (@"Received Json.Node for $(type.name ()) is not a Json.Object!"); throw new Oopsie.PARSING (@"Received Json.Node for $(type.name ()) is not a Json.Object!");
//Replace with something more elegant
var kind = obj.get_member ("type");
if (kind != null) {
obj.set_member ("kind", kind);
obj.remove_member ("type");
}
var val = obj.get_member ("value");
if (val != null) {
obj.set_member ("val", val);
obj.remove_member ("value");
}
return Json.gobject_deserialize (type, node) as Entity; return Json.gobject_deserialize (type, node) as Entity;
} }