Search in sources :

Example 26 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 27 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 28 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 29 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 30 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BRunUtil method invokeStateful.

/**
 * Invoke a ballerina function with state. Need to use compileAndSetup method in BCompileUtil to use this.
 *
 * @param compileResult CompileResult instance
 * @param packageName   Name of the package to invoke
 * @param functionName  Name of the function to invoke
 * @param args          Input parameters for the function
 * @return return values of the function
 */
public static BValue[] invokeStateful(CompileResult compileResult, String packageName, String functionName, BValue[] args) {
    if (compileResult.getErrorCount() > 0) {
        throw new IllegalStateException(compileResult.toString());
    }
    ProgramFile programFile = compileResult.getProgFile();
    Debugger debugger = new Debugger(programFile);
    programFile.setDebugger(debugger);
    PackageInfo packageInfo = programFile.getPackageInfo(packageName);
    FunctionInfo functionInfo = packageInfo.getFunctionInfo(functionName);
    if (functionInfo == null) {
        throw new RuntimeException("Function '" + functionName + "' is not defined");
    }
    if (functionInfo.getParamTypes().length != args.length) {
        throw new RuntimeException("Size of input argument arrays is not equal to size of function parameters");
    }
    BValue[] response = BLangFunctions.invokeCallable(functionInfo, compileResult.getContext(), args);
    return spreadToBValueArray(response);
}
Also used : Debugger(org.ballerinalang.util.debugger.Debugger) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BValue(org.ballerinalang.model.values.BValue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Aggregations

PackageInfo (org.ballerinalang.util.codegen.PackageInfo)48 StructInfo (org.ballerinalang.util.codegen.StructInfo)35 BStruct (org.ballerinalang.model.values.BStruct)17 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)10 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)5 Debugger (org.ballerinalang.util.debugger.Debugger)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 BString (org.ballerinalang.model.values.BString)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)2 Instruction (org.ballerinalang.util.codegen.Instruction)2 PackageVarInfo (org.ballerinalang.util.codegen.PackageVarInfo)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 Test (org.testng.annotations.Test)2 PrintStream (java.io.PrintStream)1