public static Map<String, String> getMapField(BValue bValue) { if (bValue != null && bValue instanceof BMap) { BMap bMap = (BMap) bValue; return bMap.getMap(); } return null; } }
/** * Deep equality check for a map. * * @param lhsMap Map on the left hand side * @param rhsMap Map on the right hand side * @param checkedValues Structured value pairs already compared or being compared * @return True if the map values are equal, else false. */ private static boolean isEqual(BMap lhsMap, BMap rhsMap, List<ValuePair> checkedValues) { ValuePair compValuePair = new ValuePair(lhsMap, rhsMap); if (checkedValues.contains(compValuePair)) { return true; } checkedValues.add(compValuePair); if (lhsMap.size() != rhsMap.size()) { return false; } if (!lhsMap.getMap().keySet().containsAll(rhsMap.getMap().keySet())) { return false; } Iterator<Map.Entry<String, BValue>> mapIterator = lhsMap.getMap().entrySet().iterator(); while (mapIterator.hasNext()) { Map.Entry<String, BValue> lhsMapEntry = mapIterator.next(); if (!isEqual(lhsMapEntry.getValue(), rhsMap.get(lhsMapEntry.getKey()), checkedValues)) { return false; } } return true; }
String fieldName = targetTypeEntry.getKey().toString(); if (!(((BMap) sourceValue).getMap().containsKey(fieldName)) && !Flags.isFlagOn(targetType.getFields().get(fieldName).flags, Flags.OPTIONAL)) { return false; for (Object object : ((BMap) sourceValue).getMap().entrySet()) { Map.Entry valueEntry = (Map.Entry) object; String fieldName = valueEntry.getKey().toString();
/** * Return a {@code PendingContext} that is waiting on a message from the given channel. * @param channel Channel to check the waiting context * @param key message key * @return Worker context or null */ public PendingContext pollOnChannel(String channel, BValue key) { //add channel if absent addChannel(channel); String keyVal = null; if (key != null) { if (key instanceof BMap) { keyVal = DatabaseUtils.sortBMap(((BMap) key).getMap()); } else { keyVal = key.stringValue(); } } LinkedList<PendingContext> pendingCtxs = channelList.get(channel).get(keyVal); if (pendingCtxs != null) { return pendingCtxs.poll(); } return null; }
/** * Add a worker context to the channels map that is waiting for a message. * @param channel Channel the worker is waiting for * @param key key of the message * @param ctx requested context * @param regIndex variable index to assign the channel message */ public void addWaitingContext(String channel, BValue key, Strand ctx, int regIndex) { //add channel if absent addChannel(channel); Map<String, LinkedList<PendingContext>> channelEntries = channelList.get(channel); String keyVal = null; if (key != null) { if (key instanceof BMap) { keyVal = DatabaseUtils.sortBMap(((BMap) key).getMap()); } else { keyVal = key.stringValue(); } } LinkedList<PendingContext> ctxList = channelEntries.computeIfAbsent(keyVal, bValue -> new LinkedList<>()); PendingContext pContext = new PendingContext(); pContext.regIndex = regIndex; pContext.context = ctx; ctxList.add(pContext); channelEntries.put(keyVal, ctxList); }
private static void convertStructToMap(Strand strand, int[] operands, StackFrame sf) { int i = operands[0]; int j = operands[1]; // TODO: do validation for type? BMap newMap = new BMap(BTypes.typeMap); ((BMap) sf.refRegs[i]).getMap().forEach((key, value) -> newMap.put(key, value == null ? null : ((BValue) value).copy(new HashMap<>()))); sf.refRegs[j] = newMap; }
private static void setParam(PreparedStatement stmt, BValue value, BType bType, int index, boolean isKey) throws SQLException { int type = bType.getTag(); switch (type) { case TypeTags.INT_TAG: stmt.setLong(index, ((BInteger) value).intValue()); break; case TypeTags.STRING_TAG: stmt.setString(index, value.stringValue()); break; case TypeTags.FLOAT_TAG: stmt.setDouble(index, ((BFloat) value).floatValue()); break; case TypeTags.BOOLEAN_TAG: stmt.setBoolean(index, ((BBoolean) value).booleanValue()); break; case TypeTags.BYTE_TAG: stmt.setByte(index, ((BByte) value).byteValue()); break; default: if (isKey && value instanceof BMap) { stmt.setString(index, sortBMap(((BMap) value).getMap())); } else { stmt.setString(index, value.toString()); } } }
@Override @SuppressWarnings("unchecked") public BMap toObject(BPacket packet, BValueDeserializer bValueDeserializer) { BMap payload = (BMap<String, BValue>) packet.getValue(); HashMap deserializedMap = (HashMap) bValueDeserializer.deserialize(payload, HashMap.class); BType type = (BType) bValueDeserializer.deserialize(packet.get(MAP_STORAGE_TYPE), BType.class); BMap bMap = new BMap(type); bMap.getMap().putAll(deserializedMap); return bMap; } }
/** * 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 BPacket toBValue(BMap bMap, BValueSerializer serializer) { LinkedHashMap implMap = bMap.getMap(); BValue serialized = serializer.toBValue(implMap, implMap.getClass()); BValue value = serializer.toBValue(bMap.getType(), null); return BPacket.from(typeName(), serialized).put(MAP_STORAGE_TYPE, value); }
public SerializableBMap(BMap<K, V> bMap, SerializableState state) { structName = bMap.getType().getName(); pkgPath = bMap.getType().getPackagePath(); bMap.getNativeData().forEach((k, o) -> nativeData.put(k, state.serialize(o))); bMap.getMap().forEach((k, v) -> map.put(k, state.serialize(v))); }
case TypeTags.JSON_TAG: case TypeTags.MAP_TAG: return ((BMap) sourceValue).getMap().values().stream() .allMatch(value -> checkIsLikeType((BValue) value, targetType)); case TypeTags.ARRAY_TAG:
for (Entry<String, BValue> structField : map.getMap().entrySet()) { String key = structField.getKey(); BValue value = structField.getValue();
for (Map.Entry valueEntry : this.getMap().entrySet()) { String fieldName = valueEntry.getKey().toString(); if ((valueEntry.getValue()) != null) {
switch (json.getType().getTag()) { case TypeTags.JSON_TAG: LinkedHashMap<String, BValue> map = ((BMap) json).getMap(); for (Entry<String, BValue> entry : map.entrySet()) { currentRoot = traverseJsonNode(entry.getValue(), entry.getKey(), currentRoot,
private static boolean checkIsLikeJSONType(BValue sourceValue, BJSONType targetType) { if (targetType.getConstrainedType() != null) { return checkIsLikeType(sourceValue, targetType.getConstrainedType()); } else if (sourceValue.getType().getTag() == TypeTags.ARRAY_TAG) { BValueArray source = (BValueArray) sourceValue; if (BTypes.isValueType(source.elementType)) { return checkIsType(source.elementType, targetType, new ArrayList<>()); } BRefType<?>[] arrayValues = source.getValues(); for (int i = 0; i < ((BValueArray) sourceValue).size(); i++) { if (!checkIsLikeType(arrayValues[i], targetType)) { return false; } } } else if (sourceValue.getType().getTag() == TypeTags.MAP_TAG) { for (BValue value : ((BMap) sourceValue).values()) { if (!checkIsLikeType(value, targetType)) { return false; } } } else if (sourceValue.getType().getTag() == TypeTags.RECORD_TYPE_TAG) { for (Object object : ((BMap) sourceValue).getMap().values()) { if (!checkIsLikeType((BValue) object, targetType)) { return false; } } } return true; }
case TypeTags.JSON_TAG: this.startObject(); for (Entry<String, BValue> entry : ((BMap<String, BValue>) json).getMap().entrySet()) { this.writeFieldName(entry.getKey()); serialize(entry.getValue());
try { jmsMessage = session.createMapMessage(); Map<String, BValue> contentMap = content.getMap();