private static boolean checkIsMapType(BType sourceType, BMapType targetType, List<TypePair> unresolvedTypes) { if (sourceType.getTag() != TypeTags.MAP_TAG) { return false; } return checkContraints(((BMapType) sourceType).getConstrainedType(), targetType.getConstrainedType(), unresolvedTypes); }
private static boolean checkMapCast(BType sourceType, BType targetType, List<TypePair> unresolvedTypes) { BMapType sourceMapType = (BMapType) sourceType; BMapType targetMapType = (BMapType) targetType; if (sourceMapType.equals(targetMapType)) { return true; } if (targetMapType.getConstrainedType().getTag() == TypeTags.ANY_TAG) { return true; } if (sourceMapType.getConstrainedType().getTag() == TypeTags.OBJECT_TYPE_TAG && targetMapType.getConstrainedType().getTag() == TypeTags.OBJECT_TYPE_TAG) { return checkObjectEquivalency((BStructureType) sourceMapType.getConstrainedType(), (BStructureType) targetMapType.getConstrainedType(), unresolvedTypes); } if (sourceMapType.getConstrainedType().getTag() == TypeTags.RECORD_TYPE_TAG && targetMapType.getConstrainedType().getTag() == TypeTags.RECORD_TYPE_TAG) { return checkRecordEquivalency((BRecordType) targetMapType.getConstrainedType(), (BRecordType) sourceMapType.getConstrainedType(), unresolvedTypes); } return false; }
private static boolean isDeepStampingRequiredForMap(BType sourceType) { BType constrainedType = ((BMapType) sourceType).getConstrainedType(); if (constrainedType != null) { if (BTypes.isValueType(constrainedType)) { return false; } else if (constrainedType instanceof BMapType) { return isDeepStampingRequiredForMap(constrainedType); } return true; } return true; }
private static boolean checkIsLikeMapType(BValue sourceValue, BMapType targetType) { if (!(sourceValue instanceof BMap)) { return false; } for (Object mapEntry : ((BMap) sourceValue).values()) { if (!checkIsLikeType((BValue) mapEntry, targetType.getConstrainedType())) { return false; } } return true; }
private static boolean isValidMapInsertion(BType mapType, BValue value) { if (value == null) { return true; } BType constraintType = ((BMapType) mapType).getConstrainedType(); if (constraintType == BTypes.typeAny || constraintType.equals(value.getType())) { return true; } return checkCast(value, constraintType, new ArrayList<>()); }
return isAnydata(((BMapType) type).getConstrainedType(), unresolvedTypes); case TypeTags.RECORD_TYPE_TAG: if (unresolvedTypes.contains(type)) {
/** * Convert a JSON node to a map. * * @param json JSON to convert * @param mapType MapType which the JSON is converted to. * @return If the provided JSON is of object-type, this method will return a {@link BMap} containing the values * of the JSON object. Otherwise a {@link BallerinaException} will be thrown. */ public static BMap<String, ?> jsonToBMap(BValue json, BMapType mapType) { if (json == null || !isJSONObject(json)) { throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE, getComplexObjectTypeName(OBJECT), getTypeName(json)); } BMap<String, BValue> map = new BMap<>(mapType); BType mapConstraint = mapType.getConstrainedType(); if (mapConstraint == null || mapConstraint.getTag() == TypeTags.ANY_TAG || mapConstraint.getTag() == TypeTags.JSON_TAG) { ((BMap<String, BValue>) json).getMap().entrySet().forEach(entry -> { map.put(entry.getKey(), entry.getValue()); }); return map; } // We reach here if the map is constrained. ((BMap<String, BRefType<?>>) json).getMap().entrySet().forEach(entry -> { map.put(entry.getKey(), convertJSON(entry.getValue(), mapConstraint)); }); return map; }
for (Object value : this.values()) { if (value != null) { ((BValue) value).stamp(((BMapType) type).getConstrainedType());
return checkCastByType(((BMapType) sourceType).getConstrainedType(), targetType, unresolvedTypes); default: return false;
} catch (BallerinaException e) { throw new BLangUsageException("invalid argument '" + value + "', expected map argument of element" + " type: " + ((BMapType) type).getConstrainedType());
case TypeTags.MAP_TAG: if (!isValidMapInsertion(mapType, value)) { BType expType = ((BMapType) mapType).getConstrainedType(); throw new BLangMapStoreException(BallerinaErrorReasons.INHERENT_TYPE_VIOLATION_ERROR, BLangExceptionHelper
return true; case TypeTags.MAP_TAG: return checkCastByType(((BMapType) rhsType).getConstrainedType(), lhsType, unresolvedTypes); case TypeTags.ARRAY_TAG: if (((BJSONType) lhsType).getConstrainedType() != null) {