Skip to content

Preserve Short‑Form format When Generating YAML#5306

Open
jimtng wants to merge 4 commits into
openhab:mainfrom
jimtng:yaml-format-shortform
Open

Preserve Short‑Form format When Generating YAML#5306
jimtng wants to merge 4 commits into
openhab:mainfrom
jimtng:yaml-format-shortform

Conversation

@jimtng

@jimtng jimtng commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Preserve Short‑Form format When Generating YAML

openHAB already supports the short‑form format syntax for Items in YAML. For example:

items:
  itemname:
    format: '%d'

Internally, this is correctly translated into the equivalent metadata structure:

items:
  itemname:
    metadata:
      stateDescription:
        value: ''
        config:
          pattern: '%d'

However, when exporting or regenerating YAML from this internal representation, the system currently does not restore the short‑form. Instead, it always emits the full metadata block, even when the only metadata present is the pattern value that corresponds directly to the format shorthand.

This PR updates the YAML generator so that:

  • If the stateDescription metadata contains only a pattern value,
    → the YAML generator emits the concise format: '<pattern>' form.
  • If any additional stateDescription metadata is present,
    → the generator falls back to the full metadata representation.

✔️ Benefits

  • Restores symmetry between input and output YAML.
  • Keeps generated YAML clean, readable, and consistent with user‑authored files.
  • Avoids unnecessary verbosity when only the format shorthand is needed.

@jimtng jimtng marked this pull request as ready for review January 25, 2026 10:18
@jimtng jimtng requested a review from a team as a code owner January 25, 2026 10:18
@jimtng

jimtng commented Jan 25, 2026

Copy link
Copy Markdown
Contributor Author

@lolodomo, when you have a moment, could you take a look at this? I want to make sure I’ve understood the override logic correctly, especially around the injected vs. default formats. I might be missing some of the intent from the original implementation, so your insight would be really helpful.

@lolodomo

lolodomo commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

This case is very complex in particular due to the way it is managed by the DSL provider.
I remember I made a choice to be sure to not loose stuff after several conversions in both sense between YAML and DSL.

@lolodomo

Copy link
Copy Markdown
Contributor

Looking at the code, I see that I did not consider the short form for format and I am sure it was a deliberate choice. I will have to remember and explain what kind of problem it can lead to.

@jimtng

jimtng commented Jan 25, 2026

Copy link
Copy Markdown
Contributor Author

This whole default / injected stateFormatter logic seems strange to me. I would think the short form / long form should only relate to what's actually stored in the metadata and it should be done without any regard to any default / injected format from anywhere else.

@lolodomo

lolodomo commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

This whole default / injected stateFormatter logic seems strange to me. I would think the short form / long form should only relate to what's actually stored in the metadata and it should be done without any regard to any default / injected format from anywhere else.

DSL provider does not use metadata but another feature. I remember I hesitated to change the DSL provider to remove that. I did not do it because I was not 100% of the possible impacts.

@lolodomo

Copy link
Copy Markdown
Contributor

But let me remember the scenario which was a problem.
I agree your change would be of course better if only YAML is considered.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request enhances the YAML generator for openHAB to preserve the short-form format syntax when exporting items. Previously, even when items were defined with the concise format: '%d' syntax, the generator would always expand this to the full metadata.stateDescription.config.pattern structure. This PR adds logic to detect when stateDescription metadata contains only a pattern value and generates the short-form instead, maintaining symmetry between input and output YAML.

Changes:

  • Added logic to convert stateDescription metadata with only a pattern config back to short-form format field
  • Refactored metadata handling to use early continue statements for improved readability
  • Added comprehensive tests for both short-form and long-form stateDescription scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
YamlItemFileConverter.java Refactored metadata handling logic to detect and preserve short-form format syntax when appropriate; simplified control flow with early continue statements
YamlItemFileConverterTest.java Added tests to verify stateDescription metadata conversion to short-form format and long-form preservation when additional config exists

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lolodomo

Copy link
Copy Markdown
Contributor

@openhab/core-maintainers : please wait before merging, I would like to investigate more.

jimtng added 4 commits April 25, 2026 12:38
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
@jimtng jimtng force-pushed the yaml-format-shortform branch from 4f6feb7 to 920190d Compare April 25, 2026 02:39
@jimtng

jimtng commented Apr 25, 2026

Copy link
Copy Markdown
Contributor Author

rebased to resolve conflict

@lolodomo

lolodomo commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Need to find again the use case that will be problematic with DSL.
My intention is to change the DSL provider in 5.3 to allow this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants