Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions sls-versions/src/main/java/com/palantir/sls/versions/MatchResult.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import static com.palantir.logsafe.Preconditions.checkArgument;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.regex.Matcher;
import org.immutables.value.Value;

@Value.Immutable
Expand All @@ -41,16 +44,16 @@ public static Optional<NonOrderableSlsVersion> safeValueOf(String value) {
return Optional.empty();
}

MatchResult groups = SlsVersionType.NON_ORDERABLE.getParser().tryParse(value);
if (groups == null) {
Matcher matcher = SlsVersionType.NON_ORDERABLE.getPattern().matcher(value);
if (!matcher.matches()) {
return Optional.empty();
}

return Optional.of(new NonOrderableSlsVersion.Builder()
.value(value)
.majorVersionNumber(groups.groupAsInt(1))
.minorVersionNumber(groups.groupAsInt(2))
.patchVersionNumber(groups.groupAsInt(3))
.majorVersionNumber(groupAsInt(value, matcher, 1))
.minorVersionNumber(groupAsInt(value, matcher, 2))
.patchVersionNumber(groupAsInt(value, matcher, 3))
.build());
}

Expand Down Expand Up @@ -82,5 +85,22 @@ public final String toString() {
return getValue();
}

private static int groupAsInt(String value, Matcher matcher, int group) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extracted out from RegexMatchResult, since it felt unnecessary to have the entire class + the parsers just for this (especially since NonOrderableSlsVersion is the less common usecase).

I'll move this to a separate util class though

int groupStart = matcher.start(group);
int groupEnd = matcher.end(group);
if (groupStart == -1) {
throw new NumberFormatException();
}

int integer = Integer.parseUnsignedInt(value, groupStart, groupEnd, 10);
if (integer < 0) {
throw new SafeIllegalStateException(
"Can't parse segment as integer as it overflowed",
SafeArg.of("string", value),
SafeArg.of("segment", value.substring(groupStart, groupEnd)));
}
return integer;
}

public static final class Builder extends ImmutableNonOrderableSlsVersion.Builder {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,7 @@ public static Optional<OrderableSlsVersion> safeValueOf(String value) {
return Optional.empty();
}

MatchResult groups = SlsVersionType.RELEASE.getParser().tryParse(value);
if (groups != null) {
return Optional.of(new Builder()
.value(value)
.type(SlsVersionType.RELEASE)
.majorVersionNumber(groups.groupAsInt(1))
.minorVersionNumber(groups.groupAsInt(2))
.patchVersionNumber(groups.groupAsInt(3))
.build());
}

groups = SlsVersionType.RELEASE_CANDIDATE.getParser().tryParse(value);
if (groups != null) {
return Optional.of(new Builder()
.value(value)
.type(SlsVersionType.RELEASE_CANDIDATE)
.majorVersionNumber(groups.groupAsInt(1))
.minorVersionNumber(groups.groupAsInt(2))
.patchVersionNumber(groups.groupAsInt(3))
.rcNumber(groups.groupAsInt(4))
.build());
}

groups = SlsVersionType.RELEASE_CANDIDATE_SNAPSHOT.getParser().tryParse(value);
if (groups != null) {
return Optional.of(new Builder()
.value(value)
.type(SlsVersionType.RELEASE_CANDIDATE_SNAPSHOT)
.majorVersionNumber(groups.groupAsInt(1))
.minorVersionNumber(groups.groupAsInt(2))
.patchVersionNumber(groups.groupAsInt(3))
.rcNumber(groups.groupAsInt(4))
.snapshotNumber(groups.groupAsInt(5))
.build());
}

groups = SlsVersionType.RELEASE_SNAPSHOT.getParser().tryParse(value);
if (groups != null) {
return Optional.of(new Builder()
.value(value)
.type(SlsVersionType.RELEASE_SNAPSHOT)
.majorVersionNumber(groups.groupAsInt(1))
.minorVersionNumber(groups.groupAsInt(2))
.patchVersionNumber(groups.groupAsInt(3))
.snapshotNumber(groups.groupAsInt(4))
.build());
}

return Optional.empty();
return Optional.ofNullable(OrderableSlsVersionParser.INSTANCE.tryParse(value));
}

