Search in sources :

Example 6 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    Struct clientEndpointConfig = clientEndpoint.getStructField(Constants.CLIENT_ENDPOINT_CONFIG);
    // Extract parameters from the endpoint config
    String database = clientEndpointConfig.getEnumField(Constants.EndpointConfig.DATABASE);
    String host = clientEndpointConfig.getStringField(Constants.EndpointConfig.HOST);
    int port = (int) clientEndpointConfig.getIntField(Constants.EndpointConfig.PORT);
    String name = clientEndpointConfig.getStringField(Constants.EndpointConfig.NAME);
    String username = clientEndpointConfig.getStringField(Constants.EndpointConfig.USERNAME);
    String password = clientEndpointConfig.getStringField(Constants.EndpointConfig.PASSWORD);
    Struct options = clientEndpointConfig.getStructField(Constants.EndpointConfig.OPTIONS);
    SQLDatasource datasource = new SQLDatasource();
    datasource.init(options, database, host, port, username, password, name);
    BStruct ballerinaClientConnector;
    if (clientEndpoint.getNativeData(Constants.B_CONNECTOR) != null) {
        ballerinaClientConnector = (BStruct) clientEndpoint.getNativeData(Constants.B_CONNECTOR);
    } else {
        ballerinaClientConnector = BLangConnectorSPIUtil.createBStruct(context.getProgramFile(), Constants.SQL_PACKAGE_PATH, Constants.CLIENT_CONNECTOR, database, host, port, name, username, password, options, clientEndpointConfig);
        clientEndpoint.addNativeData(Constants.B_CONNECTOR, ballerinaClientConnector);
    }
    ballerinaClientConnector.addNativeData(Constants.CLIENT_CONNECTOR, datasource);
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 7 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class FindAllWithRegex method execute.

@Override
public void execute(Context context) {
    String initialString = context.getStringArgument(0);
    BStruct regexStruct = (BStruct) context.getRefArgument(0);
    try {
        Pattern pattern = validatePattern(regexStruct);
        BStringArray stringArray = new BStringArray();
        Matcher matcher = pattern.matcher(initialString);
        int i = 0;
        while (matcher.find()) {
            stringArray.add(i++, matcher.group());
        }
        context.setReturnValues(stringArray);
    } catch (PatternSyntaxException e) {
        context.setReturnValues(BLangVMErrors.createError(context, 0, e.getMessage()));
    }
}
Also used : Pattern(java.util.regex.Pattern) BStruct(org.ballerinalang.model.values.BStruct) Matcher(java.util.regex.Matcher) BStringArray(org.ballerinalang.model.values.BStringArray) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 8 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class ReplaceAllWithRegex method execute.

@Override
public void execute(Context context) {
    String mainString = context.getStringArgument(0);
    String replaceWith = context.getStringArgument(1);
    BStruct regexStruct = (BStruct) context.getRefArgument(0);
    try {
        Pattern pattern = validatePattern(regexStruct);
        Matcher matcher = pattern.matcher(mainString);
        String replacedString = matcher.replaceAll(replaceWith);
        context.setReturnValues(new BString(replacedString));
    } catch (PatternSyntaxException e) {
        context.setReturnValues(BLangVMErrors.createError(context, 0, e.getMessage()));
    }
}
Also used : Pattern(java.util.regex.Pattern) BStruct(org.ballerinalang.model.values.BStruct) Matcher(java.util.regex.Matcher) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 9 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class ReplaceFirstWithRegex method execute.

@Override
public void execute(Context context) {
    String mainString = context.getStringArgument(0);
    String replaceWith = context.getStringArgument(1);
    BStruct regexStruct = (BStruct) context.getRefArgument(0);
    try {
        Pattern pattern = validatePattern(regexStruct);
        Matcher matcher = pattern.matcher(mainString);
        String replacedString = matcher.replaceFirst(replaceWith);
        context.setReturnValues(new BString(replacedString));
    } catch (PatternSyntaxException e) {
        context.setReturnValues(BLangVMErrors.createError(context, 0, e.getMessage()));
    }
}
Also used : Pattern(java.util.regex.Pattern) BStruct(org.ballerinalang.model.values.BStruct) Matcher(java.util.regex.Matcher) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 10 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class VectorNegativeTest method testRemoveIndexOutOfRange.

@Test(description = "Test case for testing removal of elements from outside of the bounds of a vector.")
public void testRemoveIndexOutOfRange() {
    long vectorSize = 10;
    long[] indices = new long[] { 0, vectorSize - 1, -1 };
    long[] expectedFinalValues = new long[] { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
    String[] expectedErrMsgs = new String[] { "Index out of range: 9", "Index out of range: -1" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testRemoveIndexOutOfRange", new BValue[] { buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, expectedFinalValues.length);
    for (int i = 0; i < finalVectorSize; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), expectedFinalValues[i]);
    }
    BRefValueArray removedVals = (BRefValueArray) returns[1];
    // only 1 valid index
    Assert.assertEquals(removedVals.size(), 1);
    Assert.assertEquals(((BInteger) removedVals.get(0)).intValue(), 10);
    BRefValueArray errors = (BRefValueArray) returns[2];
    Assert.assertEquals(errors.size(), expectedErrMsgs.length);
    for (int i = 0; i < expectedErrMsgs.length; i++) {
        Assert.assertTrue(errors.get(i).stringValue().contains(expectedErrMsgs[i]));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16