(GH-538) Define TypeVersion as newtype#1350
Draft
michaeltlombardi wants to merge 4 commits intoPowerShell:mainfrom
Draft
(GH-538) Define TypeVersion as newtype#1350michaeltlombardi wants to merge 4 commits intoPowerShell:mainfrom
TypeVersion as newtype#1350michaeltlombardi wants to merge 4 commits intoPowerShell:mainfrom
Conversation
790b3d3 to
f151f75
Compare
Prior to this change, the version fields for DSC used an arbitrary `String` for both resources and extensions. The generated schema for those types then reports that any string is valid and DSC must check every time it needs to process the version for whether the version is semantic or an arbitrary string. This change follows the Rust "parse, don't validate pattern" by defining a `TypeVersion` enum with the `Semantic` and `String` variants. If the version can be parsed as a semantic version, the type creates it as an instance of `TypeVersion::Semantic` and otherwise creates it as `TypeVersion::String`. The `TypeVersion` enum implements several conversion and comparison traits to make using the newtype more ergonomic. It also defines helper methods `is_semver()` and `matches_semver_req()` for easier usage. When comparing an instance of `TypeVersion`: - A semantic version is always greater than an arbitrary string version. This applies both when comparing `TypeVersion::String` instances to `TypeVersion::Semantic` and to `semver::Version`. - Arbitrary string version comparisons are case-insensitive. `Foo` and `foo` are equal but `Foo` is greater than `Bar`. - You can directly compare instances of `TypeVersion` to string slices, `String` instances, and `semver::Version` instances in addition to other instances of `TypeVersion`. - The trait implementations support using `==`, `>`, and `<` operators for easier reading. The newtype overhauls the JSON Schema for versions to help users get better validation and IntelliSense when authoring manifests. Finally, this change adds comprehensive integration tests for the newtype and its implementations as well as documentation for the type and its public methods.
Prior to this change, the shared type definitions for `TypeVersion` and `FullyQualifiedTypeName` were accessed throughout the code as `dsc_lib::types::<TypeName>`. This change converts the `types` module to private and re-exports the types from the crate root, making them accessible as `dsc_lib::<TypeName>` instead, which is more ergonomic and maps better to describing basic reusable types for DSC that aren't specific to a particular module.
Prior to this change, the `version` fields for `ResourceManifest` and `DscResource` were defined as the `String` type. This change updates those fields to use `TypeVersion` to more correctly support distinguishing between semantic versions and arbitrary string versions. This change also adds the `semantic_version()` method to `DscResource` to return an `Option` containing a reference to the semantic version if possible. This enables retrieving the semantic version more easily instead of re-parsing the version string as a semantic version. This change updates references to the `version` field across the code base as needed.
170bfe1 to
5955b07
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
This change follows the Rust "parse, don't validate pattern" by defining a
TypeVersionenum with theSemanticandStringvariants. If the version can be parsed as a semantic version, the type creates it as an instance ofTypeVersion::Semanticand otherwise creates it asTypeVersion::String.The
TypeVersionenum implements several conversion and comparison traits to make using the newtype more ergonomic. It also defines helper methodsis_semver()andmatches_semver_req()for easier usage.When comparing an instance of
TypeVersion:TypeVersion::Stringinstances toTypeVersion::Semanticand tosemver::Version.Fooandfooare equal butFoois greater thanBar.TypeVersionto string slices,Stringinstances, andsemver::Versioninstances in addition to other instances ofTypeVersion.==,>, and<operators for easier reading.The newtype overhauls the JSON Schema for versions to help users get better validation and IntelliSense when authoring manifests.
Finally, this change adds comprehensive integration tests for the newtype and its implementations as well as documentation for the type and its public methods.
PR Context
Prior to this change, the version fields for DSC used an arbitrary
Stringfor both resources and extensions. The generated schema for those types then reports that any string is valid and DSC must check every time it needs to process the version for whether the version is semantic or an arbitrary string.