nstream.adapter.common.relay.Take Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-adapter-common Show documentation
Show all versions of nstream-adapter-common Show documentation
General server-side data flow templates with Swim
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nstream.adapter.common.relay;
import swim.api.agent.AgentContext;
import swim.structure.Expression;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Log;
/**
* {@code Directive} to keep only a subset of the input.
*
* This {@code Directive} offers an alternative syntax that does not utilize
* a declarative {@code Attr}:
*
{@code
* @command($data) { # implicit Take here
* ...
* }}
*/
public final class Take extends Directive {
private final Directive> child;
public Take(Directive> parent, AgentContext context, Log log, Item scope,
Expression expression, Value childStructure) {
super(parent, context, log, scope);
final Item childScope = expression.evaluate(scope);
final Record trimmed = exciseAttrValue(childStructure);
this.child = DslInterpret.createDirective(this.context, log, trimmed, childScope, this);
}
@SuppressWarnings("unchecked")
@Override
public T evaluate() throws InterpretException {
return (T) this.child.evaluate();
}
private static Record exciseAttrValue(Value childStructure) {
final Record shallowClone = Record.create(childStructure.length());
shallowClone.attr(childStructure.head().key().stringValue());
shallowClone.addAll(childStructure.tail());
return shallowClone;
}
@Override
public String toString() {
return "Take{"
+ "scope=" + this.scope + ", "
+ "child=" + this.child
+ '}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy