Skip to content

Commit

Permalink
Improve utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Jun 18, 2024
1 parent c80e000 commit 6c2e248
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
12 changes: 12 additions & 0 deletions ballerina/tests/from_json_string_with_union.bal
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,15 @@ isolated function testUnionTypeAsExpectedTypeForParseString6() returns error? {
test:assertEquals(val.m.field2, {a: "3", b: 4});
test:assertEquals(val.n, [1.0, 2.0]);
}

type UnionWithIntersection string|int|int[] & readonly;

@test:Config {
groups: ["Union"]
}
isolated function testUnionWithIntersection() returns error? {
string jsonStr = string `[1, 2, 3]`;
UnionWithIntersection val = check parseString(jsonStr);
test:assertTrue(val is readonly & int[]);
test:assertEquals(val, [1, 2, 3]);
}
8 changes: 8 additions & 0 deletions ballerina/tests/from_string_with_tag_resolution.bal
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ isolated function testTagResolutionSimple(string yaml, anydata value, Options op

function simpleTagResolutionDataProvider() returns [string, anydata, Options][] => [
["&anchor !!bool true", true, {}],
["!!bool True", true, {}],
["!!bool TRUE", true, {}],
["!!bool false", false, {}],
["!!bool False", false, {}],
["!!bool FALSE", false, {}],
["!!null null", (), {}],
["!!null Null", (), {}],
["!!null NULL", (), {}],
["!!null ~", (), {}],
["!!int 12", 12, {}],
["!!float .nan", float:NaN, {}],
["!!float .NaN", float:NaN, {}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package io.ballerina.lib.data.yaml.parser;

import io.ballerina.lib.data.yaml.common.YamlEvent;
import io.ballerina.lib.data.yaml.utils.DiagnosticErrorCode;
import io.ballerina.lib.data.yaml.utils.DiagnosticLog;
import io.ballerina.runtime.api.types.Field;
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.utils.StringUtils;
Expand Down Expand Up @@ -63,9 +61,6 @@ public static Map<String, Field> getAllFieldsInRecord(RecordType recordType) {
Map<String, Field> recordFields = recordType.getFields();
for (String key : recordFields.keySet()) {
String fieldName = modifiedNames.getOrDefault(key, key);
if (fields.containsKey(fieldName)) {
throw DiagnosticLog.error(DiagnosticErrorCode.DUPLICATE_FIELD, fieldName);
}
fields.put(fieldName, recordFields.get(key));
}
return fields;
Expand All @@ -74,7 +69,7 @@ public static Map<String, Field> getAllFieldsInRecord(RecordType recordType) {
public static String getModifiedName(Map<BString, Object> fieldAnnotation, String fieldName) {
for (BString key : fieldAnnotation.keySet()) {
if (key.getValue().endsWith(NAME)) {
return ((Map<BString, Object>) fieldAnnotation.get(key)).get(VALUE).toString();
return ((Map<?, ?>) fieldAnnotation.get(key)).get(VALUE).toString();
}
}
return fieldName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ private Object traverseJson(Object json, Type type) {
return traverseMapJsonOrArrayJson(json, ValueCreator.createMapValue(mapType), referredType);
}
case TypeTags.INTERSECTION_TAG -> {
Type effectiveType = ((IntersectionType) referredType).getEffectiveType();
if (!SymbolFlags.isFlagOn(SymbolFlags.READONLY, effectiveType.getFlags())) {
throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE, type);
}
for (Type constituentType : ((IntersectionType) referredType).getConstituentTypes()) {
if (constituentType.getTag() == TypeTags.READONLY_TAG) {
continue;
Expand Down Expand Up @@ -216,7 +212,6 @@ private Object traverseMapValue(BMap<BString, Object> map, Object currentJsonNod
case TypeTags.NULL_TAG, TypeTags.BOOLEAN_TAG, TypeTags.INT_TAG, TypeTags.FLOAT_TAG,
TypeTags.DECIMAL_TAG, TypeTags.STRING_TAG -> {
BString bStringVal = StringUtils.fromString(mapValue.toString());
// Object value = convertToBasicType(mapValue, currentFieldType);
Object value = Values.fromStringWithType(bStringVal, currentFieldType, schema);
((BMap<BString, Object>) currentJsonNode).put(StringUtils.fromString(fieldNames.pop()), value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@ public static Object constructFloat(String value, YamlParser.ComposerState state
if (value.length() > 1) {
if (value.startsWith(".")) {
String valueSuffix = value.substring(1);
if (valueSuffix.equals("nan") || valueSuffix.equals("NaN") || valueSuffix.equals("NAN")) {
if (isCoreSchemaNaN(valueSuffix)) {
return Double.NaN;
}
if (valueSuffix.equals("inf") || valueSuffix.equals("Inf") || valueSuffix.equals("INF")) {
if (isCoreSchemaInf(valueSuffix)) {
return Double.POSITIVE_INFINITY;
}
} else if (value.startsWith("+.") || value.startsWith("-.")) {
String valueSuffix = value.substring(2);
boolean isInfinity = valueSuffix.equals("inf") ||
valueSuffix.equals("Inf") || valueSuffix.equals("INF");
boolean isInfinity = isCoreSchemaInf(valueSuffix);
if (isInfinity) {
return value.startsWith("+") ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
Expand All @@ -135,6 +134,14 @@ public static Object constructFloat(String value, YamlParser.ComposerState state
throw new Error.YamlParserException("cannot cast " + value + " to float", state.getLine(), state.getColumn());
}

private static boolean isCoreSchemaInf(String valueSuffix) {
return valueSuffix.equals("inf") || valueSuffix.equals("Inf") || valueSuffix.equals("INF");
}

private static boolean isCoreSchemaNaN(String valueSuffix) {
return valueSuffix.equals("nan") || valueSuffix.equals("NaN") || valueSuffix.equals("NAN");
}

public static boolean isCoreSchemaNull(String value) {
return value.equals("null") || value.equals("Null") || value.equals("NULL") || value.equals("~");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public static Type getType(Object value) {
return PredefinedTypes.TYPE_INT;
}

if (value instanceof Double) {
return PredefinedTypes.TYPE_FLOAT;
}
return PredefinedTypes.TYPE_FLOAT;
} else {
if (value instanceof BString) {
return PredefinedTypes.TYPE_STRING;
Expand Down

0 comments on commit 6c2e248

Please sign in to comment.