Ejemplo n.º 1
0
        public static PrimitiveValue ObjectId(ObjectId value)
        {
            var result = new PrimitiveValue
            {
                Type = RealmValueType.ObjectId,
            };

            var objectIdBytes = value.ToByteArray();

            for (var i = 0; i < 12; i++)
            {
                result.object_id_bytes[i] = objectIdBytes[i];
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static PrimitiveValue Guid(Guid value)
        {
            var result = new PrimitiveValue
            {
                Type = RealmValueType.Guid,
            };

            var guidBytes = value.ToByteArray();

            for (var i = 0; i < 16; i++)
            {
                result.guid_bytes[i] = guidBytes[i];
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static PrimitiveValue Create <T>(T value, PropertyType type)
        {
            var result = new PrimitiveValue
            {
                type      = type,
                has_value = true
            };

            switch (type)
            {
            case PropertyType.Bool:
                result.bool_value = Operator.Convert <T, bool>(value);
                break;

            case PropertyType.Bool | PropertyType.Nullable:
                var boolValue = Operator.Convert <T, bool?>(value);
                result.has_value  = boolValue.HasValue;
                result.bool_value = boolValue.GetValueOrDefault();
                break;

            case PropertyType.Int:
                result.int_value = Operator.Convert <T, long>(value);
                break;

            case PropertyType.Int | PropertyType.Nullable:
                var longValue = Operator.Convert <T, long?>(value);
                result.has_value = longValue.HasValue;
                result.int_value = longValue.GetValueOrDefault();
                break;

            case PropertyType.Float:
                result.float_value = Operator.Convert <T, float>(value);
                break;

            case PropertyType.Float | PropertyType.Nullable:
                var floatValue = Operator.Convert <T, float?>(value);
                result.has_value   = floatValue.HasValue;
                result.float_value = floatValue.GetValueOrDefault();
                break;

            case PropertyType.Double:
                result.double_value = Operator.Convert <T, double>(value);
                break;

            case PropertyType.Double | PropertyType.Nullable:
                var doubleValue = Operator.Convert <T, double?>(value);
                result.has_value    = doubleValue.HasValue;
                result.double_value = doubleValue.GetValueOrDefault();
                break;

            case PropertyType.Date:
                result.int_value = Operator.Convert <T, DateTimeOffset>(value).ToUniversalTime().Ticks;
                break;

            case PropertyType.Date | PropertyType.Nullable:
                var dateValue = Operator.Convert <T, DateTimeOffset?>(value);
                result.has_value = dateValue.HasValue;
                result.int_value = dateValue.GetValueOrDefault().ToUniversalTime().Ticks;
                break;

            default:
                throw new NotSupportedException($"PrimitiveType {type} is not supported.");
            }

            return(result);
        }