public static Expr ToFaunaObj(this object obj)
        {
            var fields = new Dictionary <string, Expr>();

            foreach (var prop in obj.GetType().GetProperties())
            {
                var propType = prop.PropertyType;
                if (propType.Name.StartsWith("CompositeIndex"))
                {
                    continue;
                }
                var propValue = prop.GetValue(obj);
                var propName  = prop.GetFaunaFieldName().Replace("data.", "");
                if (propName == "ref" || propName == "ts")
                {
                    continue;
                }
                if (propValue == null)
                {
                    fields[propName] = Language.Null();
                }
                switch (Type.GetTypeCode(propType))
                {
                case TypeCode.Byte:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Single:
                case TypeCode.String:
                case TypeCode.Boolean:
                    fields[propName] = (dynamic)propValue;
                    continue;

                case TypeCode.UInt64:
                    fields[propName] = (ulong)propValue;
                    continue;

                case TypeCode.Object:
                    var referenceAttr = prop.GetCustomAttribute(typeof(ReferenceAttribute));

                    if (typeof(IEnumerable).IsAssignableFrom(propType))
                    {
                        fields[propName] = referenceAttr == null
                                ? Language.Arr(((IEnumerable)propValue).Cast <object>().Select(a => a.ToFaunaObjOrPrimitive()).ToArray())
                                : Language.Arr(((IEnumerable)propValue).Cast <IReferenceType>().Select(a => Language.Ref(a.Id)).ToArray());

                        continue;
                    }

                    fields[propName] = referenceAttr == null?propValue.ToFaunaObj() : Language.Ref(((IReferenceType)propValue).Id);

                    continue;

                case TypeCode.Char:
                    fields[propName] = propValue.ToString();
                    continue;

                case TypeCode.DateTime:
                    fields[propName] = Language.Time(((DateTime)propValue).ToString("O"));
                    continue;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(Language.Obj(fields));
        }
Ejemplo n.º 2
0
 internal static Expr GetFaunaFieldPath(this PropertyInfo propInfo)
 {
     return(Language.Arr(GetFaunaFieldName(propInfo).Split('.').ToExprArray()));
 }