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); }
public static SensorAlarm Map(TableEntity entity) { return new SensorAlarm { SensorBoxId = entity.PartitionKey, Identifier = entity.RowKey, SensorType = Enum.Parse<SensorType>(entity.GetString(nameof(SensorAlarm.SensorType))), Status = Enum.Parse<AlarmStatus>(entity.GetString(nameof(SensorAlarm.Status))), FiredAt = entity.Timestamp ?? DateTimeOffset.MinValue }; }
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.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))); }
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 if (ef.type == typeof(long)) { return(long.Parse(entity.GetString(ef.kind.ToString()))); } else if (ef.type.IsClass) { return(ef.type.GetConstructor(new[] { typeof(string) }) !.Invoke(new[] { entity.GetString(ef.kind.ToString()) })); } else { throw new Exception($"invalid partition or row key type of {info.type} property {name}: {ef.type}"); } } var fieldName = ef.columnName; var obj = entity[fieldName]; if (obj == null) { if (ef.parameterInfo.HasDefaultValue) { return(ef.parameterInfo.DefaultValue); } return(ef.defaultValue switch { DefaultValueAttribute { InitMethod: InitMethod.DefaultConstructor } => Activator.CreateInstance(ef.type),
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.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); }
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)); } } }