Bläddra i källkod

Add unwrap functions to AttemptEnumerable

Billy Barrow 6 månader sedan
förälder
incheckning
eb8fd942fd
2 ändrade filer med 96 tillägg och 1 borttagningar
  1. 91 0
      src/lib/Concrete/AttemptEnumerable.vala
  2. 5 1
      src/lib/PropertyMapper.vala

+ 91 - 0
src/lib/Concrete/AttemptEnumerable.vala

@@ -19,6 +19,97 @@ namespace Invercargill {
             return all(a => a.success);
         }
 
+        public bool unwrap_iterate_if(PredicateDelegate<T> handler) throws Error {
+            var tracker = get_tracker();
+            while(tracker.has_next()) {
+                var item = tracker.get_next();
+                if(item.success) {
+                    if(!handler(item.result)) {
+                        return false;
+                    }
+                }
+                else {
+                    throw item.error;
+                }
+            }
+            return true;
+        }
+
+        public void unwrap_iterate(ItemDelegate<T> handler) throws Error {
+            unwrap_iterate_if(i => {
+                handler(i);
+                return true;
+            });
+        }
+
+        public Series<T> unwrap_to_series() throws Error {
+            var series = new Series<T>();
+            unwrap_iterate(i => series.add(i));
+            return series;
+        }
+
+        public Gee.Collection<T> unwrap_to_gee_collection() throws Error {
+            var collection = new Gee.LinkedList<T>();
+            unwrap_iterate(i => collection.add(i));
+            return collection;
+        }
+
+        public virtual T[] unwrap_to_array() throws Error {
+            var array = new T[1024];
+            var index = 0;
+            foreach (var item in this) {
+                if(!item.success) {
+                    throw item.error;
+                }
+                if(index >= array.length) {
+                    array.resize(array.length*2);
+                }
+                safely_assign_to_array<T>(array, index, item.result);
+                index++;
+            }
+            array.resize(index);
+            return array;
+        }
+
+        public Vector<T> unwrap_to_vector() throws Error {
+            var vector = new Vector<T>();
+            unwrap_iterate(i => vector.add(i));
+            return vector;
+        }
+
+        public Dictionary<TKey, T> unwrap_to_dictionary<TKey>(TransformDelegate<T, TKey> key_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
+            var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);       
+            unwrap_iterate(i => dict.set(key_selecter(i), i));
+            return dict;
+        }
+
+        public virtual Dictionary<TKey, TValue> unwrap_select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashFunc<TKey>? key_hash_func = null, EqualFunc<TKey>? key_equal_func = null) throws Error {
+            var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);       
+            unwrap_iterate(i => dict.set(key_selecter(i), value_selecter(i)));
+            return dict;
+        }
+
+        public Set<T> unwrap_to_set(HashFunc<T>? hash_func = null, EqualFunc<T>? equal_func = null) throws Error {
+            var @set = new Set<T>(hash_func, equal_func);       
+            unwrap_iterate(i => @set.add(i));
+            return @set;
+        }
+
+        public virtual Elements unwrap_to_elements() throws Error {
+            var series = new ElementSeries();
+            if(typeof(T).is_a(typeof(Element))) {
+                unwrap_iterate(i => series.add((Element)i));
+            }
+            else {
+                unwrap_iterate(i => series.add(new NativeElement<T>(i)));
+            }
+            return series;
+        }
+
+        public virtual Object[] unwrap_to_object_array() throws Error {
+            return unwrap_to_vector().to_object_array();
+        }
+
     }
 
 }

+ 5 - 1
src/lib/PropertyMapper.vala

@@ -26,9 +26,13 @@ namespace Invercargill {
 
         public void map_into(T object, KeyValues<string, Element> properties) throws Error {
             foreach (var mapping in mappings) {
-                if(!mapping.mandatory && !properties.has(mapping.name)) {
+                var exists = properties.has(mapping.name);
+                if(!mapping.mandatory && !exists) {
                     continue;
                 }
+                if(mapping.mandatory && !exists){
+                    throw new IndexError.KEY_NOT_FOUND(@"Failed to map into $(typeof(T).name()): Mandatory property \"$(mapping.name)\" was not present in the properties list");
+                }
                 mapping.setter(object, properties[mapping.name]);
             }
         }