Search in sources :

Example 6 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicListGlobalsTest method testWithNoGlobals.

@Test
public void testWithNoGlobals() {
    // get all globals
    StellarResult result = executor.execute("%globals");
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    String out = ConversionUtils.convert(result.getValue().get(), String.class);
    assertEquals("{}", out);
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 7 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicListGlobalsTest method test.

@Test
public void test() {
    // define some globals
    executor.getGlobalConfig().put("x", 2);
    // get all globals
    StellarResult result = executor.execute("%globals");
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    String out = ConversionUtils.convert(result.getValue().get(), String.class);
    assertEquals("{x=2}", out);
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 8 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicListVariablesTest method test.

@Test
public void test() {
    // define some vars
    executor.execute("x := 2 + 2");
    StellarResult result = executor.execute("%vars");
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    // validate the list of vars
    String vars = ConversionUtils.convert(result.getValue().get(), String.class);
    assertEquals("x = 4 via `2 + 2`", vars);
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 9 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicListVariablesTest method testWithNoVars.

@Test
public void testWithNoVars() {
    // there are no vars defined
    StellarResult result = executor.execute("%vars");
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    // validate the list of vars
    String vars = ConversionUtils.convert(result.getValue().get(), String.class);
    assertEquals("", vars);
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 10 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class AssignmentCommand method execute.

/**
 * Handles variable assignment.
 * @param input The assignment expression to execute.
 * @param executor A stellar execution environment.
 * @return
 */
@Override
public StellarResult execute(String input, StellarShellExecutor executor) {
    assert StellarAssignment.isAssignment(input);
    // extract the variable and assignment expression
    StellarAssignment assignment = StellarAssignment.from(input);
    String varName = assignment.getVariable();
    String varExpr = assignment.getStatement();
    // execute the stellar expression
    StellarResult result = executor.execute(varExpr);
    if (result.isSuccess()) {
        Object value = null;
        if (result.getValue().isPresent()) {
            value = result.getValue().get();
        } else if (result.isValueNull()) {
            value = null;
        }
        // variable assignment
        executor.assign(varName, value, Optional.of(varExpr));
        return result;
    } else {
        return result;
    }
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) StellarAssignment(org.apache.metron.stellar.common.StellarAssignment)

Aggregations

StellarResult (org.apache.metron.stellar.common.shell.StellarResult)28 Test (org.junit.Test)23 Optional (java.util.Optional)2 StellarAssignment (org.apache.metron.stellar.common.StellarAssignment)2 Spliterator (java.util.Spliterator)1 Function (java.util.function.Function)1 StreamSupport (java.util.stream.StreamSupport)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StellarResult.error (org.apache.metron.stellar.common.shell.StellarResult.error)1 StellarResult.success (org.apache.metron.stellar.common.shell.StellarResult.success)1 StellarShellExecutor (org.apache.metron.stellar.common.shell.StellarShellExecutor)1 StellarFunctionInfo (org.apache.metron.stellar.dsl.StellarFunctionInfo)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1