Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global prop to context #448

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -81,7 +81,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
28 changes: 23 additions & 5 deletions src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 @@ -62,7 +63,7 @@ public enum Library {
EXP_TEST,
FILTER,
FUNCTION,
TAG
TAG,
}

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

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

public Context(boolean global) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this constructor necessary? I think you could just use one of the others and pass in nulls. I find it confusing when a method with one parameter could be take two types of arguments.

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 @@ -515,4 +529,8 @@ public void addDependencies(SetMultimap<String, String> dependencies) {
public SetMultimap<String, String> getDependencies() {
return this.dependencies;
}

public boolean isGlobalContext() {
return global;
}
}