public void TestBasics() { var tags = new HaystackDictionary(); tags.Add("id", new HaystackReference("aaaa-bbbb")); tags.Add("site", new HaystackMarker()); tags.Add("geoAddr", new HaystackString("Richmond, Va")); tags.Add("area", new HaystackNumber(1200, "ft")); tags.Add("date", new HaystackDate(2000, 12, 3)); tags.Add("null", null); // size Assert.AreEqual(5, tags.Count); Assert.IsFalse(tags.IsEmpty()); // configured tags Assert.IsTrue(tags.Get("id").Equals(new HaystackReference("aaaa-bbbb"))); Assert.IsTrue(tags.Get("site").Equals(new HaystackMarker())); Assert.IsTrue(tags.Get("geoAddr").Equals(new HaystackString("Richmond, Va"))); Assert.IsTrue(tags.Get("area").Equals(new HaystackNumber(1200, "ft"))); Assert.IsTrue(tags.Get("date").Equals(new HaystackDate(2000, 12, 3))); Assert.ThrowsException <HaystackUnknownNameException>(() => tags.Get("null")); // missing tag Assert.IsFalse(tags.ContainsKey("foo")); Assert.ThrowsException <HaystackUnknownNameException>(() => tags.Get("foo")); }
public static TValue GetUnchecked <TValue>(this HaystackDictionary dict, string name) where TValue : HaystackValue { if (dict.ContainsKey(name)) { return(dict.Get <TValue>(name)); } return(null); }
public void TestEmpty() { // Instance Empty var tags = new HaystackDictionary(); Assert.AreEqual(tags, new HaystackDictionary()); // size Assert.AreEqual(tags.Count, 0); Assert.IsTrue(tags.IsEmpty()); // missing tag Assert.IsFalse(tags.ContainsKey("foo")); }
/// <summary> /// Read a single record by its unique ID. /// Throws an exception if the record was not found. /// </summary> /// <param name="id">Record ID.</param> /// <returns>Matching record.</returns> public async Task <HaystackDictionary> ReadByIdAsync(HaystackReference id) { HaystackGrid res = await ReadByIdsAsync(new HaystackReference[] { id }); if (res.IsEmpty()) { throw new Exception($"Record not found for: {id}"); } HaystackDictionary rec = res.Row(0); if (!rec.ContainsKey("id")) { throw new Exception($"Record not found for: {id}"); } return(rec); }