/** Returns true iff the given coordinate has a version which can be parsed into a valid orderable SLS version. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.sls.versions;

import com.palantir.logsafe.Preconditions;
import javax.annotation.Nullable;

enum OrderableSlsVersionParser {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should add some javadoc here (even though it was missing from ReleaseVersionParser)

INSTANCE;

@Nullable
@SuppressWarnings("checkstyle:CyclomaticComplexity")
public OrderableSlsVersion tryParse(String string) {
long state = Parsers.number(string, 0);
if (Parsers.failed(state)) {
// Doesn't start with a number
return null;
}
int major = Parsers.getResult(state);

state = Parsers.literalDot(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a dot after the major version number
return null;
}

state = Parsers.number(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a minor version number
return null;
}
int minor = Parsers.getResult(state);

state = Parsers.literalDot(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a dot after the minor version number
return null;
}

state = Parsers.number(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a patch version number
return null;
}
int patch = Parsers.getResult(state);

if (Parsers.getIndex(state) == string.length()) {
// Successfully reached the end of the string after parsing major.minor.patch
return new OrderableSlsVersion.Builder()
.value(string)
.type(SlsVersionType.RELEASE)
.majorVersionNumber(major)
.minorVersionNumber(minor)
.patchVersionNumber(patch)
.build();
}

int rc = -1;
boolean isRc = false;
// Store the state in a different variable, so the index doesn't get moved if -rc is not present
long rcState = Parsers.literalDashRc(string, Parsers.getIndex(state));
if (Parsers.isOk(rcState)) {
// Found -rc, so this is either a release candidate or release candidate snapshot
isRc = true;
rcState = Parsers.number(string, Parsers.getIndex(rcState));
if (Parsers.failed(rcState)) {
// Doesn't have a number after -rc
return null;
}
rc = Parsers.getResult(rcState);
// Move index past the rc part, to parse snapshot if present
state = rcState;
}

if (Parsers.getIndex(state) == string.length()) {
// Successfully reached the end of the string after parsing major.minor.patch-rcX
// If isRc is false, this means we didn't have -rc after the patch, and state is the same as before
// which we already checked isn't with an index at string.length(), therefor this shouldn't happen
Preconditions.checkState(isRc, "Unexpected error - rc should be present");
return new OrderableSlsVersion.Builder()
.value(string)
.type(SlsVersionType.RELEASE_CANDIDATE)
.majorVersionNumber(major)
.minorVersionNumber(minor)
.patchVersionNumber(patch)
.rcNumber(rc)
.build();
}

state = Parsers.literalDash(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a dash after either the patch version number or the rc number
return null;
}

state = Parsers.number(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a snapshot number after the dash
return null;
}
int snapshot = Parsers.getResult(state);

state = Parsers.literalDashG(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have -g after the snapshot number
return null;
}

state = Parsers.hexString(string, Parsers.getIndex(state));
if (Parsers.failed(state)) {
// Doesn't have a hex string after -g
return null;
}

if (Parsers.getIndex(state) < string.length()) {
// There are still some trailing characters after parsing the whole version
return null;
}

if (isRc) {
return new OrderableSlsVersion.Builder()
.value(string)
.type(SlsVersionType.RELEASE_CANDIDATE_SNAPSHOT)
.majorVersionNumber(major)
.minorVersionNumber(minor)
.patchVersionNumber(patch)
.rcNumber(rc)
.snapshotNumber(snapshot)
.build();
} else {
return new OrderableSlsVersion.Builder()
.value(string)
.type(SlsVersionType.RELEASE_SNAPSHOT)
.majorVersionNumber(major)
.minorVersionNumber(minor)
.patchVersionNumber(patch)
.snapshotNumber(snapshot)
.build();
}
}
}
Loading