Ejemplo n.º 1
0
 public static Array MergeArray(this Array a, Array b)
 {
     return(new Array()
     {
         Items = a.Items.Concat(b.Items).Distinct().ToList()
     });
 }
Ejemplo n.º 2
0
 public void CopyTo(T[] array, int arrayIndex)
 {
     _children.Items.Where(p => p.IsOf <T>()).Select(p =>
     {
         p.TryGet(out T data);
         return(data);
     }).ToArray().CopyTo(array, arrayIndex);
 }
Ejemplo n.º 3
0
 public void CopyTo(KeyValuePair <K, V>[] array, int arrayIndex)
 {
     for (int i = 0; i < _children.Items.Count; i++)
     {
         _children.Items[i].Key.TryGet(out K k);
         _children.Items[i].Value.TryGet(out V v);
         var kv = new KeyValuePair <K, V>(k, v);
         array[arrayIndex + i] = kv;
     }
 }
Ejemplo n.º 4
0
        //
        // public static NodeID ToNodeID(this string str)
        // {
        //     return new NodeID{Iri = str};
        // }

        //
        public static Array ToArray(this AHGHEEParser.ArrContext arr)
        {
            var inputData = arr.value();
            var items     = new Primitive[inputData.Length];

            for (int i = 0; i < inputData.Length; i++)
            {
                items[i] = inputData[i].ToData();
            }

            var vec = new Array()
            {
                Items = items
            };

            return(vec);
        }
Ejemplo n.º 5
0
        public void Init(Node n, IDatabaseSession db)
        {
            _n = n;
            n.Attributes ??= new Map
            {
                Items = new List <KeyValue>()
            };
            // Main Type
            var currentType = n.Attributes.AsEnumerable().FirstOrDefault(a => a.Key.Equals(Attr.Type));

            if (currentType != null)
            {
                var hasType = currentType.Value.TryGet(out NodeID type) && type != null;
                switch (hasType)
                {
                case true when type.Equals(DataType):
                    // all good.
                    break;

                case true:
                    throw new Exception($"Cannot initialize type {typeof(SimpleList<T>)} from a node with {Attr.Type} of {type}");

                default:
                    throw new Exception($"Cannot initialize type {typeof(SimpleList<T>)} from a node with {Attr.Type} and value {currentType.Value.ToDisplayString()}");
                }
            }
            else
            {
                _n.Attributes.Items.Add(Utils.Prop(DateTime.UtcNow.Ticks, Attr.Type, DataType));
            }

            // Type parameter
            var typeParam = n.Attributes.AsEnumerable().FirstOrDefault(a => a.Key.Equals(Attr.GenericTypeParameters));

            if (currentType == null)
            {
                // lets set it
                n.Attributes.Items.Add(Utils.Prop(DateTime.UtcNow.Ticks, Attr.GenericTypeParameters, new Array {
                    Items = new Primitive[] { new T().ToPrimitive().Discriminator }
                }));
            }
            else
            {
                //check it
                if (typeParam.Value.TryGet(out Array data))
                {
                    if (data.Items.Count == 1 && data.Items[0].I8.v == default(T).ToPrimitive().Discriminator)
                    {
                        // yay we match!!
                    }
                    else
                    {
                        throw new Exception(
                                  $"Cannot initialize type {typeof(SimpleList<T>)} as node {Attr.GenericTypeParameters} has a generic type of {Enum.GetName(typeof(Primitive.ItemKind), data.Items[0].I8.v)}");
                    }
                }
                else
                {
                    throw new Exception(
                              $"Cannot initialize type {typeof(SimpleList<T>)} as node {Attr.GenericTypeParameters} has a type value of {Enum.GetName(typeof(Primitive.ItemKind), typeParam.Value.Discriminator)}");
                }
            }

            // Children
            var childrenKV = _n.Attributes.Items.FirstOrDefault(kv =>
                                                                kv.Key.Equals(Attr.Children));

            if (childrenKV != null)
            {
                var children = childrenKV.Value;
                if (children.Kind == Primitive.ItemKind.Array)
                {
                    _children = new Array
                    {
                        Items = children.Array.Items.ToList()
                    };
                }
                else
                {
                    throw new Exception(
                              $"Cannot initialize type {typeof(SimpleList<T>)} from a node with {Attr.Children} value type of {Enum.GetName(typeof(Primitive.ItemKind), children.Kind)}");
                }
            }
            else
            {
                // missing, so we can create it.
                _children = new Array
                {
                    Items = new List <Primitive>()
                };
                _n.Attributes.Items.Add(Utils.Prop(DateTime.UtcNow.Ticks, Attr.Children, _children));
            }
        }