-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Replace com.github.zafarkhaja.semver.Version with a local versio…
…n parser
- Loading branch information
1 parent
d390ce8
commit 038669b
Showing
6 changed files
with
150 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
19 changes: 19 additions & 0 deletions
19
core/support/src/main/antlr/au/com/dius/pact/core/support/Version.g4
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
grammar Version; | ||
|
||
@header { | ||
package au.com.dius.pact.core.support; | ||
} | ||
|
||
version returns [ Version v ] : | ||
{ Integer major, minor, patch = null; } | ||
INT { major = $INT.int; } '.' INT { minor = $INT.int; } ('.' INT { patch = $INT.int; })? EOF { | ||
if (patch != null) { | ||
$v = new Version(major, minor, patch); | ||
} else { | ||
$v = new Version(major, minor); | ||
} | ||
} | ||
; | ||
|
||
INT : DIGIT+ ; | ||
fragment DIGIT : [0-9] ; |
89 changes: 89 additions & 0 deletions
89
core/support/src/main/java/au/com/dius/pact/core/support/Version.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package au.com.dius.pact.core.support; | ||
|
||
import com.github.michaelbull.result.Err; | ||
import com.github.michaelbull.result.Ok; | ||
import com.github.michaelbull.result.Result; | ||
import org.antlr.v4.runtime.CharStream; | ||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.TokenStream; | ||
|
||
import java.util.Objects; | ||
|
||
public class Version { | ||
private Integer major; | ||
private Integer minor; | ||
private Integer patch; | ||
|
||
public Version(Integer major, Integer minor, Integer patch) { | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
} | ||
|
||
public Version(Integer major, Integer minor) { | ||
this.major = major; | ||
this.minor = minor; | ||
} | ||
|
||
public Integer getMajor() { | ||
return major; | ||
} | ||
|
||
public void setMajor(Integer major) { | ||
this.major = major; | ||
} | ||
|
||
public Integer getMinor() { | ||
return minor; | ||
} | ||
|
||
public void setMinor(Integer minor) { | ||
this.minor = minor; | ||
} | ||
|
||
public Integer getPatch() { | ||
return patch; | ||
} | ||
|
||
public void setPatch(Integer patch) { | ||
this.patch = patch; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Version version = (Version) o; | ||
return Objects.equals(major, version.major) && | ||
Objects.equals(minor, version.minor) && | ||
Objects.equals(patch, version.patch); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(major, minor, patch); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (patch == null) { | ||
return String.format("%d.%d", major, minor); | ||
} else { | ||
return String.format("%d.%d.%d", major, minor, patch); | ||
} | ||
} | ||
|
||
public static Result<Version, Exception> parse(String version) { | ||
CharStream charStream = CharStreams.fromString(version); | ||
VersionLexer lexer = new VersionLexer(charStream); | ||
TokenStream tokens = new CommonTokenStream(lexer); | ||
VersionParser parser = new VersionParser(tokens); | ||
VersionParser.VersionContext result = parser.version(); | ||
if (result.exception != null) { | ||
return new Err(result.exception); | ||
} else { | ||
return new Ok(result.v); | ||
} | ||
} | ||
} |
This file contains 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
22 changes: 22 additions & 0 deletions
22
core/support/src/test/groovy/au/com/dius/pact/core/support/VersionParserSpec.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package au.com.dius.pact.core.support | ||
|
||
import spock.lang.Specification | ||
import org.antlr.v4.runtime.InputMismatchException | ||
|
||
class VersionParserSpec extends Specification { | ||
|
||
def 'parse full version'() { | ||
expect: | ||
Version.parse('1.2.3').component1() == new Version(1, 2, 3) | ||
} | ||
|
||
def 'parse major.minor version'() { | ||
expect: | ||
Version.parse('1.2').component1() == new Version(1, 2, null) | ||
} | ||
|
||
def 'parse invalid version'() { | ||
expect: | ||
Version.parse('lkzasdjskjdf').component2() instanceof InputMismatchException | ||
} | ||
} |