Skip to content

Commit

Permalink
#27 Make a good attempt to cast from Object to String
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Oct 17, 2023
1 parent 97126e8 commit cb5723a
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.azure.data.tables.models.ListEntitiesOptions;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -34,8 +36,31 @@ public TableClient getTableClient() {
public List<Map<String,Object>> findRows(String partitionKey) {
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter(String.format("PartitionKey eq '%s'", partitionKey));
//Cast from Map<String, Object> to Map<String, String>
return tableClient.listEntities(options, null, null).stream()
.map(tableEntity -> tableEntity.getProperties())
.toList();
}

public List<Map<String,String>> listRows(String partitionKey) {
List<Map<String,String>> stringRows = new ArrayList<>();
List<Map<String, Object>> rows = findRows(partitionKey);
for (Map<String, Object> row : rows) {
Map<String,String> stringRow = new HashMap<>();
for (String key : row.keySet()) {
Object value = row.get(key);
if (value instanceof String) {
stringRow.put(key, (String) value);
} else {
try {
stringRow.put(key, value.toString());
} catch (Exception e) {
log.error("Could not convert value {} to String", value);
stringRow.put(key, "--FailedInConversionFromAzureTable--");
}
}
}
}
return stringRows;
}
}

0 comments on commit cb5723a

Please sign in to comment.