Skip to content

Commit

Permalink
add tag Map.default.as
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 13, 2020
1 parent 057cd5c commit 486520c
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,40 @@ public int compare(Map.Entry<StringHolder, ObjectTag> e1, Map.Entry<StringHolder
return output;
});

// <--[tag]
// @attribute <MapTag.default[<key>].as[<value>]>
// @returns MapTag
// @description
// Returns a copy of the map, with the specified key defaulted to the specified value.
// If the map does not already have the specified key, this is equivalent to the 'with[key].as[value]' tag.
// If the map already has the specified key, this will return the original map, unmodified.
// For example, on a map of "a/1|b/2|c/3|", using ".default[d].as[4]" will return "a/1|b/2|c/3|d/4|".
// For example, on a map of "a/1|b/2|c/3|", using ".default[c].as[4]" will return "a/1|b/2|c/3|".
// -->
registerTag("default", (attribute, object) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("The tag 'MapTag.default' must have an input value.");
return null;
}
String key = attribute.getContext(1);
attribute.fulfill(1);
if (!attribute.matches("as")) {
attribute.echoError("The tag 'MapTag.default' must be followed by '.as'.");
return null;
}
if (!attribute.hasContext(1)) {
attribute.echoError("The tag 'MapTag.default.as' must have an input value for 'as'.");
return null;
}
if (object.map.containsKey(new StringHolder(key))) {
return object;
}
ObjectTag value = attribute.getContextObject(1);
MapTag result = object.duplicate();
result.putObject(key, value);
return result;
});

// <--[tag]
// @attribute <MapTag.with[<key>].as[<value>]>
// @returns MapTag
Expand Down

0 comments on commit 486520c

Please sign in to comment.