McSherry.SemanticVersioning icon indicating copy to clipboard operation
McSherry.SemanticVersioning copied to clipboard

Converter for System.Version

Open niemyjski opened this issue 7 years ago • 4 comments

I just ran into an issue where I have to try handing 1.2.3.4, 1.2.3 hash and 1.2.3.4 hash (info versions and regular versions) to Semantic Version. It would be useful if there was a implicit conversion that converted (major.minor.build.revision) to a semantic version (major.minor.build-revision)

niemyjski avatar Jul 15 '16 21:07 niemyjski

I'd considered adding conversion operators for System.Version in the past, but I decided against this because they are semantically incompatible. Plus, implicit operators should only be used where there would be no loss of information. It isn't possible to preserve all information System.Version <-> SemanticVersion.

McSherry avatar Jul 16 '16 09:07 McSherry

Yeah, I agree. We just needed it and have added support for different use cases.


        [Theory]
        [InlineData(null, null)]
        [InlineData("1", "1.0.0")]
        [InlineData("1.2", "1.2.0")]
        [InlineData("1.2 7ab3b4da18", "1.2.0")]
        [InlineData("1.2.3", "1.2.3")]
        [InlineData("1.2.3 7ab3b4da18", "1.2.3")]
        [InlineData("1.2.3-beta2", "1.2.3-beta2")]
        [InlineData("1.2.3.*", "1.2.3")]
        [InlineData("1.2.3.0", "1.2.3-0")]
        [InlineData("1.2.3.0*", "1.2.3-0")]
        [InlineData("1.2.3*.0", "1.2.3-0")]
        [InlineData("1.2.*.0", "1.2.0")]
        [InlineData("1.2.*", "1.2.0")]
        [InlineData("1.2.3.4", "1.2.3-4")]
        [InlineData("1.2.3.4 7ab3b4da18", "1.2.3-4")]
        public async Task SemanticVersionTests(string input, string expected) {
            var actual = await _parser.ParseAsync(input);
            Assert.Equal(expected, actual?.ToString());
        }

niemyjski avatar Jul 18 '16 14:07 niemyjski

@niemyjski I plan to introduce a "greedy" parse mode (see #12). It might be useful in your application.

McSherry avatar May 31 '19 02:05 McSherry

I've decided to reopen this issue.

Although semantic versions and System.Version aren't completely compatible, I think including a converter apart from the main API (maybe under a McSherry.SemanticVersioning.Extensions namespace or similar) would be useful while avoiding cluttering the main namespaces.

McSherry avatar Jun 29 '20 13:06 McSherry