public void ValidateDictionaryEntityGetTypes()
 {
     Assert.That(fullEntity.GetBinary("binary"), Is.InstanceOf(typeof(byte[])));
     Assert.That(fullEntity.GetBoolean("boolean"), Is.InstanceOf(typeof(bool?)));
     Assert.That(fullEntity.GetDateTime("datetime"), Is.InstanceOf(typeof(DateTime?)));
     Assert.That(fullEntity.GetDateTimeOffset("datetimeoffset"), Is.InstanceOf(typeof(DateTimeOffset?)));
     Assert.That(fullEntity.GetDouble("double"), Is.InstanceOf(typeof(double?)));
     Assert.That(fullEntity.GetGuid("guid"), Is.InstanceOf(typeof(Guid)));
     Assert.That(fullEntity.GetInt32("int32"), Is.InstanceOf(typeof(int?)));
     Assert.That(fullEntity.GetInt64("int64"), Is.InstanceOf(typeof(long?)));
     Assert.That(fullEntity.GetString("string"), Is.InstanceOf(typeof(string)));
 }
Exemplo n.º 2
0
        public void TestFromtableEntity()
        {
            var converter   = new EntityConverter();
            var tableEntity = new TableEntity(Guid.NewGuid().ToString(), "test")
            {
                { "the_date", DateTimeOffset.UtcNow },
                { "the_number", 1234 },
                { "the_float", 12.34 },
                { "the_enum", "the_two" },
                { "the_flag", "flag_one,flag_two" },
                { "a__special__name", "renamed" },
                { "the_object", "{\"the_name\": \"testName\", \"the_enum\": \"the_one\", \"the_flag\": \"flag_one,flag_two\"}" },
                { "test_null", null },
            };

            var entity1 = converter.ToRecord <Entity1>(tableEntity);

            Assert.NotNull(entity1);
            Assert.Equal(tableEntity.PartitionKey, entity1.Id.ToString());
            Assert.Equal(tableEntity.RowKey, entity1.TheName.ToString());
            Assert.Equal(tableEntity.GetDateTimeOffset("the_date"), entity1.TheDate);
            Assert.Equal(tableEntity.GetInt32("the_number"), entity1.TheNumber);
            Assert.Equal(tableEntity.GetDouble("the_float"), entity1.TheFloat);
            Assert.Equal(TestEnum.TheTwo, entity1.TheEnum);
            Assert.Equal(tableEntity.GetString("a__special__name"), entity1.Renamed);
            Assert.Null(tableEntity.GetString("test_null"));
            Assert.Null(entity1.TestNull);

            Assert.Equal("testName", entity1.TheObject.TheName);
            Assert.Equal(TestEnum.TheOne, entity1.TheObject.TheEnum);
            Assert.Equal(TestFlagEnum.FlagOne | TestFlagEnum.FlagTwo, entity1.TheObject.TheFlag);
        }
