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 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))); }
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 async Task UpdateUpsertEntitiesAsync() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies5p2" + _random.Next(); string partitionKey = "Stationery"; string rowKey = "A1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); await serviceClient.CreateTableAsync(tableName); var tableClient = serviceClient.GetTableClient(tableName); #region Snippet:TablesSample5UpsertEntityAsync var entity = new TableEntity(partitionKey, rowKey) { { "Product", "Markers" }, { "Price", 5.00 }, { "Brand", "myCompany" } }; // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity. await tableClient.UpsertEntityAsync(entity); #endregion #region Snippet:TablesSample5UpsertWithReplaceAsync // Delete an entity property. entity.Remove("Brand"); // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode, which defaults to Merge if not given. // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property. await tableClient.UpsertEntityAsync(entity, TableUpdateMode.Replace); #endregion #region Snippet:TablesSample5UpdateEntityAsync // Get the entity to update. TableEntity qEntity = await tableClient.GetEntityAsync <TableEntity>(partitionKey, rowKey); qEntity["Price"] = 7.00; // Since no UpdateMode was passed, the request will default to Merge. await tableClient.UpdateEntityAsync(qEntity, qEntity.ETag); TableEntity updatedEntity = await tableClient.GetEntityAsync <TableEntity>(partitionKey, rowKey); Console.WriteLine($"'Price' before updating: ${entity.GetDouble("Price")}"); Console.WriteLine($"'Price' after updating: ${updatedEntity.GetDouble("Price")}"); #endregion await serviceClient.DeleteTableAsync(tableName); }
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); }
public void CreateDeleteEntity() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies2p1"; string partitionKey = "Stationery"; string rowKey = "A1"; string rowKeyStrong = "B1"; #region Snippet:TablesSample2CreateTableWithTableClient // Construct a new <see cref="TableClient" /> using a <see cref="TableSharedKeyCredential" />. var tableClient = new TableClient( tableName, new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); // Create the table in the service. tableClient.Create(); #endregion #region Snippet:TablesSample2CreateDictionaryEntity // Make a dictionary entity by defining a <see cref="TableEntity">. var entity = new TableEntity(partitionKey, rowKey) { { "Product", "Marker Set" }, { "Price", 5.00 }, { "Quantity", 21 } }; Console.WriteLine($"{entity.RowKey}: {entity["Product"]} costs ${entity.GetDouble("Price")}."); #endregion #region Snippet:TablesSample2AddEntity // Add the newly created entity. tableClient.AddEntity(entity); #endregion #region Snippet:TablesSample2CreateStronglyTypedEntity // Create an instance of the strongly-typed entity and set their properties. var strongEntity = new OfficeSupplyEntity { PartitionKey = partitionKey, RowKey = rowKeyStrong, Product = "Notebook", Price = 3.00, Quantity = 50 }; Console.WriteLine($"{entity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}."); #endregion // Add the newly created entity. tableClient.AddEntity(strongEntity); #region Snippet:TablesSample2DeleteEntity // Delete the entity given the partition and row key. tableClient.DeleteEntity(partitionKey, rowKey); #endregion #region Snippet:TablesSample2DeleteTableWithTableClient tableClient.Delete(); #endregion }
public void UpdateUpsertEntities() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies5p1"; string partitionKey = "somePartition"; string rowKey = "A1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); #region Snippet:TablesSample5UpsertEntity // Get the <see cref="TableClient" /> of the table. var client = serviceClient.GetTableClient(tableName); // Make an entity. var entity = new TableEntity(partitionKey, rowKey) { { "Product", "Markers" }, { "Price", 5.00 }, { "Brand", "myCompany" } }; // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity. client.UpsertEntity(entity); // Delete an entity property. entity.Remove("Brand"); // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode (which defaults to Merge if not given). // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property. client.UpsertEntity(entity, TableUpdateMode.Replace); #endregion #region Snippet:TablesSample5UpdateEntity // Query for entities to update. Pageable <TableEntity> queryResultsBefore = client.Query <TableEntity>(); foreach (TableEntity qEntity in queryResultsBefore) { // Changing property of entity. qEntity["Price"] = 7.00; // Updating to changed entity using its generated eTag. // Since no UpdateMode was passed, the request will default to Merge. client.UpdateEntity(qEntity, qEntity.ETag); } #endregion Pageable <TableEntity> queryResultsAfter = client.Query <TableEntity>(); foreach (TableEntity qEntity in queryResultsAfter) { Console.WriteLine($"'Price' before updating: ${entity.GetDouble("Price")}"); Console.WriteLine($"'Price' after updating: ${qEntity.GetDouble("Price")}"); } serviceClient.DeleteTable(tableName); }
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)); } } }