/** * {@inheritDoc} */ @Override public BMap<?, ?> getAttributesMap() { if (sequence.size() == 1) { return ((BXMLItem) sequence.getRefValue(0)).getAttributesMap(); } return new BMap<>(new BMapType(BTypes.typeString)); }
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 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 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 BMap<String, BString> getTags(MetricId metricId) { BMap<String, BString> bTags = new BMap<>(new BMapType(BTypes.typeString)); Set<Tag> tags = metricId.getTags(); for (Tag tag : tags) { bTags.put(tag.getKey(), new BString(tag.getValue())); } return bTags; } }
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 BMap<String, BString> getTags(MetricId metricId) { BMap<String, BString> bTags = new BMap<>(new BMapType(BTypes.typeString)); Set<Tag> tags = metricId.getTags(); for (Tag tag : tags) { bTags.put(tag.getKey(), new BString(tag.getValue())); } return bTags; } }
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<>()); }
/** * {@inheritDoc} */ @Override public BMap<?, ?> getAttributesMap() { BMap<String, BString> attrMap = new BMap<>(new BMapType(BTypes.typeString)); if (nodeType != XMLNodeType.ELEMENT) { return attrMap; } OMNamespace defaultNs = ((OMElement) omNode).getDefaultNamespace(); String namespaceOfPrefix = '{' + (defaultNs == null ? XMLConstants.XMLNS_ATTRIBUTE_NS_URI : defaultNs.getNamespaceURI()) + '}'; Iterator<OMNamespace> namespaceIterator = ((OMElement) omNode).getAllDeclaredNamespaces(); while (namespaceIterator.hasNext()) { OMNamespace namespace = namespaceIterator.next(); String prefix = namespace.getPrefix(); if (prefix.isEmpty()) { continue; } attrMap.put(namespaceOfPrefix + prefix, new BString(namespace.getNamespaceURI())); } Iterator<OMAttribute> attrIterator = ((OMElement) omNode).getAllAttributes(); while (attrIterator.hasNext()) { OMAttribute attr = attrIterator.next(); attrMap.put(attr.getQName().toString(), new BString(attr.getAttributeValue())); } return attrMap; }
return isAnydata(((BMapType) type).getConstrainedType(), unresolvedTypes); case TypeTags.RECORD_TYPE_TAG: if (unresolvedTypes.contains(type)) {
@Override public BType getConstrainedType(char typeChar, BType constraint) { switch (typeChar) { case 'J': if (constraint == null) { return BTypes.typeJSON; } return new BJSONType(constraint); case 'D': if (constraint == null) { return BTypes.typeTable; } return new BTableType(constraint); case 'M': if (constraint == null || constraint == BTypes.typeAny) { return BTypes.typeMap; } return new BMapType(constraint); case 'H': return new BStreamType(constraint); case 'G': case 'T': case 'X': case 'Q': default: return constraint; } }
/** * 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; }
@Override public void receive(Event[] events) { BValueArray outputArray = new BValueArray(new BMapType(structType)); int j = 0; for (Event event : events) { // Here it is assumed that an event data will contain all the fields // of the record. Otherwise, some fields will be missing from the record value. BMap<String, BValue> output = new BMap<String, BValue>(structType); Iterator<String> fieldNamesIterator = structType.getFields().keySet().iterator(); for (Object field : event.getData()) { if (field instanceof Long || field instanceof Integer) { output.put(fieldNamesIterator.next(), new BInteger(((Number) field).longValue())); } else if (field instanceof Double || field instanceof Float) { output.put(fieldNamesIterator.next(), new BFloat(((Number) field).doubleValue())); } else if (field instanceof Boolean) { output.put(fieldNamesIterator.next(), new BBoolean(((Boolean) field))); } else if (field instanceof String) { output.put(fieldNamesIterator.next(), new BString((String) field)); } } outputArray.add(j, output); j++; } List<BValue> argsList = new ArrayList<>(); argsList.addAll(closureArgs); argsList.add(outputArray); BVMExecutor.executeFunction(functionPointer.value().getPackageInfo().getProgramFile(), functionPointer.value(), argsList.toArray(new BValue[0])); } });
for (Object value : this.values()) { if (value != null) { ((BValue) value).stamp(((BMapType) type).getConstrainedType());
@Override public void execute(Context context) { try { BMap<String, BValue> requestStruct = ((BMap<String, BValue>) context.getRefArgument(0)); HttpCarbonMessage httpCarbonMessage = (HttpCarbonMessage) requestStruct .getNativeData(HttpConstants.TRANSPORT_MESSAGE); BMapType mapType = new BMapType(BTypes.typeString); if (httpCarbonMessage.getProperty(HttpConstants.QUERY_STR) != null) { String queryString = (String) httpCarbonMessage.getProperty(HttpConstants.QUERY_STR); BMap<String, BString> params = new BMap<>(mapType); URIUtil.populateQueryParamMap(queryString, params); context.setReturnValues(params); } else { context.setReturnValues(new BMap<>(mapType)); } } catch (Exception e) { throw new BallerinaException("Error while retrieving query param from message: " + e.getMessage()); } } }
return checkCastByType(((BMapType) sourceType).getConstrainedType(), targetType, unresolvedTypes); default: return false;
BType anydataMapType = new BMapType(type); if (checkIsLikeType(value, anydataMapType)) { return anydataMapType;
} 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) {