예제 #1
0
        public void TestNoRows()
        {
            var grid = new HaystackGrid();

            grid.Meta.Add("dis", new HaystackString("Title"));
            grid.AddColumn("a", col => col.Meta.Add("dis", new HaystackString("Alpha")));
            grid.AddColumn("b");

            //.Meta
            Assert.AreEqual(grid.Meta.Count, 1);
            Assert.IsTrue(grid.Meta["dis"].Equals(new HaystackString("Title")));

            // cols
            Assert.AreEqual(grid.ColumnCount, 2);
            var c = VerifyCol(grid, 0, "a");

            Assert.AreEqual(c.Display, "Alpha");
            Assert.AreEqual(c.Meta.Count, 1);
            Assert.IsTrue(c.Meta["dis"].Equals(new HaystackString("Alpha")));

            // rows
            Assert.AreEqual(grid.RowCount, 0);
            Assert.AreEqual(grid.IsEmpty(), true);

            // iterator
            VerifyGridIterator(grid);
        }
예제 #2
0
        public void TestSimple()
        {
            var grid = new HaystackGrid();

            grid.AddColumn("id");
            grid.AddColumn("dis");
            grid.AddColumn("area");
            grid.AddRow(new HaystackReference("a"), new HaystackString("Alpha"), new HaystackNumber(1200));
            grid.AddRow(new HaystackReference("b"), null, new HaystackNumber(1400));

            //.Meta
            Assert.AreEqual(grid.Meta.Count, 0);

            // cols
            //HCol c;
            Assert.AreEqual(grid.ColumnCount, 3);
            VerifyCol(grid, 0, "id");
            VerifyCol(grid, 1, "dis");
            VerifyCol(grid, 2, "area");

            // rows
            Assert.AreEqual(grid.RowCount, 2);
            Assert.IsFalse(grid.IsEmpty());
            var row = grid.Row(0);

            Assert.IsTrue(row.Get("id").Equals(new HaystackReference("a")));
            Assert.IsTrue(row.Get("dis").Equals(new HaystackString("Alpha")));
            Assert.IsTrue(row.Get("area").Equals(new HaystackNumber(1200)));
            row = grid.Row(1);
            Assert.IsTrue(row.Get("id").Equals(new HaystackReference("b")));
            Assert.IsFalse(row.ContainsKey("dis"));
            Assert.IsTrue(row.Get("area").Equals(new HaystackNumber(1400)));
            Assert.ThrowsException <HaystackUnknownNameException>(() => row["dis"]);
            Assert.IsFalse(row.ContainsKey("fooBar"));
            Assert.ThrowsException <HaystackUnknownNameException>(() => row["fooBar"]);

            // HaystackRow no-nulls
            HaystackRow it = grid.Row(0);

            Assert.IsFalse(it.Count > 3);
            VerifyRowIterator(it, 0, "id", new HaystackReference("a"));
            VerifyRowIterator(it, 1, "dis", new HaystackString("Alpha"));
            VerifyRowIterator(it, 2, "area", new HaystackNumber(1200));


            // HaystackRow with nulls
            it = grid.Row(1);
            Assert.IsFalse(it.Count > 3);
            VerifyRowIterator(it, 0, "id", new HaystackReference("b"));
            VerifyRowIterator(it, 2, "area", new HaystackNumber(1400));

            // iterating
            VerifyGridIterator(grid);
        }
        public static IEnumerable <HaystackRow> BuildRows(ICollection <string> cols, params HaystackValue[][] rows)
        {
            var grid = new HaystackGrid();

            foreach (var col in cols)
            {
                grid.AddColumn(col);
            }
            foreach (var row in rows)
            {
                grid.AddRow(row);
            }
            return(grid.Rows);
        }
