-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
What is the problem?
Previous similar issue: #3385. I've added a comment there but I suspect nobody was notified as that ticket is closed, so I've created the current fresh issue.
The problem is that Duration cannot be converted into string.
It is often reproducible in Clojure REPLs because they call toString to print the expression results.
We use Duration instances in our data structures related to devops tasks and it's not convenient that such data structures cannot be printed.
Reproduction Steps
In Clojure:
-
Install
clj, e.g. viabrew install clojure/tools/clojure. -
Execute:
clj -Sdeps '{:deps {software.amazon.awscdk/aws-cdk-lib {:mvn/version "2.3.0"}}}' -e '(do (import software.amazon.awscdk.Duration) (str (software.amazon.awscdk.Duration/seconds 29)))'
In Java the same error would likely be caused by calling, say, System.out.println(myDuration).
What did you expect to happen?
No exception, duration is printed.
What actually happened?
Exception:
Execution error (JsiiException) at software.amazon.jsii.JsiiRuntime/processErrorResponse (JsiiRuntime.java:124).
Argument to Intrinsic must be a plain value object, got () => {
throw new Error('Duration.toString() was used, but .toSeconds, .toMinutes or .toDays should have been called instead');
}
Error: Argument to Intrinsic must be a plain value object, got () => {
throw new Error('Duration.toString() was used, but .toSeconds, .toMinutes or .toDays should have been called instead');
}
at new Intrinsic (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-kernel-tv6XwF/node_modules/aws-cdk-lib/core/lib/private/intrinsic.js:27:19)
at Function.asAny (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-kernel-tv6XwF/node_modules/aws-cdk-lib/core/lib/token.js:132:52)
at Function.asString (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-kernel-tv6XwF/node_modules/aws-cdk-lib/core/lib/token.js:101:69)
at Duration.toString (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-kernel-tv6XwF/node_modules/aws-cdk-lib/core/lib/duration.js:243:30)
at /private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:8248:134
at Kernel._wrapSandboxCode (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:8860:24)
at /private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:8248:107
at Kernel._ensureSync (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:8841:28)
at Kernel.invoke (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:8248:34)
at KernelHost.processRequest (/private/var/folders/6g/9k9wc34n0hv_cn_1sq78xfdc0000gn/T/jsii-java-runtime11476272039872580376/lib/program.js:9757:36)
CDK CLI Version
2.3.0 (build beaa5b2)
Framework Version
2.3.0
Node.js Version
16.8.0
OS
macOS
Language
Java
Language Version
11.0.8
Other information
Proposed fix
I think the code should be fixed in CDK because toString docstring claims the string representation will be always returned:
/**
* Returns a string representation of this `Duration` that is also a Token that cannot be successfully resolved.
* <p>
* This
* protects users against inadvertently stringifying a <code>Duration</code> object, when they should have called one of the
* <code>to*</code> methods instead.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Stable)
public @org.jetbrains.annotations.NotNull java.lang.String toString()Workaround
It is possible to override the printing of this type in Clojure, e.g.:
(defmethod print-method Duration
[x ^java.io.Writer writer]
(.write writer "Duration"))