Skip to content
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

Use convert API instead of CloneWithType #70

Merged
merged 9 commits into from
May 18, 2023
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
8 changes: 4 additions & 4 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
org = "ballerina"
name = "constraint"
version = "1.1.1"
version = "1.2.0"
authors = ["Ballerina"]
keywords = ["constraint", "validation"]
repository = "https://github.com/ballerina-platform/module-ballerina-constraint"
icon = "icon.png"
license = ["Apache-2.0"]
distribution = "2201.4.0"
distribution = "2201.5.0"

[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "constraint-native"
version = "1.1.1"
path = "../native/build/libs/constraint-native-1.1.1-SNAPSHOT.jar"
version = "1.2.0"
path = "../native/build/libs/constraint-native-1.2.0-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin"
class = "io.ballerina.stdlib.constraint.compiler.ConstraintCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/constraint-compiler-plugin-1.1.1-SNAPSHOT.jar"
path = "../compiler-plugin/build/libs/constraint-compiler-plugin-1.2.0-SNAPSHOT.jar"
3 changes: 2 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.5.0"

[[package]]
org = "ballerina"
name = "constraint"
version = "1.1.1"
version = "1.2.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "test"},
Expand Down
2 changes: 1 addition & 1 deletion build-config/resources/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["constraint", "validation"]
repository = "https://github.com/ballerina-platform/module-ballerina-constraint"
icon = "icon.png"
license = ["Apache-2.0"]
distribution = "2201.4.0"
distribution = "2201.5.0"

[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Introduce `@constriant:Date` to validate date structures](https://github.com/ballerina-platform/ballerina-standard-library/issues/3960)
- [Allow constraints on subtypes](https://github.com/ballerina-platform/ballerina-standard-library/issues/4349)

### Changed
- [Use `ValueUtils.convert` API instead of `CloneWithType` method](https://github.com/ballerina-platform/ballerina-standard-library/issues/3933)

## [1.1.0] - 2023-02-20

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
org.gradle.caching=true
group=io.ballerina.stdlib
version=1.1.1-SNAPSHOT
version=1.2.0-SNAPSHOT
puppycrawlCheckstyleVersion=8.18
slf4jVersion=1.7.30
testngVersion=7.4.0
ballerinaGradlePluginVersion=1.0.3
jacocoVersion=0.8.8

ballerinaLangVersion=2201.4.0
ballerinaLangVersion=2201.5.0

# Test dependency
stdlibTimeVersion=2.2.4
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.UnionType;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.stdlib.constraint.annotations.AbstractAnnotations;
import io.ballerina.stdlib.constraint.annotations.RecordFieldAnnotations;
import io.ballerina.stdlib.constraint.annotations.TypeAnnotations;
import org.ballerinalang.langlib.value.CloneWithType;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -49,12 +49,15 @@
public class Constraints {

public static Object validate(Object value, BTypedesc typedesc) {
Type type = typedesc.getDescribingType();

try {
Type type = typedesc.getDescribingType();
value = cloneWithTargetType(value, type);
if (value instanceof BError) {
return ErrorUtils.buildTypeConversionError((BError) value);
}
} catch (BError e) {
return ErrorUtils.buildTypeConversionError(e);
}

try {
List<String> failedConstraints = validateAfterTypeConversionInternal(value, type, SYMBOL_DOLLAR_SIGN);
if (!failedConstraints.isEmpty()) {
return ErrorUtils.buildValidationError(failedConstraints);
Expand Down Expand Up @@ -161,10 +164,8 @@ private static AbstractAnnotations getAnnotationImpl(Type type, List<String> fai
}

private static Object cloneWithTargetType(Object value, Type targetType) {
if (!TypeUtils.isSameType(TypeUtils.getType(value), targetType)) {
value = CloneWithType.convert(targetType, value);
}
return value;
return TypeUtils.isSameType(TypeUtils.getType(value), targetType) ? value
: ValueUtils.convert(value, targetType);
}

private Constraints() {
Expand Down