Browse Source

Make it build after lots of API changes to Invercargill

Billy Barrow 1 month ago
parent
commit
a3a9ade2a9
2 changed files with 43 additions and 20 deletions
  1. 17 11
      src/lib/Array.vala
  2. 26 9
      src/lib/Object.vala

+ 17 - 11
src/lib/Array.vala

@@ -2,7 +2,7 @@ using Invercargill;
 
 namespace InvercargillJson {
 
-    public class JsonArray : IndexedCollection<JsonElement>, Elements, JsonElements {
+    public class JsonArray : Enumerable<JsonElement>, ReadOnlyCollection<JsonElement>, ReadOnlyAddressable<JsonElement>, Collection<JsonElement>, Addressable<JsonElement>, Elements, JsonElements {
 
         private Json.Array array;
 
@@ -16,9 +16,9 @@ namespace InvercargillJson {
             this.array = array;
         }
 
-        public override Tracker<Element> get_tracker() {
+        public override Tracker<JsonElement> get_tracker() {
             var i = 0;
-            return new LambdaTracker<Element>(
+            return new LambdaTracker<JsonElement>(
                 () => {
                     return i < array.get_length();
                 },
@@ -27,32 +27,38 @@ namespace InvercargillJson {
                 });
         }
 
-        public new override JsonElement @get(int index) {
+        public new JsonElement @get(uint index) {
             return new JsonElement.from_node(array.get_element(index));
         }
-        public override int index_of(PredicateDelegate<Element> predicate) {
+        public uint? first_index_of(PredicateDelegate<Element> predicate) {
             return with_positions().first_or_default(i => predicate(i.item))?.position ?? -1;
         }
-        public override void remove(int index) {
+        public void remove_at(int index) {
             array.remove_element(index);
         }
-        public new override void @set(int index, JsonElement item) {
+        public new void @set(int index, JsonElement item) {
             assert_not_reached();
         }
-        public override void add(JsonElement item) {
+        public void add(JsonElement item) {
             array.add_element(item.assert_as<Json.Node>());
         }
-        public override void remove_first_where(Invercargill.PredicateDelegate<JsonElement> predicate) {
-            var index = index_of(predicate);
+        public void remove_first_where(Invercargill.PredicateDelegate<JsonElement> predicate) {
+            var index = first_index_of(predicate);
             if(index >= 0) {
                 array.remove_element(index);
             }
         }
-        public override void remove_where(Invercargill.PredicateDelegate<JsonElement> predicate) {
+        public void remove_all_where(Invercargill.PredicateDelegate<JsonElement> predicate) {
             with_positions()
                 .where(i => predicate((JsonElement)i.item))
                 .iterate(i => array.remove_element(i.position));
         }
+        public void clear() {
+            remove_all_where(i => true);
+        }
+        public bool try_get(uint index, out JsonElement value) {
+                assert_not_reached();
+        }
     }
 
 }

+ 26 - 9
src/lib/Object.vala

@@ -2,7 +2,7 @@ using Invercargill;
 
 namespace InvercargillJson {
 
-    public class JsonObject : Associative<string, JsonElement>, Properties {
+    public class JsonObject : Enumerable<KeyValuePair<string, JsonElement>>, ReadOnlyCollection<KeyValuePair<string, JsonElement>>, ReadOnlyAssociative<string, JsonElement>, Associative<string, JsonElement>, Properties {
 
         private Json.Object object;
 
@@ -15,16 +15,16 @@ namespace InvercargillJson {
         internal JsonObject.from_existing(Json.Object object) {
             this.object = object;
         }
-        public override void clear (string key) {
+        public void clear () {
             object.foreach_member ((o, n) => o.remove_member (n));
         }
-        public override bool has (string key) {
+        public bool has (string key) {
             return object.has_member(key);
         }
-        public new override void @set (string key, JsonElement value) {
+        public new void @set (string key, JsonElement value) {
             object.set_member(key, value.assert_as<Json.Node>());
         }
-        public override bool try_get (string key, out JsonElement value) {
+        public bool try_get (string key, out JsonElement value) {
             if(has(key)) {
                 value = new JsonElement.from_node(object.get_member(key));
                 return true;
@@ -32,15 +32,15 @@ namespace InvercargillJson {
             value = null;
             return false;
         }
-        public override Tracker<KeyValuePair<string, Element>> get_tracker () {
+        public override Tracker<KeyValuePair<string, JsonElement>> get_tracker () {
             var keys = object.get_members ();
             var i = 0;
-            return new LambdaTracker<KeyValuePair<string, Element>>(
+            return new LambdaTracker<KeyValuePair<string, JsonElement>>(
                 () => {
                     return i < keys.length();
                 },
                 () => {
-                    return new KeyValuePair<string, Element> (keys.nth_data(i), new JsonElement.from_node(object.get_member(keys.nth_data(i++))));
+                    return new KeyValuePair<string, JsonElement> (keys.nth_data(i), new JsonElement.from_node(object.get_member(keys.nth_data(i++))));
                 });
         }
 
@@ -76,7 +76,7 @@ namespace InvercargillJson {
             return null;
         }
 
-        public void set_native<T>(string key, T value) throws ElementError {
+        public void set_native<T>(string key, T value, bool? defined = false) throws ElementError {
             set(key, new JsonElement.from_element(new NativeElement<T>(value)));
         }
 
@@ -86,6 +86,23 @@ namespace InvercargillJson {
             return new JsonElement.from_node(node);
         }
 
+        public bool add(string key, JsonElement value) {
+            if(has(key)) {
+                return false;
+            }
+            @set(key, value);
+            return true;
+        }
+        public bool remove(string key, out JsonElement value) {
+            if(try_get(key, out value)) {
+                object.remove_member(key);
+                return true;
+            }
+            return false;
+        }
+        public Invercargill.Enumerable<string> keys { owned get; }
+        public Invercargill.Enumerable<JsonElement> values { owned get; }
+
     }
 
 }