use of org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException in project ballerina by ballerina-lang.
the class MessageUtils method generateRequestStruct.
public static BValue generateRequestStruct(Message request, String fieldName, BType structType, Context context) {
BValue bValue = null;
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
if (structType instanceof BStructType) {
BStruct requestStruct = createStruct(context, fieldName);
for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
String structFieldName = structField.getFieldName();
if (structField.getFieldType() instanceof BRefType) {
BType bType = structField.getFieldType();
if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
Message message = (Message) request.getFields().get(structFieldName);
requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, structFieldName, structField.getFieldType(), context));
}
} else {
if (request.getFields().containsKey(structFieldName)) {
String fieldType = structField.getFieldType().getName();
switch(fieldType) {
case STRING:
{
requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
break;
}
case INT:
{
requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
}
}
}
}
}
bValue = requestStruct;
} else {
Map<String, Object> fields = request.getFields();
if (fields.size() == 1 && fields.containsKey("value")) {
fieldName = "value";
}
if (request.getFields().containsKey(fieldName)) {
String fieldType = structType.getName();
switch(fieldType) {
case STRING:
{
bValue = new BString((String) request.getFields().get(fieldName));
break;
}
case INT:
{
bValue = new BInteger((Long) request.getFields().get(fieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
bValue = new BBoolean((Boolean) request.getFields().get(fieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
}
}
}
}
return bValue;
}
Aggregations