use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.
the class MagicDefineGlobal method execute.
@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
// grab the expression in '%define <assign-expression>'
String assignExpr = StringUtils.trimToEmpty(command.substring(MAGIC_DEFINE.length()));
if (StringUtils.length(assignExpr) < 1) {
return error(MAGIC_DEFINE + " missing assignment expression");
}
// the expression must be an assignment
if (!StellarAssignment.isAssignment(assignExpr)) {
return error(MAGIC_DEFINE + " expected assignment expression");
}
// execute the expression
StellarAssignment expr = StellarAssignment.from(assignExpr);
StellarResult result = executor.execute(expr.getStatement());
// execution must be successful
if (!result.isSuccess()) {
return error(MAGIC_DEFINE + " expression execution failed");
}
// expression should have a result
if (!result.getValue().isPresent()) {
return error(MAGIC_DEFINE + " expression produced no result");
}
// alter the global configuration
Object value = result.getValue().get();
executor.getGlobalConfig().put(expr.getVariable(), value);
return success(value);
}
use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.
the class DocCommand method execute.
@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
StellarResult result;
// expect ?functionName
String functionName = StringUtils.substring(command, 1);
// grab any docs for the given function
Spliterator<StellarFunctionInfo> fnIterator = executor.getFunctionResolver().getFunctionInfo().spliterator();
Optional<StellarFunctionInfo> functionInfo = StreamSupport.stream(fnIterator, false).filter(info -> StringUtils.equals(functionName, info.getName())).findFirst();
if (functionInfo.isPresent()) {
result = success(docFormat(functionInfo.get()));
} else {
result = error(String.format("No docs available for function '%s'", functionName));
}
return result;
}
use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.
the class MagicUndefineGlobal method execute.
@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
StellarResult result;
String variable = StringUtils.trimToEmpty(command.substring(MAGIC_UNDEFINE.length()));
if (StringUtils.isNotBlank(variable)) {
// remove the variable from the globals
Map<String, Object> globals = executor.getGlobalConfig();
globals.remove(variable);
result = noop();
} else {
result = error(String.format("%s expected name of global, got '%s'", MAGIC_UNDEFINE, variable));
}
return result;
}
use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.
the class AssignmentCommandTest method testAssignNull.
@Test
public void testAssignNull() {
StellarResult result = command.execute("x := NULL", executor);
// validate the result
assertTrue(result.isSuccess());
assertTrue(result.isValueNull());
// validate assignment
assertNull(executor.getState().get("x").getResult());
}
use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.
the class AssignmentCommandTest method testAssignmentWithVar.
@Test
public void testAssignmentWithVar() {
// define a variable
executor.assign("x", 10, Optional.empty());
// execute the assignment expression
StellarResult result = command.execute("y := x + 2", executor);
// validate the result
assertTrue(result.isSuccess());
assertTrue(result.getValue().isPresent());
assertEquals(12, result.getValue().get());
// validate assignment
assertEquals(10, executor.getState().get("x").getResult());
assertEquals(12, executor.getState().get("y").getResult());
}
Aggregations