Skip to content

Commit

Permalink
[#675] JavaTime dialects: Fix native types for Kotlin and Python
Browse files Browse the repository at this point in the history
  • Loading branch information
opatrascoiu committed Jun 20, 2024
1 parent 99c4a27 commit a13a700
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class JavaTimeKotlinNativeTypeFactory extends KotlinTypeFactory {
FEEL_TYPE_TO_JAVA_TYPE.put(DATE.getName(), java.time.LocalDate.class.getName());
FEEL_TYPE_TO_JAVA_TYPE.put(STRING.getName(), String.class.getSimpleName());
FEEL_TYPE_TO_JAVA_TYPE.put(BOOLEAN.getName(), Boolean.class.getSimpleName());
FEEL_TYPE_TO_JAVA_TYPE.put(NUMBER.getName(), java.math.BigDecimal.class.getName());
FEEL_TYPE_TO_JAVA_TYPE.put(NUMBER.getName(), java.lang.Number.class.getName());
FEEL_TYPE_TO_JAVA_TYPE.put(ANY.getName(), "kotlin.Any");
FEEL_TYPE_TO_JAVA_TYPE.put(NULL.getName(), "kotlin.Any");
}
Expand All @@ -53,7 +53,7 @@ public class JavaTimeKotlinNativeTypeFactory extends KotlinTypeFactory {
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(DATE.getName(), java.time.LocalDate.class.getName());
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(STRING.getName(), String.class.getName());
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(BOOLEAN.getName(), Boolean.class.getName());
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NUMBER.getName(), java.math.BigDecimal.class.getName());
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NUMBER.getName(), java.lang.Number.class.getName());
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(ANY.getName(), "kotlin.Any");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(NULL.getName(), "kotlin.Any");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package com.gs.dmn.feel.synthesis.type;

import java.math.BigDecimal;

