Skip to content

Commit

Permalink
Add global prop to context
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeoh committed May 29, 2020
1 parent 07d9555 commit e2cd95f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/Jinjava.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Jinjava() {
*/
public Jinjava(JinjavaConfig globalConfig) {
this.globalConfig = globalConfig;
this.globalContext = new Context();
this.globalContext = new Context(true);

Properties expConfig = new Properties();
expConfig.setProperty(
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
public class Context extends ScopeMap<String, Object> {
public static final String GLOBAL_MACROS_SCOPE_KEY = "__macros__";
public static final String IMPORT_RESOURCE_PATH_KEY = "import_resource_path";
private final boolean global;

private SetMultimap<String, String> dependencies = HashMultimap.create();
private Map<Library, Set<String>> disabled;
Expand All @@ -63,7 +64,7 @@ public enum Library {
EXP_TEST,
FILTER,
FUNCTION,
TAG
TAG,
}

private final CallStack extendPathStack;
Expand Down Expand Up @@ -95,25 +96,38 @@ public enum Library {
private boolean validationMode = false;

public Context() {
this(null, null, null);
this(null, null, null, false);
}

public Context(boolean global) {
this(null, null, null, global);
}

public Context(Context parent) {
this(parent, null, null);
this(parent, null, null, false);
}

public Context(Context parent, Map<String, ?> bindings) {
this(parent, bindings, null);
this(parent, bindings, null, false);
}

public Context(
Context parent,
Map<String, ?> bindings,
Map<Library, Set<String>> disabled
) {
this(parent, bindings, disabled, false);
}

private Context(
Context parent,
Map<String, ?> bindings,
Map<Library, Set<String>> disabled,
boolean global
) {
super(parent);
this.disabled = disabled;

this.global = global;
if (bindings != null) {
this.putAll(bindings);
}
Expand Down Expand Up @@ -272,7 +286,8 @@ public void addResolvedFunction(String function) {
public void handleDeferredNode(Node node) {
deferredNodes.add(node);
Set<String> deferredProps = DeferredValueUtils.findAndMarkDeferredProperties(this);
if (getParent() != null) {

if (getParent() != null && !getParent().isGlobalContext()) {
Context parent = getParent();
//Place deferred values on the parent context
deferredProps
Expand Down Expand Up @@ -523,4 +538,8 @@ public void addDependencies(SetMultimap<String, String> dependencies) {
public SetMultimap<String, String> getDependencies() {
return this.dependencies;
}

public boolean isGlobalContext() {
return global;
}
}

0 comments on commit e2cd95f

Please sign in to comment.