/** * Gets the value of the key if it is a BsonArray, or throws if not. * * @param key the key * @return the value of the key as a BsonArray * @throws org.bson.BsonInvalidOperationException if the document does not contain the key or the value is not of the expected type */ public BsonArray getArray(final Object key) { throwIfKeyAbsent(key); return get(key).asArray(); }
/** * If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a * BsonArray. * * @param key the key * @param defaultValue the default value * @return the value of the key as a BsonArray * @throws org.bson.BsonInvalidOperationException if the document contains the key but the value is not of the expected type */ public BsonArray getArray(final Object key, final BsonArray defaultValue) { if (!containsKey(key)) { return defaultValue; } return get(key).asArray(); }
@Override protected void doReadStartArray() { BsonArray array = currentValue.asArray(); setContext(new Context(getContext(), BsonContextType.ARRAY, array)); }
private void addClause(final BsonDocument document, final Map.Entry<String, BsonValue> clause) { if (clause.getKey().equals("$and")) { for (BsonValue value : clause.getValue().asArray()) { for (Map.Entry<String, BsonValue> element : value.asDocument().entrySet()) { addClause(document, element); } } } else if (document.size() == 1 && document.keySet().iterator().next().equals("$and")) { document.get("$and").asArray().add(new BsonDocument(clause.getKey(), clause.getValue())); } else if (document.containsKey(clause.getKey())) { if (document.get(clause.getKey()).isDocument() && clause.getValue().isDocument()) { BsonDocument existingClauseValue = document.get(clause.getKey()).asDocument(); BsonDocument clauseValue = clause.getValue().asDocument(); if (keysIntersect(clauseValue, existingClauseValue)) { promoteRenderableToDollarForm(document, clause); } else { existingClauseValue.putAll(clauseValue); } } else { promoteRenderableToDollarForm(document, clause); } } else { document.append(clause.getKey(), clause.getValue()); } }
@Override public BsonDocument clone() { BsonDocument to = new BsonDocument(); for (Entry<String, BsonValue> cur : entrySet()) { switch (cur.getValue().getBsonType()) { case DOCUMENT: to.put(cur.getKey(), cur.getValue().asDocument().clone()); break; case ARRAY: to.put(cur.getKey(), cur.getValue().asArray().clone()); break; case BINARY: to.put(cur.getKey(), BsonBinary.clone(cur.getValue().asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.put(cur.getKey(), BsonJavaScriptWithScope.clone(cur.getValue().asJavaScriptWithScope())); break; default: to.put(cur.getKey(), cur.getValue()); } } return to; }
if (keyvalueforStruct.getValue().asArray().isEmpty()) { switch (arrayEncoding) { case ARRAY: switch (arrayEncoding) { case ARRAY: BsonType valueType = keyvalueforStruct.getValue().asArray().get(0).getBsonType(); List<BsonValue> arrValues = keyvalueforStruct.getValue().asArray().getValues(); ArrayList<Object> list = new ArrayList<>(); break; case DOCUMENT: final BsonArray array = keyvalueforStruct.getValue().asArray(); final Map<String, BsonValue> convertedArray = new HashMap<>(); final Schema arraySchema = schema.field(keyvalueforStruct.getKey()).schema();
if (keyValuesforSchema.getValue().asArray().isEmpty()) { switch (arrayEncoding) { case ARRAY: switch (arrayEncoding) { case ARRAY: BsonType valueType = keyValuesforSchema.getValue().asArray().get(0).getBsonType(); for (BsonValue element: keyValuesforSchema.getValue().asArray()) { if (element.getBsonType() != valueType) { throw new ConnectException("Field " + key + " of schema " + builder.name() + " is not a homogenous array.\n" final SchemaBuilder documentSchemaBuilder = SchemaBuilder.struct().name(builder.name() + "." + key).optional(); final Map<String, BsonType> union = new HashMap<>(); for (BsonValue element: keyValuesforSchema.getValue().asArray()) { final BsonDocument arrayDocs = element.asDocument(); for (Entry<String, BsonValue> arrayDoc: arrayDocs.entrySet()) { final BsonArray array = keyValuesforSchema.getValue().asArray(); final SchemaBuilder arrayStructBuilder = SchemaBuilder.struct().name(builder.name() + "." + key).optional(); final Map<String, BsonValue> convertedArray = new HashMap<>();
@Override public BsonArray clone() { BsonArray to = new BsonArray(); for (BsonValue cur : this) { switch (cur.getBsonType()) { case DOCUMENT: to.add(cur.asDocument().clone()); break; case ARRAY: to.add(cur.asArray().clone()); break; case BINARY: to.add(BsonBinary.clone(cur.asBinary())); break; case JAVASCRIPT_WITH_SCOPE: to.add(BsonJavaScriptWithScope.clone(cur.asJavaScriptWithScope())); break; default: to.add(cur); } } return to; } }
return value.asSymbol().getSymbol(); case ARRAY: return value.asArray().toArray(); case DOCUMENT: return Document.parse(value.asDocument().toJson());
check(doc.get("null").isNull()); check(doc.get("array").getBsonType()).is(BsonType.ARRAY); check(doc.get("array").asArray()).isEmpty(); check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT); check(doc.get("object").asDocument().keySet()).isEmpty();
break; case ARRAY: pipeArray(value.asArray()); break; case DOUBLE:
private BsonArray getAggregationMetadata(BsonDocument contentToTransform) { List<Optional<BsonValue>> ___aggrs = JsonUtils .getPropsFromPath(contentToTransform, "$." + AbstractAggregationOperation.AGGREGATIONS_ELEMENT_NAME); if (___aggrs == null || ___aggrs.isEmpty()) { return null; } Optional<BsonValue> __aggrs = ___aggrs.get(0); if (__aggrs == null || !__aggrs.isPresent()) { return null; } BsonValue _aggrs = __aggrs.get(); if (_aggrs.isArray()) { return _aggrs.asArray(); } else { return null; } } }
@Override public PVector<V> applyUnsafe(BsonValue p) throws BsonInvalidOperationException { BsonArray bsonArray = p.asArray(); List<V> values = bsonArray.getValues().stream().map(v -> itemDeserialiser.apply(v)).collect(Collectors.toList()); return TreePVector.from(values); } }
return value.asSymbol().getSymbol(); case ARRAY: return value.asArray().toArray(); case DOCUMENT: return Document.parse(value.asDocument().toJson());
/** * @param vars RequestContext.getAggregationVars() * @return the stages, with unescaped operators and bound variables * @throws org.restheart.handlers.metadata.InvalidMetadataException * @throws org.restheart.handlers.aggregation.QueryVariableNotBoundException */ public List<BsonDocument> getResolvedStagesAsList(BsonDocument vars) throws InvalidMetadataException, QueryVariableNotBoundException { BsonArray replacedStages = bindAggregationVariables( JsonUtils.unescapeKeys(stages), vars) .asArray(); List<BsonDocument> ret = new ArrayList<>(); replacedStages.stream().filter((stage) -> (stage.isDocument())) .forEach((stage) -> { ret.add(stage.asDocument()); }); return ret; }
private void addItems(BsonArray elements, BsonDocument items, String ns) { if (items.containsKey(ns)) { elements.addAll( items .get(ns) .asArray()); } } }
@Override public boolean check( HttpServerExchange exchange, RequestContext context, BsonDocument contentToCheck, BsonValue args) { if (args.isArray()) { BsonArray conditions = filterMissingOptionalAndNullNullableConditions( args.asArray(), contentToCheck); return applyConditions(conditions, contentToCheck, context); } else { context.addWarning( "checker wrong definition: args property must be " + "an arrary of string property names."); return true; } }
public static List<RequestTransformer> getFromJson(BsonDocument props) throws InvalidMetadataException { BsonValue _rts = props.get(RTS_ELEMENT_NAME); if (_rts == null || !_rts.isArray()) { throw new InvalidMetadataException((_rts == null ? "missing '" : "invalid '") + RTS_ELEMENT_NAME + "' element; it must be an array"); } BsonArray rts = _rts.asArray(); List<RequestTransformer> ret = new ArrayList<>(); for (BsonValue o : rts.getValues()) { if (o.isDocument()) { ret.add(getSingleFromJson(o.asDocument())); } else { throw new InvalidMetadataException("invalid '" + RTS_ELEMENT_NAME + "'. Array elements must be json objects"); } } return ret; }
public static boolean doesRequestUsesUpdateOperators(BsonValue content) { if (content.isDocument()) { return JsonUtils.containsUpdateOperators(content.asDocument()); } else if (content.isArray()) { BsonArray objs = content.asArray(); return objs.stream().allMatch(obj -> { if (obj.isDocument()) { return doesRequestUsesUpdateOperators(obj); } else { return true; } }); } else { return true; } }
private BsonArray unflattenArray(BsonArray array) { BsonArray unflattenArray = new BsonArray(); for (BsonValue value : array) { if (value.isArray()) { unflattenArray.add(unflattenArray(value.asArray())); } else if (value.isDocument()) { unflattenArray.add(new JsonUnflattener(value.asDocument()) .withSeparator(separator).unflatten()); } else { unflattenArray.add(value); } } return unflattenArray; }