fix: throw clear errors when constant instantiation is performed through YAML directly#5271
fix: throw clear errors when constant instantiation is performed through YAML directly#5271
Conversation
There was a problem hiding this comment.
Pull request overview
Improves YAML loading feedback by failing fast with a clearer error when a constant is mistakenly declared as a direct scalar under variables (instead of using the supported variable/constant syntax).
Changes:
- Add an explicit
IllegalArgumentExceptionfor scalar (Number/String/Boolean) variable entries during constant evaluation. - Provide an in-message pointer to the YAML spec and a suggested workaround snippet.
| """ | ||
| Constant creation failed: '$name' with value '$root'. | ||
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | ||
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable |
There was a problem hiding this comment.
The exception message links to .../reference/yaml/index.html#variable, but the docs in this repo reference the variables section as /reference/yaml/#variables (e.g., site/content/howtos/simulation/variables/_index.md:10). Please update the URL/anchor so the guidance doesn’t send users to a dead/incorrect page.
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | |
| > https://alchemistsimulator.github.io/reference/yaml/#variables |
| fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException( | ||
| """ | ||
| Constant creation failed: '$name' with value '$root'. | ||
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | ||
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | ||
| Likely workaround: | ||
| ```yaml | ||
| $name: | ||
| formula: > | ||
| $root | ||
| ``` | ||
| """.trimIndent(), | ||
| ) |
There was a problem hiding this comment.
The suggested workaround snippet uses formula: > $root, but visitDependentVariable treats String formulas as JSR-223 code (not string literals). If $root is a YAML String, following this advice will likely evaluate as an identifier and fail unless it’s quoted as a language literal. Consider tailoring the message by type (e.g., numeric/list constants can use a non-string formula, while string constants need explicit quoting guidance).
| fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException( | |
| """ | |
| Constant creation failed: '$name' with value '$root'. | |
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | |
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | |
| Likely workaround: | |
| ```yaml | |
| $name: | |
| formula: > | |
| $root | |
| ``` | |
| """.trimIndent(), | |
| ) | |
| fun formulaLiteral(value: Any?): String = when (value) { | |
| is String -> "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"" | |
| else -> value.toString() | |
| } | |
| fun unsupportedConstantInstantiation(): Nothing { | |
| val suggestedFormula = formulaLiteral(root) | |
| throw IllegalArgumentException( | |
| """ | |
| Constant creation failed: '$name' with value '$root'. | |
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | |
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | |
| Likely workaround: | |
| ```yaml | |
| $name: | |
| formula: > | |
| $suggestedFormula | |
| ``` | |
| """.trimIndent(), | |
| ) | |
| } |
| val constant: Result<Constant<*>>? = when (root) { | ||
| is Number -> unsupportedConstantInstantiation() | ||
| is String -> unsupportedConstantInstantiation() | ||
| is Boolean -> unsupportedConstantInstantiation() |
There was a problem hiding this comment.
This change introduces a new hard failure for scalar entries under variables (Number/String/Boolean). There are existing YAML-loading regression tests for variables/constants (e.g., alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestKtVariable.kt); please add a regression test that loads a YAML with variables: { someConst: 3 } (and/or a scalar string/boolean) and asserts that the thrown exception is the new, user-friendly one.
05c32ce to
719bfb1
Compare
…ugh YAML directly
719bfb1 to
4e91b32
Compare
|



No description provided.