Skip to content

feature: add semantic version types and test #386

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

Merged
merged 19 commits into from
Aug 19, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
* Copyright 2020, Optimizely and contributors
*
* 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.optimizely.ab.config.audience.match;

import javax.annotation.Nullable;

import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;

class GEMatch extends AttributeMatch<Number> {
Number value;

protected GEMatch(Number value) {
this.value = value;
}

@Nullable
public Boolean eval(Object attributeValue) {
try {
if(isValidNumber(attributeValue)) {
return castToValueType(attributeValue, value).doubleValue() >= value.doubleValue();
}
} catch (Exception e) {
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
*
* Copyright 2020, Optimizely and contributors
*
* 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.optimizely.ab.config.audience.match;

import javax.annotation.Nullable;

import static com.optimizely.ab.internal.AttributesUtil.isValidNumber;

class LEMatch extends AttributeMatch<Number> {
Number value;

protected LEMatch(Number value) {
this.value = value;
}

@Nullable
public Boolean eval(Object attributeValue) {
try {
if(isValidNumber(attributeValue)) {
return castToValueType(attributeValue, value).doubleValue() <= value.doubleValue();
}
} catch (Exception e) {
return null;
}
return null;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2019, Optimizely and contributors
* Copyright 2018-2020, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,11 +50,21 @@ public static MatchType getMatchType(String matchType, Object conditionValue) th
return new MatchType(matchType, new SubstringMatch((String) conditionValue));
}
break;
case "ge":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new GEMatch((Number) conditionValue));
}
break;
case "gt":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new GTMatch((Number) conditionValue));
}
break;
case "le":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LEMatch((Number) conditionValue));
}
break;
case "lt":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LTMatch((Number) conditionValue));
Expand All @@ -65,6 +75,31 @@ public static MatchType getMatchType(String matchType, Object conditionValue) th
return new MatchType(matchType, new DefaultMatchForLegacyAttributes<String>((String) conditionValue));
}
break;
case "semver_eq":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionEqualsMatch((String) conditionValue));
}
break;
case "semver_ge":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGEMatch((String) conditionValue));
}
break;
case "semver_gt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGTMatch((String) conditionValue));
}
break;
case "semver_le":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLEMatch((String) conditionValue));
}
break;
case "semver_lt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLTMatch((String) conditionValue));
}
break;
default:
throw new UnknownMatchTypeException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
*
* Copyright 2020, Optimizely and contributors
*
* 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.optimizely.ab.config.audience.match;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.optimizely.ab.internal.AttributesUtil.parseNumeric;
import static com.optimizely.ab.internal.AttributesUtil.stringIsNullOrEmpty;

public final class SemanticVersion {

private static final String BUILD_SEPERATOR = "+";
private static final String PRE_RELEASE_SEPERATOR = "-";

private final String version;

public SemanticVersion(String version) {
this.version = version;
}

public int compare(SemanticVersion targetedVersion) throws Exception {

if (targetedVersion == null || stringIsNullOrEmpty(targetedVersion.version)) {
return 0;
}

String[] targetedVersionParts = targetedVersion.splitSemanticVersion();
String[] userVersionParts = splitSemanticVersion();

for (int index = 0; index < targetedVersionParts.length; index++) {

if (userVersionParts.length <= index) {
return targetedVersion.isPreRelease() ? 1 : -1;
}
Integer targetVersionPartInt = parseNumeric(targetedVersionParts[index]);
Integer userVersionPartInt = parseNumeric(userVersionParts[index]);

if (userVersionPartInt == null) {
// Compare strings
int result = userVersionParts[index].compareTo(targetedVersionParts[index]);
if (result != 0) {
return result;
}
} else if (targetVersionPartInt != null) {
if (!userVersionPartInt.equals(targetVersionPartInt)) {
return userVersionPartInt < targetVersionPartInt ? -1 : 1;
}
} else {
return -1;
}
}

if (!targetedVersion.isPreRelease() &&
isPreRelease()) {
return -1;
}

return 0;
}

public boolean isPreRelease() {
return version.contains(PRE_RELEASE_SEPERATOR);
}

public boolean isBuild() {
return version.contains(BUILD_SEPERATOR);
}

public String[] splitSemanticVersion() throws Exception {
List<String> versionParts = new ArrayList<>();
// pre-release or build.
String versionSuffix = "";
// for example: beta.2.1
String[] preVersionParts;

// Contains white spaces
if (version.contains(" ")) { // log and throw error
throw new Exception("Semantic version contains white spaces. Invalid Semantic Version.");
}

if (isBuild() || isPreRelease()) {
String[] partialVersionParts = version.split(isPreRelease() ?
PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR);

if (partialVersionParts.length <= 1) {
// throw error
throw new Exception("Invalid Semantic Version.");
}
// major.minor.patch
String versionPrefix = partialVersionParts[0];

versionSuffix = partialVersionParts[1];

preVersionParts = versionPrefix.split("\\.");
} else {
preVersionParts = version.split("\\.");
}

if (preVersionParts.length > 3) {
// Throw error as pre version should only contain major.minor.patch version
throw new Exception("Invalid Semantic Version.");
}

for (String preVersionPart : preVersionParts) {
if (parseNumeric(preVersionPart) == null) {
throw new Exception("Invalid Semantic Version.");
}
}

Collections.addAll(versionParts, preVersionParts);
if (!stringIsNullOrEmpty(versionSuffix)) {
versionParts.add(versionSuffix);
}

return versionParts.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
* Copyright 2020, Optimizely and contributors
*
* 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.optimizely.ab.config.audience.match;

import javax.annotation.Nullable;

class SemanticVersionEqualsMatch implements Match {
String value;

protected SemanticVersionEqualsMatch(String value) {
this.value = value;
}

@Nullable
public Boolean eval(Object attributeValue) {
try {
if (this.value != null && attributeValue instanceof String) {
SemanticVersion conditionalVersion = new SemanticVersion(value);
SemanticVersion userSemanticVersion = new SemanticVersion((String) attributeValue);
return userSemanticVersion.compare(conditionalVersion) == 0;
}
} catch (Exception e) {
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
* Copyright 2020, Optimizely and contributors
*
* 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.optimizely.ab.config.audience.match;

import javax.annotation.Nullable;

class SemanticVersionGEMatch implements Match {
String value;

protected SemanticVersionGEMatch(String value) {
this.value = value;
}

@Nullable
public Boolean eval(Object attributeValue) {
try {
if (this.value != null && attributeValue instanceof String) {
SemanticVersion conditionalVersion = new SemanticVersion(value);
SemanticVersion userSemanticVersion = new SemanticVersion((String) attributeValue);
return userSemanticVersion.compare(conditionalVersion) >= 0;
}
} catch (Exception e) {
return null;
}
return null;
}
}
Loading