Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Reclass reference interpolation #19

Merged
merged 8 commits into from
Sep 12, 2023
Merged

Conversation

simu
Copy link
Member

@simu simu commented Sep 7, 2023

This PR introduces methods Mapping::interpolate() and Value::interpolate(), which use Token::parse() to parse Reclass references, and then call Token::render() to interpolate the references.

Additionally, the PR introduces methods Value::rendered(), Value::render() and Value::render_with_self() which users should use when wanting to interpolate Reclass references and flatten ValueLists in a Value.

The PR also renames method Node::flatten_parameters() to Node::render_parameters() and extends the method to also interpolate all references by calling Value::render_with_self() on the node's parameters.

Internally, the PR introduces a method Value::raw_string() (see 3c7e449) which produces a stringified version of the Value which is suitable for looking up Reclass reference values and can be used to emit complex values (Mappings and Sequences) for references which are embedded in strings (e.g. using a reference in a multi-line string which is used as a YAML or JSON config file). Complex values are serialized as JSON by the function, while simple values are serialized in a format that matches Python's str().

Additionally, we restructure the refs module to better match the structure of the implementation, see a42b0a0, and introduce a new method Token::parse() (cf. 236c238) to parse an arbitrary string which may contain Reclass references into a Token.

Checklist

  • The PR has a meaningful title. The title will be used to auto generate the changelog
  • PR contains a single logical change (to build a better changelog).
  • Update the documentation.
  • Update tests.
  • Categorize the PR by setting a good title and adding one of the labels:
    bug, enhancement, documentation, change, breaking, dependency, internal
    as they show up in the changelog

@simu simu added the enhancement New feature or request label Sep 7, 2023
@simu simu changed the title Feat/interpolate refs Add support for Reclass reference interpolation Sep 7, 2023
@simu simu force-pushed the feat/interpolate-refs branch 2 times, most recently from ba4fd5c to 844b12c Compare September 7, 2023 11:24
@simu simu force-pushed the feat/cleanup-anyhow-format branch from 257203a to 44131b9 Compare September 7, 2023 12:23
@simu simu force-pushed the feat/interpolate-refs branch from 844b12c to 2b781ab Compare September 7, 2023 13:39
@simu simu force-pushed the feat/cleanup-anyhow-format branch from 44131b9 to b721fc0 Compare September 8, 2023 12:02
@simu simu force-pushed the feat/interpolate-refs branch from 2b781ab to 9fc5634 Compare September 8, 2023 12:02
@simu simu force-pushed the feat/cleanup-anyhow-format branch 2 times, most recently from 6a15bf8 to cf4a759 Compare September 8, 2023 17:22
Base automatically changed from feat/cleanup-anyhow-format to main September 8, 2023 17:30
@simu simu force-pushed the feat/interpolate-refs branch 3 times, most recently from 8b23c25 to 29b7e08 Compare September 8, 2023 19:10
@simu simu changed the base branch from main to feat/load-classes September 8, 2023 19:11
@simu simu force-pushed the feat/load-classes branch 3 times, most recently from 0c8b5ce to 2b7cd26 Compare September 8, 2023 19:41
@simu simu force-pushed the feat/interpolate-refs branch 2 times, most recently from fd3a3b0 to 9f4f423 Compare September 8, 2023 19:47
@simu simu force-pushed the feat/load-classes branch from 2b7cd26 to fc05daf Compare September 8, 2023 19:59
@simu simu force-pushed the feat/interpolate-refs branch from 9f4f423 to 23ed4a9 Compare September 8, 2023 19:59
@simu simu force-pushed the feat/load-classes branch from fc05daf to dd45fd2 Compare September 8, 2023 20:39
@simu simu force-pushed the feat/interpolate-refs branch from 23ed4a9 to 6cf1747 Compare September 8, 2023 20:39
Base automatically changed from feat/load-classes to main September 11, 2023 09:05
@simu simu force-pushed the feat/interpolate-refs branch from 6cf1747 to 3a7e144 Compare September 11, 2023 09:07
Copy link

@bastjan bastjan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Took a while to understand the interpolate and resolve part.

This method generates strings which are suitable for looking up values
in the Reclass parameters during reference interpolation. The
implementation tries to match Python's `str()` formatting where
necessary (e.g. boolean keys and values).
We leave the tests associated with `enum Token` in a separate source
file which we rename to `refs/token_tests.rs`. Additionally, we move the
tests for the `parse_ref()` function to a separate source file.
This commit introduces methods `Mapping::interpolate()` and
`Value::interpolate()`, which use `Token::parse()` to parse Reclass
references, and then call `Token::render()` to interpolate the
references.

The commit also renames method `Node::flatten_parameters()` to
`Node::render_parameters()` and extends the method to also interpolate
all references.
Add Rust and Python test cases for the new node
@simu simu force-pushed the feat/interpolate-refs branch from 3a7e144 to 7e20e88 Compare September 12, 2023 07:25
@simu simu merged commit a304ca0 into main Sep 12, 2023
@simu simu deleted the feat/interpolate-refs branch September 12, 2023 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants