Skip to content

Commit

Permalink
Use map<anydata> type for application-properties in a message
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Mar 16, 2023
1 parent 096da17 commit 2167b3b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion asb-ballerina/message.bal
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ public type Message record {|
@display {label: "Application Properties"}
public type ApplicationProperties record {|
@display {label: "Properties"}
map<byte[]> properties?;
map<anydata> properties?;
|};
4 changes: 2 additions & 2 deletions asb-ballerina/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
# + message - Represents a message that contains application properties
# + name - Represents the name of the application property that the user wants to retrieve.
# + return - Returns null if the requested property does not exist
public isolated function getApplicationPropertyByName(Message message, string name) returns any|error? {
public isolated function getApplicationPropertyByName(Message message, string name) returns anydata|error? {
ApplicationProperties applicationPropertiesResult = check message.applicationProperties.ensureType();
map<any> properties = check applicationPropertiesResult.properties.ensureType();
map<anydata> properties = check applicationPropertiesResult.properties.ensureType();
return properties[name];
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@
import com.azure.core.util.IterableStream;
import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusException;
import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusReceivedMessage;
import com.azure.messaging.servicebus.ServiceBusReceiverClient;
import com.azure.messaging.servicebus.ServiceBusClientBuilder.ServiceBusReceiverClientBuilder;
import com.azure.messaging.servicebus.models.DeadLetterOptions;
import com.azure.messaging.servicebus.models.ServiceBusReceiveMode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.ArrayType;
import io.ballerina.runtime.api.types.MapType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
Expand Down Expand Up @@ -271,21 +268,41 @@ private BMap<BString, Object> getReceivedMessage(BObject endpointClient, Service
return createRecordValue;
}

private static BMap<BString, Object> getApplicationProperties(ServiceBusReceivedMessage message)
throws JsonProcessingException {
private static BMap<BString, Object> getApplicationProperties(ServiceBusReceivedMessage message) {
BMap<BString, Object> applicationPropertiesRecord = ValueCreator.createRecordValue(ModuleUtils.getModule(),
ASBConstants.APPLICATION_PROPERTY_TYPE);
ArrayType byteArrayType = TypeCreator.createArrayType(PredefinedTypes.TYPE_BYTE);
MapType mapType = TypeCreator.createMapType(byteArrayType);
MapType mapType = TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA);
BMap<BString, Object> applicationProperties = ValueCreator.createMapValue(mapType);
for (Map.Entry<String, Object> property: message.getApplicationProperties().entrySet()) {
String propertyKey = property.getKey();
byte[] propertyValue = OBJECT_MAPPER.writeValueAsBytes(property.getValue());
applicationProperties.put(
StringUtils.fromString(propertyKey), ValueCreator.createArrayValue(propertyValue));
populateApplicationProperty(applicationProperties, property.getKey(), property.getValue());
}
return ValueCreator.createRecordValue(applicationPropertiesRecord, applicationProperties);
}
private static void populateApplicationProperty(BMap<BString, Object> applicationProperties,
String key, Object value) {
BString propertyKey = StringUtils.fromString(key);
if (value instanceof String) {
applicationProperties.put(propertyKey, StringUtils.fromString((String) value));
} else if (value instanceof Integer) {
applicationProperties.put(propertyKey, (Integer) value);
} else if (value instanceof Long) {
applicationProperties.put(propertyKey, (Long) value);
} else if (value instanceof Float) {
applicationProperties.put(propertyKey, (Float) value);
} else if (value instanceof Double) {
applicationProperties.put(propertyKey, (Double) value);
} else if (value instanceof Boolean) {
applicationProperties.put(propertyKey, (Boolean) value);
} else if (value instanceof Character) {
applicationProperties.put(propertyKey, (Character) value);
} else if (value instanceof Byte) {
applicationProperties.put(propertyKey, (Byte) value);
} else if (value instanceof Short) {
applicationProperties.put(propertyKey, (Short) value);
} else {
applicationProperties.put(propertyKey, StringUtils.fromString(value.toString()));
}
}

/**
* Receive Batch of Messages with configurable parameters as Map when Receiver
Expand Down

0 comments on commit 2167b3b

Please sign in to comment.