Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
37 changes: 32 additions & 5 deletions jolie/src/main/java/jolie/runtime/typing/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +51,22 @@ public Type findSubType( String key ) {
return (subTypes != null) ? subTypes.get( key ) : null;
}

@Override
public Type findSubType( String key, Value value ) {

Type checkSubType = findSubType( key );
if( checkSubType == null ) {
return null;
}

try {
checkSubType.check( value );
return checkSubType;
} catch( TypeCheckingException e ) {
return null;
}
}

@Override
public Range cardinality() {
return cardinality;
Expand Down Expand Up @@ -258,6 +273,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 ) {
Expand Down Expand Up @@ -436,6 +456,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;

Expand All @@ -457,6 +479,11 @@ public Type findSubType( String key ) {
return linkedType.findSubType( key );
}

@Override
public Type findSubType( String key, Value value ) {
return linkedType.findSubType( key, value );
}

public String linkedTypeName() {
return linkedTypeName;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/jolie-js/src/main/java/jolie/js/JsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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 ) {
Expand Down
62 changes: 62 additions & 0 deletions test/extensions/http_json.ol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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
*/

include "../AbstractTestUnit.iol"

interface Iface {
RequestResponse: AB(void)(string)
RequestResponse: BA(void)(string)
}

outputPort Server {
Location: "socket://localhost:12345"
Protocol: http {
format = "json"
}
Interfaces: Iface
}

embedded {
Jolie:
"private/http_json_server.ol"
}

constants {
correctResponce = "{\"sameName\":{\"anArray\":[\"Should be wrapped in an array\"],\"sameName\":\"\"}}"
}

define doTest
{
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 ( BAResponse != correctResponce) {
throw( TestFailed, "Type BA does not fit correct response." )
}


}
67 changes: 67 additions & 0 deletions test/extensions/private/http_json_server.ol
Original file line number Diff line number Diff line change
@@ -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
}]
}
}

Loading