From facfa0ae1d44ff4ed9ba8e707af0e99e9326ad99 Mon Sep 17 00:00:00 2001 From: Heshan Padmasiri Date: Mon, 29 Jul 2024 16:06:24 +0530 Subject: [PATCH] Fix record type creation --- .../stdlib/http/api/HttpResource.java | 22 +++++++++++++++++-- .../http/api/nativeimpl/ParseHeader.java | 18 ++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResource.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResource.java index ac068ba942..0f8fd0bd09 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResource.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResource.java @@ -17,10 +17,12 @@ */ package io.ballerina.stdlib.http.api; +import io.ballerina.runtime.api.PredefinedTypes; import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.creators.TypeCreator; import io.ballerina.runtime.api.creators.ValueCreator; import io.ballerina.runtime.api.types.ArrayType; +import io.ballerina.runtime.api.types.FiniteType; import io.ballerina.runtime.api.types.MapType; import io.ballerina.runtime.api.types.MethodType; import io.ballerina.runtime.api.types.RecordType; @@ -45,11 +47,13 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static io.ballerina.runtime.api.flags.SymbolFlags.OPTIONAL; import static io.ballerina.stdlib.http.api.HttpConstants.ANN_NAME_RESOURCE_CONFIG; import static io.ballerina.stdlib.http.api.HttpConstants.APPLICATION_JSON; import static io.ballerina.stdlib.http.api.HttpConstants.APPLICATION_OCTET_STREAM; @@ -82,8 +86,8 @@ public class HttpResource implements Resource { private static final BString HTTP_RESOURCE_CONFIG = StringUtils.fromString(ModuleUtils.getHttpPackageIdentifier() + ":" + ANN_NAME_RESOURCE_CONFIG); private static final String RETURN_ANNOT_PREFIX = "$returns$"; - private static final MapType LINK_MAP_TYPE = TypeCreator.createMapType(TypeCreator.createRecordType( - LINK, ModuleUtils.getHttpPackage(), 0, false, 0)); + private static final RecordType LINK_TYPE = createLinkType(); + private static final MapType LINK_MAP_TYPE = TypeCreator.createMapType(LINK_TYPE); private String resourceLinkName; private List linkedResources = new ArrayList<>(); @@ -497,6 +501,20 @@ public RemoteMethodType getRemoteFunction() { return (RemoteMethodType) balResource; } + private static RecordType createLinkType() { + FiniteType method = TypeCreator.createFiniteType("Method", + Stream.of("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS") + .map(StringUtils::fromString).collect(Collectors.toUnmodifiableSet()), + 0); + return TypeCreator.createRecordType(LINK, ModuleUtils.getHttpPackage(), 0, Map.of( + "rel", TypeCreator.createField(PredefinedTypes.TYPE_STRING, "rel", OPTIONAL), + "href", TypeCreator.createField(PredefinedTypes.TYPE_STRING, "href", 0), + "types", + TypeCreator.createField(TypeCreator.createArrayType(PredefinedTypes.TYPE_STRING), "types", OPTIONAL), + "methods", TypeCreator.createField(TypeCreator.createArrayType(method), "methods", OPTIONAL) + ), PredefinedTypes.TYPE_NEVER, false, 0); + } + /** * Linked resource information. */ diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/nativeimpl/ParseHeader.java b/native/src/main/java/io/ballerina/stdlib/http/api/nativeimpl/ParseHeader.java index 9791bc634e..7443d5a53b 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/nativeimpl/ParseHeader.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/nativeimpl/ParseHeader.java @@ -17,6 +17,7 @@ */ package io.ballerina.stdlib.http.api.nativeimpl; +import io.ballerina.runtime.api.PredefinedTypes; import io.ballerina.runtime.api.creators.TypeCreator; import io.ballerina.runtime.api.creators.ValueCreator; import io.ballerina.runtime.api.types.ArrayType; @@ -30,6 +31,8 @@ import io.ballerina.stdlib.http.api.ValueCreatorUtils; import io.ballerina.stdlib.mime.util.HeaderUtil; +import java.util.Map; + import static io.ballerina.stdlib.http.api.HttpConstants.HEADER_VALUE_FIELD; import static io.ballerina.stdlib.http.api.HttpConstants.HEADER_VALUE_PARAM_FIELD; import static io.ballerina.stdlib.http.api.HttpConstants.HEADER_VALUE_RECORD; @@ -44,9 +47,8 @@ */ public class ParseHeader { - private static final RecordType RECORD_TYPE = TypeCreator.createRecordType( - HEADER_VALUE_RECORD, ModuleUtils.getHttpPackage(), 0, false, 0); - private static final ArrayType ARRAY_TYPE = TypeCreator.createArrayType(RECORD_TYPE); + private static final RecordType HEADER_VALUE_TYPE = createHeaderValueType(); + private static final ArrayType ARRAY_TYPE = TypeCreator.createArrayType(HEADER_VALUE_TYPE); private static final String COMMA_OUT_OF_QUOTATIONS = "(,)(?=(?:[^\"]|\"[^\"]*\")*$)"; public static Object parseHeader(BString headerValue) { @@ -72,4 +74,14 @@ public static Object parseHeader(BString headerValue) { private ParseHeader() { } + + private static RecordType createHeaderValueType() { + return TypeCreator.createRecordType(HEADER_VALUE_RECORD, ModuleUtils.getHttpPackage(), 0, + Map.of( + "value", TypeCreator.createField(PredefinedTypes.TYPE_STRING, "value", 0), + "params", + TypeCreator.createField(TypeCreator.createMapType(PredefinedTypes.TYPE_STRING), "params", 0) + ), + PredefinedTypes.TYPE_NEVER, false, 0); + } }