Skip to content

Commit

Permalink
ObjectTag.as[type] tag, deprecates as_type style
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 21, 2022
1 parent d811d9a commit ba63cc3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_list>
// @returns ListTag
// @group conversion
// @deprecated use as[list]
// @description
// Returns the element as a ListTag.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerStaticTag(ListTag.class, "as_list", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, ListTag.valueOf(element, attribute.context), "ListTag", attribute.hasAlternative());
}, "aslist");
Expand All @@ -685,10 +687,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_map>
// @returns MapTag
// @group conversion
// @deprecated use as[map]
// @description
// Returns the element as a MapTag.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerStaticTag(MapTag.class, "as_map", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, MapTag.valueOf(element, attribute.context), "MapTag", attribute.hasAlternative());
});
Expand All @@ -697,10 +701,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_custom>
// @returns CustomObjectTag
// @group conversion
// @deprecated use as[custom]
// @description
// Returns the element as a custom object.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerTag(CustomObjectTag.class, "as_custom", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, CustomObjectTag.valueOf(element, attribute.context), "Custom", attribute.hasAlternative());
}, "ascustom");
Expand All @@ -709,11 +715,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_script>
// @returns ScriptTag
// @group conversion
// @deprecated use as[script]
// @description
// Returns the element as a ScriptTag.
// Note: the value must be a valid script.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerStaticTag(ScriptTag.class, "as_script", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, ScriptTag.valueOf(element, attribute.context), "ScriptTag", attribute.hasAlternative());
}, "asscript");
Expand All @@ -722,11 +729,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_queue>
// @returns QueueTag
// @group conversion
// @deprecated use as[queue]
// @description
// Returns the element as a QueueTag.
// Note: the value must be a valid QueueTag.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerTag(QueueTag.class, "as_queue", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, QueueTag.valueOf(element, attribute.context), "QueueTag", attribute.hasAlternative());
}, "asqueue");
Expand All @@ -735,11 +743,12 @@ public static void registerTags() {
// @attribute <ElementTag.as_duration>
// @returns DurationTag
// @group conversion
// @deprecated use as[duration]
// @description
// Returns the element as a Duration.
// Note: the value must be a valid Duration.
// Deprecated in favor of <@link tag ObjectTag.as>
// -->
tagProcessor.registerStaticTag(DurationTag.class, "as_duration", (attribute, object) -> {
Deprecations.asXTags.warn(attribute.context);
String element = object.element;
return handleNull(element, DurationTag.valueOf(element, attribute.context), "DurationTag", attribute.hasAlternative());
}, "asduration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,34 @@ public void generateCoreTags() {
}
return new JavaReflectedObjectTag(obj);
});

// <--[tag]
// @attribute <ObjectTag.as[<type>]>
// @returns ObjectTag
// @description
// Returns the object, automatically converted to the named object type.
// Type names can be of the long form, like "ListTag", "MapTag", "ElementTag", ... or the short form, like "List", "Map", "Element", ...
// Type name input is not case-sensitive, so "List" or "list" are the same.
// @example
// my_example:
// type: task
// # Some input can be any raw object type, but will be forced to a list using 'as[list]' below
// definitions: some_input
// script:
// - narrate "Input list has size <[some_input].as[list].size>"
// -->
registerTag(ObjectTag.class, ElementTag.class, "as", (attribute, object, asType) -> {
ObjectType<?> type = ObjectFetcher.objectsByName.get(asType.asLowerString());
if (type == null) {
attribute.echoError("Invalid object type '" + asType + "'. Cannot convert.");
return null;
}
ObjectTag result = object.asType(type, attribute.context);
if (result == null) {
attribute.echoError("Cannot convert object '" + object + "' to type '" + type.longName + "'.");
}
return result;
});
}

public void registerFutureTagDeprecation(String name, String... deprecatedVariants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class Deprecations {
// Added 2022/04/11, deprecate officially by 2024.
public static Warning prebinaryTags = new FutureWarning("prebinaryTags", "Tags and tools related to binary processing that predate the BinaryTag feature are deprecated in favor of using BinaryTag. This includes 'ElementTag.base64_encode/decode', 'hex_encode/decode', ... refer to meta-docs for specifics");

// Added 2022/08/21, deprecate officially by 2025.
public static Warning asXTags = new FutureWarning("asXTags", "Tags of the form 'as_x' where 'x' is a type, such as 'as_list', are deprecated in favor of the tag ObjectTag.as[<type>], used like 'as[list]'.");

// ==================== PAST deprecations of things that are already gone but still have a warning left behind ====================

// Removed in February 2020.
Expand Down

0 comments on commit ba63cc3

Please sign in to comment.