public override Spec.Datum ConvertObject(T arrayObject) { if (arrayObject == null) { return new Spec.Datum() { type = Spec.Datum.DatumType.R_NULL } } ; var retval = new Spec.Datum() { type = Spec.Datum.DatumType.R_ARRAY }; var array = (IEnumerable)arrayObject; foreach (var obj in array) { retval.r_array.Add(arrayTypeConverter.ConvertObject(obj)); } return(retval); } #endregion }
public override Dictionary <string, TValue> ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(null); } if (datum.type != RethinkDb.Spec.Datum.DatumType.R_OBJECT) { throw new NotSupportedException("Attempted to convert Datum to named-value dictionary, but Datum was unsupported type " + datum.type); } Dictionary <string, TValue> retval = new Dictionary <string, TValue>(); IDatumConverter valueConverter = null; if (typeof(TValue) != typeof(object)) { valueConverter = rootDatumConverterFactory.Get <TValue>(); } foreach (var kvp in datum.r_object) { IDatumConverter thisValueConverter = valueConverter; if (thisValueConverter == null) { Type valueType = rootDatumConverterFactory.GetBestNativeTypeForDatum(kvp.val); thisValueConverter = GetConverter(valueType); } retval[kvp.key] = (TValue)thisValueConverter.ConvertDatum(kvp.val); } return(retval); }
public override long ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable long, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { if (datum.r_num > long.MaxValue || datum.r_num < long.MinValue) { throw new NotSupportedException("Attempted to cast Datum to non-nullable long, but Datum outside range of valid long"); } if (datum.r_num % 1 == 0) { return((long)datum.r_num); } throw new NotSupportedException("Attempted to cast fractional Datum to non-nullable long"); } else { throw new NotSupportedException("Attempted to cast Datum to Long, but Datum was unsupported type " + datum.type); } }
public override T ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(default(T)); } else if (datum.type == Spec.Datum.DatumType.R_OBJECT) { object[] constructorParameters = new object[properties.Count]; foreach (var assocPair in datum.r_object) { var property = properties.Where(pi => String.Equals(pi.Name, assocPair.key, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); if (property == null) { throw new InvalidOperationException("Unexpected key/value pair in anonymous-type object: " + assocPair.key); } constructorParameters[property.Index] = property.DatumConverter.ConvertDatum(assocPair.val); } return((T)(typeConstructor.Invoke(constructorParameters))); } else { throw new NotSupportedException("Attempted to cast Datum to anonymous type, but Datum was unsupported type " + datum.type); } }
public override ushort ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable unsigned short, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { if (datum.r_num > ushort.MaxValue || datum.r_num < ushort.MinValue) { throw new NotSupportedException("Attempted to cast Datum outside range of unsigned short"); } if (datum.r_num % 1 == 0) { return((ushort)datum.r_num); } else { throw new NotSupportedException("Attempted to cast fractional Datum to unsigned short"); } } else { throw new NotSupportedException("Attempted to cast Datum to unsigned Short, but Datum was unsupported type " + datum.type); } }
public override Spec.Datum ConvertObject(T anonymousObject) { if (anonymousObject == null) { return new Spec.Datum() { type = Spec.Datum.DatumType.R_NULL } } ; var datum = new Spec.Datum() { type = Spec.Datum.DatumType.R_OBJECT }; foreach (var property in properties) { var value = property.GetMethod.Invoke(anonymousObject, new object[0]); datum.r_object.Add(new Spec.Datum.AssocPair() { key = property.Name, val = property.DatumConverter.ConvertObject(value), }); } return(datum); }
public override sbyte ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable signed byte, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { if (datum.r_num > sbyte.MaxValue || datum.r_num < sbyte.MinValue) { throw new NotSupportedException("Attempted to cast Datum outside range of signed byte"); } if (datum.r_num % 1 == 0) { return((sbyte)datum.r_num); } else { throw new NotSupportedException("Attempted to cast fractional Datum to signed byte"); } } else { throw new NotSupportedException("Attempted to cast Datum to signed Byte, but Datum was unsupported type " + datum.type); } }
public override ulong ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable unsigned long, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { var valueAsLong = (ulong)datum.r_num; if (valueAsLong >= ulong.MaxValue || valueAsLong <= ulong.MinValue) { throw new NotSupportedException("Attempted to cast Datum to non-nullable unsigned long, but Datum outside range of valid unsigned long"); } if (datum.r_num % 1 == 0) { return((ulong)datum.r_num); } else { throw new NotSupportedException("Attempted to cast fractional Datum to non-nullable unsigned long"); } } else { throw new NotSupportedException("Attempted to cast Datum to unsigned Long, but Datum was unsupported type " + datum.type); } }
public override byte[] ConvertDatum(Spec.Datum datum) { if (datum.type == Datum.DatumType.R_NULL) { return(null); } else if (datum.type != Datum.DatumType.R_OBJECT) { throw new NotSupportedException("Attempted to cast Datum to byte[], but Datum was unexpected type " + datum.type); } var keys = datum.r_object.ToDictionary(kvp => kvp.key, kvp => kvp.val); Datum reql_type; if (!keys.TryGetValue("$reql_type$", out reql_type) || reql_type.type != Datum.DatumType.R_STR || reql_type.r_str != "BINARY") { throw new NotSupportedException("Attempted to cast OBJECT to byte[], but Datum was not $reql_type$ = BINARY"); } Datum data; if (!keys.TryGetValue("data", out data) || data.type != Datum.DatumType.R_STR) { throw new NotSupportedException("Attempted to cast OBJECT to byte[], but object was missing data field"); } return(Convert.FromBase64String(data.r_str)); }
public override Dictionary <string, TValue> .ValueCollection ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(null); } return(dictionaryConverter.ConvertDatum(datum).Values); }
private static void WriteDatum(JsonWriter writer, Spec.Datum datum) { switch (datum.type) { case Spec.Datum.DatumType.R_BOOL: writer.WriteBoolean(datum.r_bool); break; case Spec.Datum.DatumType.R_JSON: throw new NotSupportedException(); case Spec.Datum.DatumType.R_NULL: writer.WriteNull(); break; case Spec.Datum.DatumType.R_NUM: writer.WriteNumber(datum.r_num); break; case Spec.Datum.DatumType.R_STR: writer.WriteString(datum.r_str); break; case Spec.Datum.DatumType.R_ARRAY: { var newterm = new Term() { type = Term.TermType.MAKE_ARRAY }; newterm.args.AddRange(datum.r_array.Select(ap => new Term() { type = Term.TermType.DATUM, datum = ap, })); WriteTerm(writer, newterm); } break; case Spec.Datum.DatumType.R_OBJECT: { var newterm = new Term() { type = Term.TermType.MAKE_OBJ }; newterm.optargs.AddRange(datum.r_object.Select(ap => new Term.AssocPair() { key = ap.key, val = new Term() { type = Term.TermType.DATUM, datum = ap.val } })); WriteTerm(writer, newterm); } break; } }
public override Dictionary <string, TValue> .KeyCollection ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(null); } string[] keys = arrayDatumConverter.ConvertDatum(datum); return(keys.ToDictionary(k => k, k => default(TValue)).Keys); }
public Nullable <T> ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(new Nullable <T>()); } else { return(new Nullable <T>(innerConverter.ConvertDatum(datum))); } }
public override Object ConvertDatum(Spec.Datum datum) { if (datum.type == Datum.DatumType.R_NULL) { return(null); } Type valueType = rootDatumConverterFactory.GetBestNativeTypeForDatum(datum); var valueConverter = rootDatumConverterFactory.Get(valueType); return(valueConverter.ConvertDatum(datum)); }
public override Guid ConvertDatum(Spec.Datum datum) { Guid guid; if (Guid.TryParse(datum.r_str, out guid)) { return(guid); } else { throw new Exception(string.Format("Not valid serialized Guid: {0}", datum.r_str)); } }
public override Bound ConvertDatum(Spec.Datum datum) { switch (datum.r_str) { case "open": return(Bound.Open); case "closed": return(Bound.Closed); default: throw new NotSupportedException("Bound value must be open or closed"); } }
public override string ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(null); } else if (datum.type == Spec.Datum.DatumType.R_STR) { return(datum.r_str); } else { throw new NotSupportedException("Attempted to cast Datum to string, but Datum was unsupported type " + datum.type); } }
public override double ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable double, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { return(datum.r_num); } else { throw new NotSupportedException("Attempted to cast Datum to Double, but Datum was unsupported type " + datum.type); } }
public override Uri ConvertDatum(Spec.Datum datum) { if (datum.type == Datum.DatumType.R_NULL) { return(null); } Uri uri; if (Uri.TryCreate(datum.r_str, UriKind.Absolute, out uri)) { return(uri); } else { throw new Exception(string.Format("Not valid serialized Uri: {0}", datum.r_str)); } }
public override Spec.Datum ConvertObject(Dictionary <string, TValue> dictionary) { if (dictionary == null) { return new Spec.Datum() { type = Spec.Datum.DatumType.R_NULL } } ; var retval = new Spec.Datum() { type = Spec.Datum.DatumType.R_OBJECT }; foreach (var kvp in dictionary) { IDatumConverter valueConverter; if ((object)kvp.Value == null) { // can't call kvp.Value.GetType() if it's null! valueConverter = GetConverter(typeof(object)); } else { valueConverter = GetConverter(kvp.Value.GetType()); } var convertedValue = valueConverter.ConvertObject(kvp.Value); retval.r_object.Add(new RethinkDb.Spec.Datum.AssocPair() { key = kvp.Key, val = convertedValue, }); } return(retval); } #endregion }
public override T ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(default(T)); } else if (datum.type == Spec.Datum.DatumType.R_ARRAY) { var retval = Array.CreateInstance(typeof(T).GetElementType(), datum.r_array.Count); for (int i = 0; i < datum.r_array.Count; i++) { retval.SetValue(arrayTypeConverter.ConvertDatum(datum.r_array [i]), i); } return((T)Convert.ChangeType(retval, typeof(T))); } else { throw new NotSupportedException("Attempted to cast Datum to array, but Datum was unsupported type " + datum.type); } }
public override char ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable char, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_STR) { if (datum.r_str.Length != 1) { throw new NotSupportedException("Attempted to cast Datum to char, but Datum was not a single character"); } return(datum.r_str[0]); } else { throw new NotSupportedException("Attempted to cast Datum to non-nullable char, but Datum was unsupported type " + datum.type); } }
public override decimal ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable decimal, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { if (datum.r_num >= Convert.ToDouble(decimal.MaxValue) || datum.r_num <= Convert.ToDouble(decimal.MinValue)) { throw new NotSupportedException("Attempted to cast Datum outside range of decimal"); } else { return(Convert.ToDecimal(datum.r_num)); } } else { throw new NotSupportedException("Attempted to cast Datum to Decimal, but Datum was unsupported type " + datum.type); } }
public override float ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { throw new NotSupportedException("Attempted to cast Datum to non-nullable float, but Datum was null"); } else if (datum.type == Spec.Datum.DatumType.R_NUM) { var valueAsFloat = (float)datum.r_num; if (valueAsFloat >= float.MaxValue || valueAsFloat <= float.MinValue) { throw new NotSupportedException("Attempted to cast Datum outside range of float"); } else { return((float)datum.r_num); } } else { throw new NotSupportedException("Attempted to cast Datum to Float, but Datum was unsupported type " + datum.type); } }
public override T ConvertDatum(Spec.Datum datum) { var value = PrimitiveDatumConverterFactory.UnsignedLongDatumConverter.Instance.Value.ConvertDatum(datum); return((T)Enum.ToObject(typeof(T), value)); }
public Type GetBestNativeTypeForDatum(Spec.Datum datum) { // Attempt to auto-detect the best native type for a given Datum. switch (datum.type) { case Datum.DatumType.R_ARRAY: { var hasNullValues = datum.r_array.Any(d => d.type == Datum.DatumType.R_NULL); var arrayValuesExcludingNulls = datum.r_array.Where(d => d.type != Datum.DatumType.R_NULL); var nativeTypesExcludingNulls = arrayValuesExcludingNulls.Select(GetBestNativeTypeForDatum).Distinct().ToArray(); if (nativeTypesExcludingNulls.Length == 0) { // only have nulls, or, empty return(typeof(object[])); } if (nativeTypesExcludingNulls.Length == 2 && nativeTypesExcludingNulls.Contains(typeof(double)) && nativeTypesExcludingNulls.Contains(typeof(int))) { // we have numbers, both ints and doubles; we'll make an array of doubles as the return value. // This is a special case; only works because we know GetBestNativeTypeForDatum will only return int or double. If that changes, // we need a more sophisticated manner to get the best numeric type here. nativeTypesExcludingNulls = new [] { typeof(double) } } ; if (nativeTypesExcludingNulls.Length == 1) { Type arrayContentType = nativeTypesExcludingNulls[0]; if (!hasNullValues || !arrayContentType.IsValueType) { // we either have no nulls, or, this type can be assigned to null, so we just use the type return(arrayContentType.MakeArrayType()); } else { // the type is Nullable<T>[], where T is the type of all the objects in the array return(typeof(Nullable <>).MakeGenericType(arrayContentType).MakeArrayType()); } } throw new RethinkDbRuntimeException("Heterogeneous arrays are not currently supported as their types are indistinguishable"); } case Datum.DatumType.R_BOOL: return(typeof(bool)); case Datum.DatumType.R_NULL: return(typeof(object)); case Datum.DatumType.R_NUM: if (datum.r_num == Math.Floor(datum.r_num)) { return(typeof(int)); } else { return(typeof(double)); } case Datum.DatumType.R_OBJECT: { var reqlTypeDatum = datum.r_object.SingleOrDefault(kvp => kvp.key == "$reql_type$"); if (reqlTypeDatum != null && reqlTypeDatum.val.type == Datum.DatumType.R_STR) { var reqlType = reqlTypeDatum.val.r_str; switch (reqlType) { case "BINARY": return(typeof(byte[])); case "TIME": return(typeof(DateTimeOffset)); default: throw new RethinkDbInternalErrorException("Unrecognized reql_type"); } } return(typeof(Dictionary <string, object>)); } case Datum.DatumType.R_STR: return(typeof(string)); case Datum.DatumType.R_JSON: throw new RethinkDbInternalErrorException("Unsupported datum type"); default: throw new RethinkDbInternalErrorException("Unrecognized datum type"); } }
public override T ConvertDatum(Spec.Datum datum) { if (datum.type == Spec.Datum.DatumType.R_NULL) { return(default(T)); } else if (datum.type == Spec.Datum.DatumType.R_OBJECT) { if (itemConverters.Length != 2) { throw new NotSupportedException("TupleDatumConverter only supports OBJECT values if it's a two-tuple; this one is a " + typeof(T).FullName); } object item1 = null; object item2 = null; foreach (var assocPair in datum.r_object) { // left/right for a join if (assocPair.key == "left") { item1 = itemConverters[0].ConvertDatum(assocPair.val); } else if (assocPair.key == "right") { item2 = itemConverters[1].ConvertDatum(assocPair.val); } // group/reduction for a grouped map reduce else if (assocPair.key == "group") { item1 = itemConverters[0].ConvertDatum(assocPair.val); } else if (assocPair.key == "reduction") { item2 = itemConverters[1].ConvertDatum(assocPair.val); } else { throw new InvalidOperationException("Unexpected key/value pair in tuple object: " + assocPair.key + "; expected left/right or group/reduction"); } } return((T)(tupleConstructor.Invoke(new object[] { item1, item2 }))); } else if (datum.type == Spec.Datum.DatumType.R_ARRAY) { if (itemConverters.Length != datum.r_array.Count) { throw new InvalidOperationException(String.Format("Unexpected array of length {0} where tuple of type {1} was expected", datum.r_array.Count, typeof(T))); } object[] values = new object[itemConverters.Length]; for (int i = 0; i < itemConverters.Length; i++) { values[i] = itemConverters[i].ConvertDatum(datum.r_array[i]); } return((T)(tupleConstructor.Invoke(values))); } else { throw new NotSupportedException("Attempted to cast Datum to tuple, but Datum was unsupported type " + datum.type); } }