io.cdap.cdap.internal.app.runtime.plugin.CollectMacroEvaluator Maven / Gradle / Ivy
/*
* Copyright © 2016-2017 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.cdap.cdap.internal.app.runtime.plugin;
import io.cdap.cdap.api.macro.MacroEvaluator;
import io.cdap.cdap.api.macro.MacroFunction;
import io.cdap.cdap.api.macro.Macros;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A macro evaluator used strictly for collecting macro properties and functions.
*
* The evaluator is passed as an argument to a {@link MacroParser} and internally keeps
* track of macro properties and macro functions
* TODO: CDAP-6628 this currently works only for simple properties lookup and simple functions,
* nested macros wouldn't work and needs change in macroparser logic
* TODO: consolidate this with TrackingMacroEvaluator
*/
public class CollectMacroEvaluator implements MacroEvaluator {
private final Set lookupProperties;
private final Set macroFunctions;
public CollectMacroEvaluator() {
this.lookupProperties = new HashSet<>();
this.macroFunctions = new HashSet<>();
}
@Override
public String lookup(String property) {
lookupProperties.add(property);
return "";
}
@Override
public String evaluate(String macroFunction, String... arguments) {
List argumentsList = new ArrayList<>();
for (String arg : arguments) {
argumentsList.add(arg);
}
macroFunctions.add(new MacroFunction(macroFunction, argumentsList));
return "";
}
public Macros getMacros() {
return new Macros(lookupProperties, macroFunctions);
}
}