Skip to content

Commit

Permalink
Merge pull request #1117 from HubSpot/allow-macro-alias-in-from-tag
Browse files Browse the repository at this point in the history
Support aliasing macro function names in {% from %} tag
  • Loading branch information
jasmith-hs authored Oct 10, 2023
2 parents 15f33ad + 4a01731 commit 963ad10
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ public static boolean integrateChild(
Object val = child.getContext().getGlobalMacro(importMapping.getKey());

if (val != null) {
interpreter.getContext().addGlobalMacro((MacroFunction) val);
MacroFunction toImport = (MacroFunction) val;
if (!importMapping.getKey().equals(importMapping.getValue())) {
toImport = new MacroFunction(toImport, importMapping.getValue());
}
interpreter.getContext().addGlobalMacro(toImport);
} else {
val = child.getContext().get(importMapping.getKey());

Expand Down
38 changes: 11 additions & 27 deletions src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import com.hubspot.jinjava.tree.parse.TagToken;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Beta
Expand Down Expand Up @@ -100,11 +99,10 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
}

FromTag.integrateChild(imports, child, interpreter);
Map<String, String> newToOldImportNames = renameMacros(imports, interpreter)
.entrySet()
.stream()
.filter(e -> !e.getKey().equals(e.getValue()))
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Map<String, String> newToOldImportNames = getNewToOldWithoutMacros(
imports,
interpreter
);
if (child.getContext().getDeferredTokens().isEmpty() || output == null) {
return "";
} else if (newToOldImportNames.size() > 0) {
Expand Down Expand Up @@ -132,33 +130,19 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter
}
}

private static Map<String, String> renameMacros(
private static Map<String, String> getNewToOldWithoutMacros(
Map<String, String> oldToNewImportNames,
JinjavaInterpreter interpreter
) {
Set<String> toRemove = new HashSet<>();
Map<String, MacroFunction> macroFunctions = oldToNewImportNames
return oldToNewImportNames
.entrySet()
.stream()
.filter(e -> !e.getKey().equals(e.getValue()))
.filter(
e ->
!e.getKey().equals(e.getValue()) &&
!interpreter.getContext().containsKey(e.getKey()) &&
interpreter.getContext().isGlobalMacro(e.getKey())
interpreter.getContext().containsKey(e.getValue()) ||
!interpreter.getContext().isGlobalMacro(e.getValue())
)
.peek(entry -> toRemove.add(entry.getKey()))
.collect(
Collectors.toMap(
Map.Entry::getValue,
e -> interpreter.getContext().getGlobalMacro(e.getKey())
)
);

macroFunctions.forEach(
(key, value) ->
interpreter.getContext().addGlobalMacro(new MacroFunction(value, key))
);
toRemove.forEach(oldToNewImportNames::remove);
return oldToNewImportNames;
.collect(Collectors.toMap(Entry::getValue, Entry::getKey)); // flip order
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/FromTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public void importedContextExposesVars() {
.contains("wrap-padding: padding-left:42px;padding-right:42px");
}

@Test
public void itImportsAliasedMacroName() {
assertThat(fixture("from-alias-macro"))
.contains("wrap-spacer:")
.contains("<td height=\"42\">")
.contains("wrap-padding: padding-left:42px;padding-right:42px");
}

@Test
public void importedCycleDected() {
fixture("from-recursion");
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/tags/macrotag/from-alias-macro.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% from "pegasus-importable.jinja" import wrap_padding as wp, spacer as sp %}

wrap-padding: {{ wp }}
wrap-spacer: {{ sp() }}

0 comments on commit 963ad10

Please sign in to comment.