public void ErlTupleTest() { var l = new ErlTuple("test", 1, 1.1, true, (byte)255, 'x', new ErlAtom("a")); var r = new ErlTuple("test", 1, 1.1, true, (byte)255, 'x', new ErlAtom("a")); Assert.AreEqual(7, l.Count); Assert.AreEqual(ErlTypeOrder.ErlString, l[0].TypeOrder); Assert.AreEqual("test", l[0].ValueAsString); Assert.AreEqual(ErlTypeOrder.ErlLong, l[1].TypeOrder); Assert.AreEqual(1, l[1].ValueAsInt); Assert.AreEqual(ErlTypeOrder.ErlDouble, l[2].TypeOrder); Assert.AreEqual(1.1, l[2].ValueAsDouble); Assert.AreEqual(ErlTypeOrder.ErlBoolean, l[3].TypeOrder); Assert.AreEqual(true, l[3].ValueAsBool); Assert.AreEqual(ErlTypeOrder.ErlByte, l[4].TypeOrder); Assert.AreEqual(255, l[4].ValueAsInt); Assert.AreEqual(ErlTypeOrder.ErlByte, l[5].TypeOrder); Assert.AreEqual('x', l[5].ValueAsChar); Assert.AreEqual(ErlTypeOrder.ErlAtom, l[6].TypeOrder); Assert.AreEqual("a", l[6].ValueAsString); Assert.IsTrue(l.Matches(r)); Assert.AreEqual(new ErlVarBind(), l.Match(r)); Assert.AreEqual(l, r); Assert.IsTrue(l.Equals(r)); Assert.AreEqual("{\"test\",1,1.1,true,255,120,a}", l.ToString()); Assert.IsFalse(l.IsScalar); Assert.AreEqual(ErlTypeOrder.ErlTuple, l.TypeOrder); IErlObject temp = null; Assert.IsFalse(l.Subst(ref temp, new ErlVarBind())); Assert.AreEqual(1, l.Visit(0, (acc, o) => acc + (o is ErlAtom ? 1 : 0))); Assert.IsTrue(new ErlTuple(new ErlVar(X), true, 1).Subst(ref temp, new ErlVarBind { { X, new ErlLong(10) } })); Assert.AreEqual("{10,true,1}", temp.ToString()); var d = new DateTime(2013, 1, 2); var ts = new TimeSpan(1, 2, 3); Assert.DoesNotThrow(() => { var x = l.ValueAsObject; }); Assert.AreEqual(1, new ErlList("1")[0].ValueAsInt); Assert.AreEqual(1, new ErlList("1")[0].ValueAsLong); Assert.AreEqual(1, new ErlList("1")[0].ValueAsDecimal); Assert.AreEqual(d, new ErlList(d.ToString())[0].ValueAsDateTime); Assert.AreEqual(ts, new ErlList(ts.ToString())[0].ValueAsTimeSpan); Assert.AreEqual(1.0, new ErlList("1.0")[0].ValueAsDouble); Assert.AreEqual("a", new ErlList("a")[0].ValueAsString); Assert.IsTrue(new ErlList("true")[0].ValueAsBool); Assert.IsFalse(new ErlList("xxxx")[0].ValueAsBool); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsInt; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsLong; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsDecimal; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsDateTime; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsTimeSpan; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsDouble; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsString; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsBool; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsChar; }); Assert.Throws <ErlIncompatibleTypesException>(() => { var x = l.ValueAsByteArray; }); List <IErlObject> s = l; Assert.AreEqual(l.Value, s); Assert.IsFalse(new ErlList(1, 1.0, "a").Equals(new ErlTuple(1, 1.0, "a"))); Assert.IsFalse(new ErlList(1, 1.0, "a") == new ErlTuple(1, 1.0, "a")); Assert.IsTrue(new ErlList(1, 1.0, "a") == new ErlList(1, 1.0, "a")); }
/// <summary> /// Maps ErlRow to CLR row supplied by schema, either Dynamic or TypedRow /// </summary> public Row ErlTupleToRow(string schemaName, ErlTuple tuple, Schema schema) { var singleKey = schema.ExtraData[SCHEMA_KEY_COUNT].AsInt(0) < 2; var row = Row.MakeRow(schema, schema.TypedRowType); var i = -1; foreach (var elm in enumErlResponseTuple(tuple, singleKey)) { i++; if (i == 0) { var thisname = tuple[0].ValueAsString; if (!schemaName.EqualsOrdSenseCase(thisname)) { throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_MISMATCH_ERROR.Args(thisname, schemaName)); } continue; } //map fields now if (i - 1 >= schema.FieldCount) { throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_FLD_COUNT_MISMATCH_ERROR.Args(schemaName, schema.Name)); } var fdef = schema[i - 1]; var atr = fdef[m_Store.TargetName]; var erlType = atr.BackendType; if (erlType.IsNullOrWhiteSpace()) { throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR + "fielddef('{0}') has no backend type".Args(fdef.Name)); } if (elm.IsNull()) { row.SetFieldValue(fdef, null); continue; } Tuple <Type, Func <IErlObject, object> > mapping; if (ERL_TO_CLR_TYPEMAP.TryGetValue(erlType, out mapping)) { object clrValue = null; try { clrValue = mapping.Item2(elm); } catch (Exception error) { var err = "Schema '{0}' field '{1}' ({2}) cannot be converted from '{3}' to '{4}' format in record:\n {5}" .Args(schemaName, fdef.Name, elm.ToString(), elm.GetType(), erlType, tuple.ToString()); App.Log.Write(new Log.Message { Type = Log.MessageType.TraceErl, Topic = CoreConsts.ERLANG_TOPIC, From = "SchemaMap.ErlTupleToRow({0})".Args(schemaName), Text = err + error.ToMessageWithType(), Exception = error }); throw new ErlDataAccessException(err, inner: error); } row.SetFieldValue(fdef, clrValue); } else { var err = StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR + "erltype'{0}' not matched in the dict".Args(erlType); throw new ErlDataAccessException(err); } } return(row); }