Search in sources :

Example 26 with StructInfo

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

the class BTestUtils method createAndGetStruct.

public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
    PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
    StructInfo structInfo = structPackageInfo.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 27 with StructInfo

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

the class Utils method createSpanStruct.

public static BStruct createSpanStruct(Context context, String spanId, String serviceName, String spanName) {
    PackageInfo observePackage = context.getProgramFile().getPackageInfo("ballerina.observe");
    StructInfo spanStructInfo = observePackage.getStructInfo("Span");
    return BLangVMStructs.createBStruct(spanStructInfo, spanId, serviceName, spanName);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 28 with StructInfo

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

the class TransactionUtils method checkTransactionCoordinatorError.

private static void checkTransactionCoordinatorError(BValue value, WorkerExecutionContext ctx, String errMsg) {
    if (value.getType().getTag() == TypeTags.STRUCT_TAG) {
        PackageInfo errorPackageInfo = ctx.programFile.getPackageInfo(PACKAGE_BUILTIN);
        StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_GENERIC_ERROR);
        if (((BStruct) value).getType().structInfo.equals(errorStructInfo)) {
            throw new BallerinaException(errMsg + ((BStruct) value).getStringField(0));
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 29 with StructInfo

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

the class OpenSecureSocket method createReturnStruct.

private BStruct createReturnStruct(Context context, SSLSocket sslSocket, ByteChannel channel) throws IOException {
    PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
    // Create ByteChannel Struct
    StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
    Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
    BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
    channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
    // Create Socket Struct
    StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
    BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
    socketStruct.setRefField(0, channelStruct);
    socketStruct.setIntField(0, sslSocket.getPort());
    socketStruct.setIntField(1, sslSocket.getLocalPort());
    socketStruct.setStringField(0, sslSocket.getInetAddress().getHostAddress());
    socketStruct.setStringField(1, sslSocket.getLocalAddress().getHostAddress());
    socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
    return socketStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) ByteChannel(java.nio.channels.ByteChannel) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)

Example 30 with StructInfo

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

the class ParseTo method execute.

@Override
public void execute(Context context) {
    String dateString = context.getStringArgument(0);
    BEnumerator pattern = (BEnumerator) context.getRefArgument(0);
    TemporalAccessor parsedDateTime;
    switch(pattern.getName()) {
        case "RFC_1123":
            parsedDateTime = DateTimeFormatter.RFC_1123_DATE_TIME.parse(dateString);
            break;
        default:
            throw new BallerinaException("failed to parse date/time string: " + dateString);
    }
    StructInfo timeZoneStructInfo = Utils.getTimeZoneStructInfo(context);
    StructInfo timeStructInfo = Utils.getTimeStructInfo(context);
    long epochTime = -1;
    String zoneId;
    try {
        epochTime = Instant.from(parsedDateTime).toEpochMilli();
        zoneId = String.valueOf(ZoneId.from(parsedDateTime));
    } catch (DateTimeException e) {
        if (epochTime < 0) {
            throw new BallerinaException("failed to parse \"" + dateString + "\" to the " + pattern.getName() + " format");
        }
        zoneId = ZoneId.systemDefault().toString();
    }
    BStruct timeStruct = Utils.createTimeStruct(timeZoneStructInfo, timeStructInfo, epochTime, zoneId);
    context.setReturnValues(timeStruct);
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BStruct(org.ballerinalang.model.values.BStruct) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) StructInfo(org.ballerinalang.util.codegen.StructInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

StructInfo (org.ballerinalang.util.codegen.StructInfo)40 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)35 BStruct (org.ballerinalang.model.values.BStruct)21 BStructType (org.ballerinalang.model.types.BStructType)6 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)3 BType (org.ballerinalang.model.types.BType)2 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)2 Test (org.testng.annotations.Test)2 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 URL (java.net.URL)1 ByteChannel (java.nio.channels.ByteChannel)1 SocketChannel (java.nio.channels.SocketChannel)1