void VerifyGrid(string str, HaystackDictionary meta, object[] cols, HaystackValue[][] rows) { // normalize nulls if (meta == null) { meta = new HaystackDictionary(); } for (int i = 0; i < cols.Length; ++i) { if (cols[i] == null) { cols[i] = new HaystackDictionary(); } } // read from zinc var grid = new ZincReader(str).ReadValue <HaystackGrid>(); VerifyGridEquals(grid, meta, cols, rows); // write grid and verify we can parse that too string writeStr = ZincWriter.ToZinc(grid); var writeGrid = new ZincReader(writeStr).ReadValue <HaystackGrid>(); VerifyGridEquals(writeGrid, meta, cols, rows); }
public HttpClientMockBuilder WithReadAsync(string expectedFilter, HaystackGrid response) { _requestHandlers.Add(async request => { var relativeUri = _baseUri.MakeRelativeUri(request.RequestUri); if (relativeUri.OriginalString != "read") { return(null); } var reader = new ZincReader(await request.Content.ReadAsStringAsync()); var grid = reader.ReadValue <HaystackGrid>(); var filter = grid.Rows.First().Get <HaystackString>("filter").Value; if (filter != expectedFilter) { return(null); } using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) { var writer = new ZincWriter(streamWriter); writer.WriteValue(response); streamWriter.Flush(); stream.Position = 0; return(new HttpResponseMessage { Content = new StringContent(await new StreamReader(stream).ReadToEndAsync()), }); } }); return(this); }
/// <summary> /// Execute a POST request and parse the raw string result into a grid. /// </summary> /// <param name="op">Operation to execute.</param> /// <param name="req">Properly formatted request string</param> /// <returns>Grid of results.</returns> private async Task <HaystackGrid> PostGridAsync(string op, HaystackGrid req) { string reqStr = ZincWriter.ToZinc(req); string resStr = await PostStringAsync(op, reqStr, "text/zinc"); return(new ZincReader(resStr).ReadValue <HaystackGrid>()); }
private void VerifyCoord(decimal latitude, decimal longitude, string s) { var coordinate = new HaystackCoordinate(latitude, longitude); Assert.AreEqual(latitude, coordinate.Latitude); Assert.AreEqual(longitude, coordinate.Longitude); Assert.AreEqual(s, ZincWriter.ToZinc(coordinate)); }
/// <summary> /// Read history time-series data for a given record and time range. /// The range has an inclusive start and an exclusive end. /// The range must match the timezone configured on the history record. /// </summary> /// <param name="id">Record ID.</param> /// <param name="range">Time range.</param> /// <returns>Grid of time-series data.</returns> public Task <HaystackGrid> HisReadAsync(HaystackReference id, HaystackDateTimeRange range) { var req = new HaystackGrid() .AddColumn("id") .AddColumn("range") .AddRow(id, new HaystackString($"{ZincWriter.ToZinc(range.Start)},{ZincWriter.ToZinc(range.End)}")); return(CallAsync("hisRead", req)); }
public void GridToString_NullColumn() { var grid = new HaystackGrid() .AddColumn("val") .AddColumn("null") .AddRow(new HaystackString("value"), null); var str = ZincWriter.ToZinc(grid); Assert.AreEqual("ver:\"3.0\"\nval,null\n\"value\",N", str.Trim()); }
public void GridToString_InnerGrid() { var innerGrid = new HaystackGrid() .AddColumn("val") .AddColumn("other") .AddRow(new HaystackString("value"), new HaystackNumber(10)); var grid = new HaystackGrid() .AddColumn("val") .AddColumn("inner") .AddRow(new HaystackString("value"), innerGrid); var str = ZincWriter.ToZinc(grid); Assert.AreEqual("ver:\"3.0\"\nval,inner\n\"value\",<<\nver:\"3.0\"\nval,other\n\"value\",10\n>>", str.Trim()); }
/// <summary> /// Use vendor specific logic using the "eval" call. /// </summary> /// <param name="req">Grid with expressions in the "expr" field.</param> /// <returns>Grid of results per expression.</returns> public async Task <HaystackGrid[]> EvalAllAsync(HaystackGrid req) { var requestZinc = ZincWriter.ToZinc(req); var resultZinc = await PostStringAsync("evalAll", requestZinc, "text/zinc"); var result = new ZincReader(resultZinc).ReadGrids().ToArray(); for (int i = 0; i < result.Length; ++i) { if (result[i].IsError()) { throw new HaystackException(result[i]); } } return(result); }
public void TestNumber() { VerifyZinc(new HaystackNumber(123), "123"); VerifyZinc(new HaystackNumber(123.4, "m/s"), "123.4m/s"); VerifyZinc(new HaystackNumber(9.6, "m/s"), "9.6m/s"); VerifyZinc(new HaystackNumber(-5.2, "\u00b0F"), "-5.2\u00b0F"); VerifyZinc(new HaystackNumber(23, "%"), "23%"); VerifyZinc(new HaystackNumber(2.4e-3, "fl_oz"), "0.0024fl_oz"); VerifyZinc(new HaystackNumber(2.4e5, "$"), "240000$"); Assert.IsTrue(ZincReader.ReadValue("1234.56fl_oz").Equals(new HaystackNumber(1234.56, "fl_oz"))); Assert.IsTrue(ZincReader.ReadValue("0.000028fl_oz").Equals(new HaystackNumber(0.000028, "fl_oz"))); // specials VerifyZinc(new HaystackNumber(double.NegativeInfinity), "-INF"); VerifyZinc(new HaystackNumber(double.PositiveInfinity), "INF"); VerifyZinc(new HaystackNumber(double.NaN), "NaN"); // verify units never serialized for special values Assert.AreEqual(ZincWriter.ToZinc(new HaystackNumber(double.NaN, "ignore")), "NaN"); Assert.AreEqual(ZincWriter.ToZinc(new HaystackNumber(double.PositiveInfinity, "%")), "INF"); Assert.AreEqual(ZincWriter.ToZinc(new HaystackNumber(double.NegativeInfinity, "%")), "-INF"); }
public override string toZinc() => ZincWriter.ToZinc(M.Map(this));
public override string toZinc() => ZincWriter.ToZinc(Source);
protected void VerifyZinc(HaystackValue val, string s) { Assert.AreEqual(s, ZincWriter.ToZinc(val)); Assert.IsTrue(ZincReader.ReadValue(s).Equals(val)); }
public override string ToString() => ZincWriter.ToZinc(Source.Start) + "," + ZincWriter.ToZinc(Source.End);