From da2355da28788734ba631a28a8608e22bb8847f1 Mon Sep 17 00:00:00 2001 From: Niels Erik Jepsen Date: Tue, 12 Nov 2024 19:30:33 +0000 Subject: [PATCH 1/5] added findSubType that takes a String and a Value, and finds the Type which also corresponds to the Value valueToJsonString in JsUtils.java now uses the new method to find the correct subtype --- .../main/java/jolie/runtime/typing/Type.java | 35 ++++++++++++++++--- .../src/main/java/jolie/js/JsUtils.java | 9 ++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/jolie/src/main/java/jolie/runtime/typing/Type.java b/jolie/src/main/java/jolie/runtime/typing/Type.java index 01447290c..a1df25d39 100644 --- a/jolie/src/main/java/jolie/runtime/typing/Type.java +++ b/jolie/src/main/java/jolie/runtime/typing/Type.java @@ -21,17 +21,16 @@ package jolie.runtime.typing; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; import jolie.lang.NativeType; import jolie.lang.parse.ast.types.BasicTypeDefinition; import jolie.runtime.Value; import jolie.runtime.ValueVector; import jolie.util.Range; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; - class TypeImpl extends Type { private final Range cardinality; private final BasicType< ? > basicType; @@ -52,6 +51,21 @@ public Type findSubType( String key ) { return (subTypes != null) ? subTypes.get( key ) : null; } + public Type findSubType( String key, Value value ) { + + Type checkSubType = findSubType( key ); + if( checkSubType == null ) { + return null; + } + + try { + checkSubType.check( value ); //checks if the value is of the the given type (I think) + return checkSubType; + } catch( TypeCheckingException e ) { + return null; + } + } + @Override public Range cardinality() { return cardinality; @@ -258,6 +272,11 @@ public Type findSubType( String key ) { return (ret != null) ? ret : right.findSubType( key ); } + @Override + public Type findSubType( String key, Value value ) { + Type ret = left.findSubType( key, value ); + return (ret != null) ? ret : right.findSubType( key, value ); + } @Override public void cutChildrenFromValue( Value value ) { @@ -436,6 +455,8 @@ public boolean isVoid() { public abstract Type findSubType( String key ); + public abstract Type findSubType( String key, Value value ); + protected abstract void check( Value value, StringBuilder pathBuilder ) throws TypeCheckingException; @@ -457,6 +478,10 @@ public Type findSubType( String key ) { return linkedType.findSubType( key ); } + public Type findSubType( String key, Value value ) { + return linkedType.findSubType( key, value ); + } + public String linkedTypeName() { return linkedTypeName; } diff --git a/lib/jolie-js/src/main/java/jolie/js/JsUtils.java b/lib/jolie-js/src/main/java/jolie/js/JsUtils.java index d1e161d68..450377095 100644 --- a/lib/jolie-js/src/main/java/jolie/js/JsUtils.java +++ b/lib/jolie-js/src/main/java/jolie/js/JsUtils.java @@ -28,13 +28,13 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import jolie.runtime.Value; -import jolie.runtime.ValueVector; -import jolie.runtime.typing.Type; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; +import jolie.runtime.Value; +import jolie.runtime.ValueVector; +import jolie.runtime.typing.Type; public class JsUtils { /** @@ -110,7 +110,8 @@ public static void valueToJsonString( Value value, boolean extendedRoot, Type ty } int i = 0; for( Map.Entry< String, ValueVector > child : value.children().entrySet() ) { - final Type subType = (type != null ? type.findSubType( child.getKey() ) : null); + final Type subType = + (type != null ? type.findSubType( child.getKey(), child.getValue().first() ) : null); appendKeyColon( builder, child.getKey() ); valueVectorToJsonString( child.getValue(), builder, false, subType ); if( i++ < size - 1 ) { From 8812f3b0ddbcdde23ed58af12d472f3222653ba9 Mon Sep 17 00:00:00 2001 From: Niels Erik Jepsen Date: Tue, 12 Nov 2024 19:38:47 +0000 Subject: [PATCH 2/5] added missing @overrides --- jolie/src/main/java/jolie/runtime/typing/Type.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jolie/src/main/java/jolie/runtime/typing/Type.java b/jolie/src/main/java/jolie/runtime/typing/Type.java index a1df25d39..1f3f800da 100644 --- a/jolie/src/main/java/jolie/runtime/typing/Type.java +++ b/jolie/src/main/java/jolie/runtime/typing/Type.java @@ -51,6 +51,7 @@ public Type findSubType( String key ) { return (subTypes != null) ? subTypes.get( key ) : null; } + @Override public Type findSubType( String key, Value value ) { Type checkSubType = findSubType( key ); @@ -478,6 +479,7 @@ public Type findSubType( String key ) { return linkedType.findSubType( key ); } + @Override public Type findSubType( String key, Value value ) { return linkedType.findSubType( key, value ); } From 28df901857a8b177b4cbfad7e19634f31b83ecb3 Mon Sep 17 00:00:00 2001 From: Niels Erik Jepsen Date: Sat, 16 Nov 2024 20:28:38 +0000 Subject: [PATCH 3/5] WIP test working test semi implemented --- test/extensions/http_json.ol | 36 ++++++++++++++++++++++ test/extensions/http_json2.ol | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 test/extensions/http_json.ol create mode 100644 test/extensions/http_json2.ol diff --git a/test/extensions/http_json.ol b/test/extensions/http_json.ol new file mode 100644 index 000000000..d5e25670b --- /dev/null +++ b/test/extensions/http_json.ol @@ -0,0 +1,36 @@ +include "../AbstractTestUnit.iol" + +from .http_json2 import Iface + +interface Iface2 { + RequestResponse: AB(void)(string) + RequestResponse: BA(void)(string) +} + +outputPort Server { + Location: "socket://localhost:12345" + Protocol: http { + format = "json" + } + Interfaces: Iface2 +} + +embedded { +Jolie: + "http_json2.ol" +} + + +define doTest +{ + AB@Server()( thing1 ) + BA@Server()( thing2 ) + + if ( thing1 == thing2) { + t = "yes" + } else { + t = "not" + } + a = "{\"test\":{\"test\":\"\",\"canbeanything\":\"This should be wrapped in an array\"}}" + throw( TestFailed, "\n" + thing1 + "\n" + thing2 + "\n" + t ) +} diff --git a/test/extensions/http_json2.ol b/test/extensions/http_json2.ol new file mode 100644 index 000000000..0ff09b075 --- /dev/null +++ b/test/extensions/http_json2.ol @@ -0,0 +1,56 @@ +include "json_utils.iol" + + +type Response1: B | A +type Response2: A | B + +type A { + canbeanything*: undefined + test: string +} + +type B { + test?: A +} + +interface Iface { + RequestResponse: AB(void)(Response1) + RequestResponse: BA(void)(Response2) +} + +define makeResponse +{ + Response << { + test << { + canbeanything[0] = "This should be wrapped in an array" + test = "" + //Not wrapped because something with the same name (in this case "test") exists in both of the types (we think). + /* + curl localhost:12345/hello + {"test":{"test":"","canbeanything":"This should be wrapped in an array"}} + */ + } + } +} + + +service Bug { + execution: concurrent + inputPort ip { + location: "socket://localhost:12345" + protocol: http { + format = "json" + .contentType = "text/plain" + } + interfaces: Iface + } + main { + [AB()(Response) { + makeResponse + }] + [BA()(Response) { + makeResponse + }] + } +} + From ceea9cbe93b392321bdb04afb6eade1c73aaa677 Mon Sep 17 00:00:00 2001 From: Niels Erik Jepsen Date: Fri, 22 Nov 2024 21:04:58 +0000 Subject: [PATCH 4/5] finalized test --- test/extensions/http_json.ol | 52 ++++++++++++---- test/extensions/http_json2.ol | 56 ----------------- test/extensions/private/http_json_server.ol | 67 +++++++++++++++++++++ 3 files changed, 106 insertions(+), 69 deletions(-) delete mode 100644 test/extensions/http_json2.ol create mode 100644 test/extensions/private/http_json_server.ol diff --git a/test/extensions/http_json.ol b/test/extensions/http_json.ol index d5e25670b..239fa8d16 100644 --- a/test/extensions/http_json.ol +++ b/test/extensions/http_json.ol @@ -1,8 +1,25 @@ -include "../AbstractTestUnit.iol" +/* + * Copyright (C) 2024 Niels Erik Jepsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ -from .http_json2 import Iface +include "../AbstractTestUnit.iol" -interface Iface2 { +interface Iface { RequestResponse: AB(void)(string) RequestResponse: BA(void)(string) } @@ -12,25 +29,34 @@ outputPort Server { Protocol: http { format = "json" } - Interfaces: Iface2 + Interfaces: Iface } embedded { Jolie: - "http_json2.ol" + "private/http_json_server.ol" } +constants { + correctResponce = "{\"sameName\":{\"anArray\":[\"Should be wrapped in an array\"],\"sameName\":\"\"}}" +} define doTest { - AB@Server()( thing1 ) - BA@Server()( thing2 ) + AB@Server()( ABResponse ) + BA@Server()( BAResponse ) + + if ( ABResponse != correctResponce) { + if ( BAResponse != correctResponce) { + throw( TestFailed, "Both type AB and BA does not fit correct response." ) + } else { + throw( TestFailed, "Type AB does not fit correct response." ) + } + } - if ( thing1 == thing2) { - t = "yes" - } else { - t = "not" + if ( BAResponse != correctResponce) { + throw( TestFailed, "Type BA does not fit correct response." ) } - a = "{\"test\":{\"test\":\"\",\"canbeanything\":\"This should be wrapped in an array\"}}" - throw( TestFailed, "\n" + thing1 + "\n" + thing2 + "\n" + t ) + + } diff --git a/test/extensions/http_json2.ol b/test/extensions/http_json2.ol deleted file mode 100644 index 0ff09b075..000000000 --- a/test/extensions/http_json2.ol +++ /dev/null @@ -1,56 +0,0 @@ -include "json_utils.iol" - - -type Response1: B | A -type Response2: A | B - -type A { - canbeanything*: undefined - test: string -} - -type B { - test?: A -} - -interface Iface { - RequestResponse: AB(void)(Response1) - RequestResponse: BA(void)(Response2) -} - -define makeResponse -{ - Response << { - test << { - canbeanything[0] = "This should be wrapped in an array" - test = "" - //Not wrapped because something with the same name (in this case "test") exists in both of the types (we think). - /* - curl localhost:12345/hello - {"test":{"test":"","canbeanything":"This should be wrapped in an array"}} - */ - } - } -} - - -service Bug { - execution: concurrent - inputPort ip { - location: "socket://localhost:12345" - protocol: http { - format = "json" - .contentType = "text/plain" - } - interfaces: Iface - } - main { - [AB()(Response) { - makeResponse - }] - [BA()(Response) { - makeResponse - }] - } -} - diff --git a/test/extensions/private/http_json_server.ol b/test/extensions/private/http_json_server.ol new file mode 100644 index 000000000..3116bb1df --- /dev/null +++ b/test/extensions/private/http_json_server.ol @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2024 Niels Erik Jepsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +type BAResponse: B | A +type ABResponse: A | B + +type A { + anArray*: undefined + sameName: string +} + +type B { + sameName?: A +} + +interface Iface { + RequestResponse: AB(void)(BAResponse) + RequestResponse: BA(void)(ABResponse) +} + +define makeResponse +{ + //sameName is required to have the same name to test that the correct type is found when converting the type to json + Response << { + sameName << { + anArray[0] = "Should be wrapped in an array" + sameName = "" + } + } +} + +service json_server { + execution: concurrent + inputPort ip { + location: "socket://localhost:12345" + protocol: http { + format = "json" + .contentType = "text/plain" //< overwrite the output format to + } + interfaces: Iface + } + main { + [AB()(Response) { + makeResponse + }] + [BA()(Response) { + makeResponse + }] + } +} + From 9eaabee23454f3871c985f12ac92c3ee93534d71 Mon Sep 17 00:00:00 2001 From: Niels Erik Jepsen Date: Fri, 22 Nov 2024 21:21:45 +0000 Subject: [PATCH 5/5] remove comment --- jolie/src/main/java/jolie/runtime/typing/Type.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jolie/src/main/java/jolie/runtime/typing/Type.java b/jolie/src/main/java/jolie/runtime/typing/Type.java index 1f3f800da..a28a017f2 100644 --- a/jolie/src/main/java/jolie/runtime/typing/Type.java +++ b/jolie/src/main/java/jolie/runtime/typing/Type.java @@ -60,7 +60,7 @@ public Type findSubType( String key, Value value ) { } try { - checkSubType.check( value ); //checks if the value is of the the given type (I think) + checkSubType.check( value ); return checkSubType; } catch( TypeCheckingException e ) { return null;