use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class AbstractExecute method createStruct.
BStruct createStruct(Context context, String structName) {
PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(PROTOCOL_STRUCT_PACKAGE_GRPC);
StructInfo structInfo = httpPackageInfo.getStructInfo(structName);
BStructType structType = structInfo.getType();
return new BStruct(structType);
}
use of org.ballerinalang.model.types.BStructType 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;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class JSONUtils method jsonNodeToBArray.
/**
* Convert a JSON node to an array.
*
* @param arrayNode JSON to convert
* @param targetArrayType Type of the target array
* @return If the provided JSON is of array type, this method will return a {@link BArrayType} containing the values
* of the JSON array. Otherwise the method will throw a {@link BallerinaException}.
*/
@SuppressWarnings("rawtypes")
private static BNewArray jsonNodeToBArray(JsonNode arrayNode, BArrayType targetArrayType) {
if (!arrayNode.isArray()) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.ARRAY), getTypeName(arrayNode));
}
BType elementType = targetArrayType.getElementType();
BRefValueArray refValueArray;
switch(elementType.getTag()) {
case TypeTags.INT_TAG:
return jsonNodeToBIntArray(arrayNode);
case TypeTags.FLOAT_TAG:
return jsonNodeToBFloatArray(arrayNode);
case TypeTags.STRING_TAG:
return jsonNodeToBStringArray(arrayNode);
case TypeTags.BOOLEAN_TAG:
return jsonNodeToBBooleanArray(arrayNode);
case TypeTags.ANY_TAG:
refValueArray = new BRefValueArray(elementType);
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode element = arrayNode.get(i);
refValueArray.add(i, (BRefType) getBValue(element));
}
return refValueArray;
default:
refValueArray = new BRefValueArray(elementType);
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode element = arrayNode.get(i);
if (elementType.getTag() == TypeTags.MAP_TAG) {
refValueArray.add(i, jsonNodeToBMap(element, (BMapType) elementType));
} else if (elementType instanceof BStructType) {
refValueArray.add(i, convertJSONNodeToStruct(element, (BStructType) elementType));
} else if (elementType instanceof BArrayType) {
refValueArray.add(i, jsonNodeToBArray(element, (BArrayType) elementType));
} else {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, elementType, getTypeName(element));
}
}
return refValueArray;
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class JSONUtils method convertStructToJSON.
/**
* Convert {@link BStruct} to {@link BJSON}.
*
* @param struct {@link BStruct} to be converted to {@link BJSON}
* @return JSON representation of the provided array
*/
@SuppressWarnings("unchecked")
public static BJSON convertStructToJSON(BStruct struct) {
BJSON bjson = new BJSON(new JsonNode(Type.OBJECT));
JsonNode jsonNode = bjson.value();
BStructType structType = (BStructType) struct.getType();
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
for (BStructType.StructField structField : structType.getStructFields()) {
String key = structField.getFieldName();
BType fieldType = structField.getFieldType();
try {
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
jsonNode.set(key, struct.getIntField(++longRegIndex));
break;
case TypeTags.FLOAT_TAG:
jsonNode.set(key, struct.getFloatField(++doubleRegIndex));
break;
case TypeTags.STRING_TAG:
jsonNode.set(key, struct.getStringField(++stringRegIndex));
break;
case TypeTags.BOOLEAN_TAG:
jsonNode.set(key, struct.getBooleanField(++booleanRegIndex) == 1);
break;
case TypeTags.BLOB_TAG:
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, BTypes.typeBlob);
default:
BValue value = struct.getRefField(++refRegIndex);
if (value == null) {
jsonNode.set(key, new BJSON(NULL).value());
} else if (value.getType().getTag() == TypeTags.MAP_TAG) {
jsonNode.set(key, convertMapToJSON((BMap<String, BValue>) value).value());
} else if (value instanceof BJSON) {
jsonNode.set(key, ((BJSON) value).value());
} else if (value instanceof BNewArray) {
jsonNode.set(key, convertArrayToJSON((BNewArray) value).value());
} else if (value instanceof BStruct) {
jsonNode.set(key, convertStructToJSON((BStruct) value).value());
} else {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, value.getType());
}
}
} catch (Exception e) {
handleError(e, key);
}
}
return bjson;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class ProgramFileReader method readFunctionInfo.
private void readFunctionInfo(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
int funcNameCPIndex = dataInStream.readInt();
UTF8CPEntry funcNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcNameCPIndex);
String funcName = funcNameUTF8Entry.getValue();
FunctionInfo functionInfo = new FunctionInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), funcNameCPIndex, funcName);
functionInfo.setPackageInfo(packageInfo);
int funcSigCPIndex = dataInStream.readInt();
UTF8CPEntry funcSigUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(funcSigCPIndex);
setCallableUnitSignature(functionInfo, funcSigUTF8Entry.getValue(), packageInfo);
int flags = dataInStream.readInt();
boolean nativeFunc = Flags.isFlagOn(flags, Flags.NATIVE);
functionInfo.setNative(nativeFunc);
String uniqueFuncName;
boolean attached = Flags.isFlagOn(flags, Flags.ATTACHED);
if (attached) {
int attachedToTypeCPIndex = dataInStream.readInt();
functionInfo.attachedToTypeCPIndex = attachedToTypeCPIndex;
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(attachedToTypeCPIndex);
functionInfo.attachedToType = typeRefCPEntry.getType();
uniqueFuncName = AttachedFunctionInfo.getUniqueFuncName(typeRefCPEntry.getType().getName(), funcName);
packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
// Update the attachedFunctionInfo
if (typeRefCPEntry.getType().getTag() == TypeTags.STRUCT_TAG) {
BStructType structType = (BStructType) typeRefCPEntry.getType();
AttachedFunctionInfo attachedFuncInfo = structType.structInfo.funcInfoEntries.get(funcName);
attachedFuncInfo.functionInfo = functionInfo;
}
} else {
uniqueFuncName = funcName;
packageInfo.addFunctionInfo(uniqueFuncName, functionInfo);
}
int workerDataChannelsLength = dataInStream.readShort();
for (int i = 0; i < workerDataChannelsLength; i++) {
readWorkerDataChannelEntries(dataInStream, packageInfo, functionInfo);
}
// Read worker info entries
readWorkerInfoEntries(dataInStream, packageInfo, functionInfo);
if (nativeFunc) {
NativeCallableUnit nativeFunction = NativeUnitLoader.getInstance().loadNativeFunction(functionInfo.getPkgPath(), uniqueFuncName);
if (nativeFunction == null) {
throw new BLangRuntimeException("native function not available " + functionInfo.getPkgPath() + ":" + uniqueFuncName);
}
functionInfo.setNativeCallableUnit(nativeFunction);
}
// Read attributes
readAttributeInfoEntries(dataInStream, packageInfo, functionInfo);
}
Aggregations