Exemplo n.º 3
0
 public static Sensor Map(TableEntity entity)
 {
     return(new Sensor
     {
         BoxId = entity.PartitionKey,
         Type = Enum.Parse <SensorType>(entity.RowKey),
         LastSeen = entity.GetDateTimeOffset(nameof(Sensor.LastSeen)) ?? DateTimeOffset.MinValue,
         Max = entity.GetDouble(nameof(Sensor.Max)) ?? 0,
         Min = entity.GetDouble(nameof(Sensor.Min)) ?? 0
     });
 }
 public void ValidateDictionaryEntityGetTypeForNulledProperties()
 {
     Assert.That(emptyEntity.GetBinary(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetBoolean(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetDateTime(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetDateTimeOffset(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetDouble(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetGuid(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetInt32(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetInt64(nulledPropertyKey), Is.Null);
     Assert.That(emptyEntity.GetString(nulledPropertyKey), Is.Null);
 }
Exemplo n.º 5
0
        public void TypeCoercionForDateTimeTypes()
        {
            var entity    = new TableEntity("partition", "row");
            var offsetNow = DateTimeOffset.UtcNow;
            var dateNow   = offsetNow.UtcDateTime;

            // Initialize a property to an DateTimeOffset value
            entity["DTOffset"] = offsetNow;
            Assert.That(entity["DTOffset"] is DateTimeOffset);
            Assert.That(entity["DTOffset"], Is.EqualTo(offsetNow));
            Assert.That(entity.GetDateTimeOffset("DTOffset"), Is.EqualTo(offsetNow));
            Assert.That(entity.GetDateTime("DTOffset"), Is.EqualTo(dateNow));

            // Initialize a property to an DateTime value
            entity["DT"] = dateNow;
            Assert.AreEqual(typeof(DateTime), entity["DT"].GetType());
            DateTimeOffset dtoffset = (DateTime)entity["DT"];

            Assert.That(entity["DT"], Is.EqualTo(dateNow));
            Assert.That(entity.GetDateTime("DT"), Is.EqualTo(dateNow));
            Assert.That(entity.GetDateTimeOffset("DT"), Is.EqualTo(offsetNow));
        }
Exemplo n.º 6
0
    private object?GetFieldValue(EntityInfo info, string name, TableEntity entity)
    {
        var ef = info.properties[name].First();

        if (ef.kind == EntityPropertyKind.PartitionKey || ef.kind == EntityPropertyKind.RowKey)
        {
            if (ef.type == typeof(string))
            {
                return(entity.GetString(ef.kind.ToString()));
            }
            else if (ef.type == typeof(Guid))
            {
                return(Guid.Parse(entity.GetString(ef.kind.ToString())));
            }
            else if (ef.type == typeof(int))
            {
                return(int.Parse(entity.GetString(ef.kind.ToString())));
            }
            else
            {
                throw new Exception("invalid ");
            }
        }

        var fieldName = ef.columnName;
        var obj       = entity[fieldName];

        if (obj == null)
        {
            return(null);
        }
        var objType = obj.GetType();

        if (ef.type == typeof(string))
        {
            return(entity.GetString(fieldName));
        }
        else if (ef.type == typeof(bool) || ef.type == typeof(bool?))
        {
            return(entity.GetBoolean(fieldName));
        }
        else if (ef.type == typeof(DateTimeOffset) || ef.type == typeof(DateTimeOffset?))
        {
            return(entity.GetDateTimeOffset(fieldName));
        }
        else if (ef.type == typeof(DateTime) || ef.type == typeof(DateTime?))
        {
            return(entity.GetDateTime(fieldName));
        }
        else if (ef.type == typeof(double) || ef.type == typeof(double?))
        {
            return(entity.GetDouble(fieldName));
        }
        else if (ef.type == typeof(Guid) || ef.type == typeof(Guid?))
        {
            return((object?)Guid.Parse(entity.GetString(fieldName)));
        }
        else if (ef.type == typeof(int) || ef.type == typeof(short) || ef.type == typeof(int?) || ef.type == typeof(short?))
        {
            return(entity.GetInt32(fieldName));
        }
        else if (ef.type == typeof(long) || ef.type == typeof(long?))
        {
            return(entity.GetInt64(fieldName));
        }
        else
        {
            var outputType = ef.type;
            if (ef.discriminator != null)
            {
                var(attr, typeProvider) = ef.discriminator.Value;
                var v = GetFieldValue(info, attr.FieldName, entity) ?? throw new Exception($"No value for {attr.FieldName}");
                outputType = typeProvider.GetTypeInfo(v);
            }


            if (objType == typeof(string))
            {
                var value = entity.GetString(fieldName);
                if (value.StartsWith('[') || value.StartsWith('{') || value == "null")
                {
                    return(JsonSerializer.Deserialize(value, outputType, options: _options));
                }
                else
                {
                    return(JsonSerializer.Deserialize($"\"{value}\"", outputType, options: _options));
                }
            }
            else
            {
                var value = entity.GetString(fieldName);
                return(JsonSerializer.Deserialize(value, outputType, options: _options));
            }
        }
    }