public abstract class JavaTypeFactory implements NativeTypeFactory {
@Override
public String nullableType(String type) {
Expand All @@ -24,8 +26,12 @@ public String constructorOfGenericType(String typeName, String... typeParameters
}

@Override
public String javaClass(String className) {
public String classOf(String className) {
return String.format("%s.class", className);
}

@Override
public String getNativeNumberConcreteType() {
return BigDecimal.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.gs.dmn.feel.synthesis.type;

import java.math.BigDecimal;
import java.util.Arrays;

public abstract class KotlinTypeFactory implements NativeTypeFactory {
Expand All @@ -30,7 +31,12 @@ public String constructorOfGenericType(String typeName, String... typeParameters
}

@Override
public String javaClass(String className) {
public String classOf(String className) {
return String.format("%s::class.java", className);
}

@Override
public String getNativeNumberConcreteType() {
return BigDecimal.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import com.gs.dmn.feel.analysis.semantics.type.*;

import java.math.BigDecimal;

public interface NativeTypeFactory {
String toNativeType(String feelType);

Expand All @@ -25,17 +23,15 @@ public interface NativeTypeFactory {

String constructorOfGenericType(String typeName, String... typeParameters);

String javaClass(String type);
String classOf(String type);

//
// Types
//
default String getNativeNumberType() {
return toNativeType(NumberType.NUMBER.getName());
}
default String getNativeNumberConcreteType() {
return BigDecimal.class.getName();
}
String getNativeNumberConcreteType();

default String getNativeDateType() {
return toNativeType(DateType.DATE.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public String constructorOfGenericType(String typeName, String... typeParameters
}

@Override
public String javaClass(String className) {
public String classOf(String className) {
return String.format("%s.__class__", className);
}

@Override
public String getNativeNumberConcreteType() {
return getNativeNumberType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2302,12 +2302,12 @@ public String getNativeDurationType() {

@Override
public String getDefaultIntegerValue() {
return this.nativeFactory.constructor(getNativeNumberType(), "\"0\"");
return this.nativeFactory.constructor(this.nativeTypeFactory.getNativeNumberConcreteType(), "\"0\"");
}

@Override
public String getDefaultDecimalValue() {
return this.nativeFactory.constructor(getNativeNumberType(), "\"0.0\"");
return this.nativeFactory.constructor(this.nativeTypeFactory.getNativeNumberConcreteType(), "\"0.0\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ private String javaFunctionToNative(JavaFunctionInfo javaInfo, FunctionType func
String javaInfoArg = this.dmnTransformer.constructor(this.dmnTransformer.javaFunctionInfoClassName(), javaInfoArgs);
String returnType = this.dmnTransformer.toNativeType(functionType.getReturnType());
String className = this.nativeTypeFactory.constructorOfGenericType(this.dmnTransformer.javaExternalFunctionClassName(), returnType);
String javaClassOfReturnType = this.nativeTypeFactory.javaClass(returnType);
String javaClassOfReturnType = this.nativeTypeFactory.classOf(returnType);
return this.dmnTransformer.constructor(className, String.format("%s, %s, %s", javaInfoArg, this.dmnTransformer.externalExecutorVariableName(), javaClassOfReturnType));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
* Copyright 2016 Goldman Sachs.
*
* 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.gs.dmn.feel.synthesis;

import com.gs.dmn.AbstractTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2016 Goldman Sachs.
*
* 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.gs.dmn.feel.synthesis.type;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class JavaNativeTypeFactoryTest {
private final NativeTypeFactory typeFactory = new JavaTimeNativeTypeFactory();

@Test
public void testToNativeType() {
assertEquals("java.lang.Number", typeFactory.toNativeType("number"));
assertEquals("String", typeFactory.toNativeType("string"));
assertEquals("Boolean", typeFactory.toNativeType("boolean"));
}

@Test
public void testToQualifiedNativeType() {
assertEquals("java.lang.Number", typeFactory.toQualifiedNativeType("number"));
assertEquals("java.lang.String", typeFactory.toQualifiedNativeType("string"));
assertEquals("java.lang.Boolean", typeFactory.toQualifiedNativeType("boolean"));
}

@Test
public void testNullableType() {
assertEquals("A", typeFactory.nullableType("A"));
}

@Test
public void testConstructorOfGenericType() {
assertEquals("A<>", typeFactory.constructorOfGenericType("A", "a", "b"));
}

@Test
public void testClassOf() {
assertEquals("A.class", typeFactory.classOf("A"));
}

@Test
public void testGetNativeNumberType() {
assertEquals("java.lang.Number", typeFactory.getNativeNumberType());
}

@Test
public void testGetNativeNumberConcreteType() {
assertEquals("java.math.BigDecimal", typeFactory.getNativeNumberConcreteType());
}

@Test
public void testGetNativeDateType() {
assertEquals("java.time.LocalDate", typeFactory.getNativeDateType());
}

@Test
public void testGetNativeTimeType() {
assertEquals("java.time.temporal.TemporalAccessor", typeFactory.getNativeTimeType());
}

@Test
public void testGetNativeDateAndTimeType() {
assertEquals("java.time.temporal.TemporalAccessor", typeFactory.getNativeDateAndTimeType());
}

@Test
public void testGetNativeDurationType() {
assertEquals("java.time.temporal.TemporalAmount", typeFactory.getNativeDurationType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2016 Goldman Sachs.
*
* 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.gs.dmn.feel.synthesis.type;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class KotlinNativeTypeFactoryTest {
private final NativeTypeFactory typeFactory = new JavaTimeKotlinNativeTypeFactory();

@Test
public void testToNativeType() {
assertEquals("java.lang.Number", typeFactory.toNativeType("number"));
assertEquals("String", typeFactory.toNativeType("string"));
}

@Test
public void testToQualifiedNativeType() {
assertEquals("java.lang.Number", typeFactory.toQualifiedNativeType("number"));
}

@Test
public void testNullableType() {
assertEquals("A?", typeFactory.nullableType("A"));
}

@Test
public void testConstructorOfGenericType() {
assertEquals("A<a, b>", typeFactory.constructorOfGenericType("A", "a", "b"));
}

@Test
public void testClassOf() {
assertEquals("A::class.java", typeFactory.classOf("A"));
}

@Test
public void testGetNativeNumberType() {
assertEquals("java.lang.Number", typeFactory.getNativeNumberType());
}

@Test
public void testGetNativeNumberConcreteType() {
assertEquals("java.math.BigDecimal", typeFactory.getNativeNumberConcreteType());
}

@Test
public void testGetNativeDateType() {
assertEquals("java.time.LocalDate", typeFactory.getNativeDateType());
}

@Test
public void testGetNativeTimeType() {
assertEquals("java.time.temporal.TemporalAccessor", typeFactory.getNativeTimeType());
}

@Test
public void testGetNativeDateAndTimeType() {
assertEquals("java.time.temporal.TemporalAccessor", typeFactory.getNativeDateAndTimeType());
}

@Test
public void testGetNativeDurationType() {
assertEquals("java.time.temporal.TemporalAmount", typeFactory.getNativeDurationType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2016 Goldman Sachs.
*
* 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.gs.dmn.feel.synthesis.type;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class PythonNativeTypeFactoryTest {
private final NativeTypeFactory typeFactory = new StandardNativeTypeToPythonFactory();

@Test
public void testToNativeType() {
assertEquals("decimal.Decimal", typeFactory.toNativeType("number"));
assertEquals("str", typeFactory.toNativeType("string"));
assertEquals("bool", typeFactory.toNativeType("boolean"));
}

@Test
public void testToQualifiedNativeType() {
assertEquals("decimal.Decimal", typeFactory.toQualifiedNativeType("number"));
assertEquals("str", typeFactory.toQualifiedNativeType("string"));
assertEquals("bool", typeFactory.toQualifiedNativeType("boolean"));
}

@Test
public void testNullableType() {
assertEquals("typing.Optional[A]", typeFactory.nullableType("A"));
}

@Test
public void testConstructorOfGenericType() {
assertEquals("A", typeFactory.constructorOfGenericType("A", "a", "b"));
}

@Test
public void testClassOf() {
assertEquals("A.__class__", typeFactory.classOf("A"));
}

@Test
public void testGetNativeNumberType() {
assertEquals("decimal.Decimal", typeFactory.getNativeNumberType());
}

@Test
public void testGetNativeNumberConcreteType() {
assertEquals("decimal.Decimal", typeFactory.getNativeNumberConcreteType());
}

@Test
public void testGetNativeDateType() {
assertEquals("datetime.date", typeFactory.getNativeDateType());
}

@Test
public void testGetNativeTimeType() {
assertEquals("datetime.time", typeFactory.getNativeTimeType());
}

@Test
public void testGetNativeDateAndTimeType() {
assertEquals("datetime.datetime", typeFactory.getNativeDateAndTimeType());
}

@Test
public void testGetNativeDurationType() {
assertEquals("datetime.timedelta", typeFactory.getNativeDurationType());
}
}
Loading

0 comments on commit a13a700

Please sign in to comment.