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();
}
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()));
}
}
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()));
}
}
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()));
}
}
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]));
}
}
Aggregations