use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.
the class JSONUtils method convertJSONNodeToStruct.
/**
* Convert a BJSON to a user defined struct.
*
* @param jsonNode JSON to convert
* @param structType Type (definition) of the target struct
* @return If the provided JSON is of object-type, this method will return a {@link BStruct} containing the values
* of the JSON object. Otherwise the method will throw a {@link BallerinaException}.
*/
public static BStruct convertJSONNodeToStruct(JsonNode jsonNode, BStructType structType) {
if (!jsonNode.isObject()) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, getComplexObjectTypeName(Type.OBJECT), getTypeName(jsonNode));
}
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
int blobRegIndex = -1;
BStruct bStruct = new BStruct(structType);
StructInfo structInfo = structType.structInfo;
boolean fieldExists;
for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) {
BType fieldType = fieldInfo.getFieldType();
String fieldName = fieldInfo.getName();
try {
fieldExists = jsonNode.has(fieldName);
JsonNode jsonValue = jsonNode.get(fieldName);
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
longRegIndex++;
if (fieldExists) {
bStruct.setIntField(longRegIndex, jsonNodeToInt(jsonValue));
}
break;
case TypeTags.FLOAT_TAG:
doubleRegIndex++;
if (fieldExists) {
bStruct.setFloatField(doubleRegIndex, jsonNodeToFloat(jsonValue));
}
break;
case TypeTags.STRING_TAG:
stringRegIndex++;
String stringVal;
if (!fieldExists) {
stringVal = "";
} else if (jsonValue.isString()) {
stringVal = jsonValue.stringValue();
} else {
stringVal = jsonValue.toString();
}
bStruct.setStringField(stringRegIndex, stringVal);
break;
case TypeTags.BOOLEAN_TAG:
booleanRegIndex++;
if (fieldExists) {
bStruct.setBooleanField(booleanRegIndex, jsonNodeToBool(jsonValue) ? 1 : 0);
}
break;
case TypeTags.UNION_TAG:
case TypeTags.STRUCT_TAG:
case TypeTags.ANY_TAG:
case TypeTags.JSON_TAG:
case TypeTags.ARRAY_TAG:
case TypeTags.MAP_TAG:
case TypeTags.NULL_TAG:
refRegIndex++;
if (fieldExists) {
bStruct.setRefField(refRegIndex, (BRefType<?>) convertJSON(jsonValue, fieldType));
}
break;
case TypeTags.FUNCTION_POINTER_TAG:
if (fieldExists) {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
}
// TODO: set the default value
bStruct.setRefField(++refRegIndex, null);
break;
case TypeTags.BLOB_TAG:
bStruct.setBlobField(++blobRegIndex, new byte[0]);
break;
default:
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, fieldName, getTypeName(jsonValue));
}
} catch (Exception e) {
handleError(e, fieldName);
}
}
return bStruct;
}
use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.
the class HttpUtil method getHttpConnectorError.
public static BStruct getHttpConnectorError(Context context, Throwable throwable) {
PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(HttpConstants.PROTOCOL_PACKAGE_HTTP);
StructInfo errorStructInfo = httpPackageInfo.getStructInfo(HttpConstants.HTTP_CONNECTOR_ERROR);
BStruct httpConnectorError = new BStruct(errorStructInfo.getType());
if (throwable.getMessage() == null) {
httpConnectorError.setStringField(0, IO_EXCEPTION_OCCURED);
} else {
httpConnectorError.setStringField(0, throwable.getMessage());
}
return httpConnectorError;
}
use of org.ballerinalang.util.codegen.StructInfo in project ballerina by ballerina-lang.
the class AbstractHTTPAction method createStruct.
/**
* Creates a ballerina struct.
*
* @param context ballerina context
* @param structName name of the struct
* @param protocolPackage package name
* @return the ballerina struct
*/
protected BStruct createStruct(Context context, String structName, String protocolPackage) {
PackageInfo httpPackageInfo = context.getProgramFile().getPackageInfo(protocolPackage);
StructInfo structInfo = httpPackageInfo.getStructInfo(structName);
BStructType structType = structInfo.getType();
return new BStruct(structType);
}
use of org.ballerinalang.util.codegen.StructInfo 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.util.codegen.StructInfo in project ballerina by ballerina-lang.
the class InitEndpoint method getConnectorError.
private static BStruct getConnectorError(Context context, Throwable throwable) {
PackageInfo grpcPackageInfo = context.getProgramFile().getPackageInfo(PROTOCOL_PACKAGE_GRPC);
StructInfo errorStructInfo = grpcPackageInfo.getStructInfo(CONNECTOR_ERROR);
BStruct grpcConnectorError = new BStruct(errorStructInfo.getType());
if (throwable.getMessage() == null) {
grpcConnectorError.setStringField(0, "Service Initialization error.");
} else {
grpcConnectorError.setStringField(0, throwable.getMessage());
}
return grpcConnectorError;
}
Aggregations