예제 #4
0
        /// <summary>
        /// Read all records with a given filter.
        /// </summary>
        /// <param name="filter">Record filter.</param>
        /// <param name="limit">Maximum number of results to request.</param>
        /// <returns>Grid with records.</returns>
        public Task <HaystackGrid> ReadAllAsync(string filter, int limit)
        {
            var req = new HaystackGrid()
                      .AddColumn("filter");

            if (limit > 0)
            {
                req.AddColumn("limit");
                req.AddRow(new HaystackString(filter), new HaystackNumber(limit));
            }
            else
            {
                req.AddRow(new HaystackString(filter));
            }
            return(CallAsync("read", req));
        }
예제 #5
0
        public HaystackGrid ToGrid()
        {
            // Gather entities and unique keys.
            var entities = ReadEntities().ToArray();
            var keys     = entities.SelectMany(d => d.Keys).Distinct().ToArray();

            // Build the grid.
            var grid = new HaystackGrid();

            foreach (var key in keys)
            {
                grid.AddColumn(key);
            }
            foreach (var entity in entities)
            {
                grid.AddRow(keys.Select(key => entity.ContainsKey(key) ? entity[key] : null).ToArray());
            }
            return(grid);
        }
        private HaystackGrid ReadGrid()
        {
            bool nested = _currentToken == HaystackToken.lt2;

            if (nested)
            {
                Consume(HaystackToken.lt2);
                if (_currentToken == HaystackToken.nl)
                {
                    Consume(HaystackToken.nl);
                }
            }

            bool bValisVer = false;

            if (_currentValue is string)
            {
                if (((string)_currentValue).CompareTo("ver") == 0)
                {
                    bValisVer = true;
                }
            }
            // ver:"3.0"
            if (_currentToken != HaystackToken.id || !bValisVer)
            {
                throw GetException("Expecting grid 'ver' identifier, not " + curToStr());
            }
            Consume();
            Consume(HaystackToken.colon);
            _version = CheckVersion(ConsumeStr());

            // grid meta
            var grid = new HaystackGrid();

            if (_currentToken == HaystackToken.id)
            {
                var dict = ReadDictionary();
                foreach (var kv in dict)
                {
                    grid.AddMeta(kv.Key, kv.Value);
                }
            }
            Consume(HaystackToken.nl);

            // column definitions
            int numCols = 0;

            while (_currentToken == HaystackToken.id)
            {
                ++numCols;
                string name    = ConsumeTagName();
                var    colMeta = new HaystackDictionary();
                if (_currentToken == HaystackToken.id)
                {
                    colMeta = ReadDictionary();
                }
                grid.AddColumn(name, colMeta);
                if (_currentToken != HaystackToken.comma)
                {
                    break;
                }
                Consume(HaystackToken.comma);
            }
            if (numCols == 0)
            {
                throw GetException("No columns defined");
            }
            Consume(HaystackToken.nl);

            // grid rows
            while (true)
            {
                if (_currentToken == HaystackToken.nl)
                {
                    break;
                }
                if (_currentToken == HaystackToken.eof)
                {
                    break;
                }
                if (nested && _currentToken == HaystackToken.gt2)
                {
                    break;
                }

                // read cells
                HaystackValue[] cells = new HaystackValue[numCols];
                for (int i = 0; i < numCols; ++i)
                {
                    if (_currentToken == HaystackToken.comma || _currentToken == HaystackToken.nl || _currentToken == HaystackToken.eof)
                    {
                        cells[i] = null;
                    }
                    else
                    {
                        cells[i] = ParseValue();
                    }
                    if (i + 1 < numCols)
                    {
                        Consume(HaystackToken.comma);
                    }
                }
                grid.AddRow(cells);

                // newline or end
                if (nested && _currentToken == HaystackToken.gt2)
                {
                    break;
                }
                if (_currentToken == HaystackToken.eof)
                {
                    break;
                }
                Consume(HaystackToken.nl);
            }

            if (_currentToken == HaystackToken.nl)
            {
                Consume(HaystackToken.nl);
            }
            if (nested)
            {
                Consume(HaystackToken.gt2);
            }
            return(grid);
        }