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

Improve YAML model repository #4024

Merged
merged 11 commits into from Feb 4, 2024
Merged

Improve YAML model repository #4024

merged 11 commits into from Feb 4, 2024

Conversation

J-N-K
Copy link
Member

@J-N-K J-N-K commented Jan 6, 2024

Related to #3666

This improves the existing YAML model parser and repository. In detail:

  • YamlFile has been removed. This simplifies code for consumers, because they only need to implement YamlModelListener and the DTO as inheritor of YamlDTO.
  • getRootName has been removed from YamlModelListener. The information is now provided by the @YamlElementName annotation on the respective DTO class.
  • The addedModel, updatedModel and removedModel methods in YamlModelListener have been changed to accept Collection<T> instead of Collection<? extends YamlDTO> which simplifies code in the listener.
  • The YamlModelRepository has been refactored to an interface with methods for adding/updating/removing elements from a model. The implementation is YamlModelRepositoryImpl. This interface is public and can be implemented to modify an existing model or create a new one. This allows to dynamically update the content.
  • The YamlModelRepositoryImpl now parses the elements using JsonNode instead of de-serializing the file directly. JsonNodes are cached by model (file) name and type. This allows to mix different inheritors of YamlDTO in one file. Since all elements are cached as JsonNode, they can also be read before the respective YamlModelListener is registered. In addition, this removes the coupling between directory and model type.
  • Tests for the YamlModelRepository have been added.
  • A readOnly flag (defaults to true) has been added. Models without this flag (or readOnly: true) cannot be modified. This is to prevent changing user-defined models. On deserialization comments in the file are lost, so they would be missing in an updated model.

@lolodomo I would especially like to hear your comments since you did the original implementation.

Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K J-N-K added the enhancement An enhancement or new feature of the Core label Jan 6, 2024
@J-N-K J-N-K requested a review from a team as a code owner January 6, 2024 20:53
Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K J-N-K marked this pull request as draft January 7, 2024 07:33
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K J-N-K marked this pull request as ready for review January 10, 2024 16:55
@wborn wborn requested a review from lolodomo January 15, 2024 21:58
This reverts commit 1c4e6c7eb2afdc55628ca327e8cd9f7453d354a7.

Signed-off-by: Jan N. Klug <github@klug.nrw>
@J-N-K
Copy link
Member Author

J-N-K commented Jan 21, 2024

@wborn How do we proceed here?

Copy link
Member

@wborn wborn left a comment

Choose a reason for hiding this comment

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

I have only found some minor issues so far. 😉

*/
@NonNullByDefault
Copy link
Member

Choose a reason for hiding this comment

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

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

This essentially a DTO. Missing elements in the data are de-serialized as null.

If we decide to make the DTO classes @NonNullByDefault we would need to either annotate all fields with @Nullable (which is the similar to not annotating the class at all) or provide default values (which doesn't make sense in most cases).

Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Copy link
Member

@wborn wborn left a comment

Choose a reason for hiding this comment

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

Thanks for the improvements!

@wborn wborn merged commit fe4cbe5 into openhab:main Feb 4, 2024
3 checks passed
@wborn wborn added this to the 4.2 milestone Feb 4, 2024
@J-N-K J-N-K deleted the yaml2 branch February 4, 2024 09:30
@lolodomo
Copy link
Contributor

lolodomo commented Feb 4, 2024

I did not take time to have a look. Does this PR break user YAML files containing custom tags ?

@J-N-K
Copy link
Member Author

J-N-K commented Feb 4, 2024

No, I don't think so. The format is the same, just some more options added and code refactoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants