@Test public void object_array_append() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson(" [ {'$append' : { 'field7' : {} } }, {'$set': {'field7.-1.elemf1':'value'}} ]"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); updater.update(doc, md.getFieldTreeRoot(), new Path()); JsonNode f7 = doc.get(new Path("field7")); Assert.assertEquals(5, f7.size()); Assert.assertEquals("value", f7.get(4).get("elemf1").asText()); }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; Path absPath = new Path(contextPath, arrayField); JsonNode node = doc.get(absPath); int insertTo = insertionIndex; if (node == null || node instanceof NullNode) { doc.modify(absPath, node = factory.arrayNode(), true); } if (node instanceof ArrayNode) { ArrayNode arrayNode = (ArrayNode) node; for (RValueData rvalueData : values) { LOGGER.debug("add element to {} rvalue:{}", absPath, rvalueData); Object newValue = null; Type newValueType = null; JsonNode newValueNode = null; if (rvalueData.refPath != null) { JsonNode refNode = doc.get(new Path(contextPath, rvalueData.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = rvalueData.refType.fromJson(newValueNode); newValueType = rvalueData.refType; } } else if (rvalueData.value != null) { newValue = rvalueData.value.getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : fieldMd.getElement().getType().toJson(factory, newValue); newValueType = fieldMd.getElement().getType(); } else if (rvalueData.rvalueType == RValueExpression.RValueType._null) { newValueNode = factory.nullNode(); } LOGGER.debug("newValueType:{}, newValue:{}, newValueNode:{} ", newValueType, newValue, newValueNode); if (insertTo >= 0) { while (arrayNode.size() < insertTo) { arrayNode.addNull(); } if (arrayNode.size() > insertTo) { arrayNode.insert(insertTo, newValueNode); } else { arrayNode.add(newValueNode); } insertTo++; } else { arrayNode.add(newValueNode); } ret = true; } if (ret) { MutablePath p = new MutablePath(arraySizeField); p.rewriteIndexes(absPath); LOGGER.debug("Setting {} = {}", p, arrayNode.size()); doc.modify(p, factory.numberNode(arrayNode.size()), false); } } return ret; }
ArrayAddExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; Path absPath = new Path(contextPath, arrayField); JsonNode node = doc.get(absPath); int insertTo = insertionIndex; if (node == null || node instanceof NullNode) { doc.modify(absPath, node = factory.arrayNode(), true); } if (node instanceof ArrayNode) { ArrayNode arrayNode = (ArrayNode) node; for (RValueData rvalueData : values) { LOGGER.debug("add element to {} rvalue:{}", absPath, rvalueData); Object newValue = null; Type newValueType = null; JsonNode newValueNode = null; if (rvalueData.refPath != null) { JsonNode refNode = doc.get(new Path(contextPath, rvalueData.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = rvalueData.refType.fromJson(newValueNode); newValueType = rvalueData.refType; } } else if (rvalueData.value != null) { newValue = rvalueData.value.getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : fieldMd.getElement().getType().toJson(factory, newValue); newValueType = fieldMd.getElement().getType(); } else if (rvalueData.rvalueType == RValueExpression.RValueType._null) { newValueNode = factory.nullNode(); } LOGGER.debug("newValueType:{}, newValue:{}, newValueNode:{} ", newValueType, newValue, newValueNode); if (insertTo >= 0) { while (arrayNode.size() < insertTo) { arrayNode.addNull(); } if (arrayNode.size() > insertTo) { arrayNode.insert(insertTo, newValueNode); } else { arrayNode.add(newValueNode); } insertTo++; } else { arrayNode.add(newValueNode); } ret = true; } if (ret) { MutablePath p = new MutablePath(arraySizeField); p.rewriteIndexes(absPath); LOGGER.debug("Setting {} = {}", p, arrayNode.size()); doc.modify(p, factory.numberNode(arrayNode.size()), false); } } return ret; } }
ArrayAddExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; Path absPath = new Path(contextPath, arrayField); JsonNode node = doc.get(absPath); int insertTo = insertionIndex; if (node == null || node instanceof NullNode) { doc.modify(absPath, node = factory.arrayNode(), true); } if (node instanceof ArrayNode) { ArrayNode arrayNode = (ArrayNode) node; for (RValueData rvalueData : values) { LOGGER.debug("add element to {} rvalue:{}", absPath, rvalueData); Object newValue = null; Type newValueType = null; JsonNode newValueNode = null; if (rvalueData.refPath != null) { JsonNode refNode = doc.get(new Path(contextPath, rvalueData.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = rvalueData.refType.fromJson(newValueNode); newValueType = rvalueData.refType; } } else if (rvalueData.value != null) { newValue = rvalueData.value.getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : fieldMd.getElement().getType().toJson(factory, newValue); newValueType = fieldMd.getElement().getType(); } else if (rvalueData.rvalueType == RValueExpression.RValueType._null) { newValueNode = factory.nullNode(); } LOGGER.debug("newValueType:{}, newValue:{}, newValueNode:{} ", newValueType, newValue, newValueNode); if (insertTo >= 0) { while (arrayNode.size() < insertTo) { arrayNode.addNull(); } if (arrayNode.size() > insertTo) { arrayNode.insert(insertTo, newValueNode); } else { arrayNode.add(newValueNode); } insertTo++; } else { arrayNode.add(newValueNode); } ret = true; } if (ret) { MutablePath p = new MutablePath(arraySizeField); p.rewriteIndexes(absPath); LOGGER.debug("Setting {} = {}", p, arrayNode.size()); doc.modify(p, factory.numberNode(arrayNode.size()), false); } } return ret; } ArrayAddExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, ArrayAddExpression expr); }
ArrayAddExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; Path absPath = new Path(contextPath, arrayField); JsonNode node = doc.get(absPath); int insertTo = insertionIndex; if (node == null || node instanceof NullNode) { doc.modify(absPath, node = factory.arrayNode(), true); } if (node instanceof ArrayNode) { ArrayNode arrayNode = (ArrayNode) node; for (RValueData rvalueData : values) { LOGGER.debug("add element to {} rvalue:{}", absPath, rvalueData); Object newValue = null; Type newValueType = null; JsonNode newValueNode = null; if (rvalueData.refPath != null) { JsonNode refNode = doc.get(new Path(contextPath, rvalueData.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = rvalueData.refType.fromJson(newValueNode); newValueType = rvalueData.refType; } } else if (rvalueData.value != null) { newValue = rvalueData.value.getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : fieldMd.getElement().getType().toJson(factory, newValue); newValueType = fieldMd.getElement().getType(); } else if (rvalueData.rvalueType == RValueExpression.RValueType._null) { newValueNode = factory.nullNode(); } LOGGER.debug("newValueType:{}, newValue:{}, newValueNode:{} ", newValueType, newValue, newValueNode); if (insertTo >= 0) { while (arrayNode.size() < insertTo) { arrayNode.addNull(); } if (arrayNode.size() > insertTo) { arrayNode.insert(insertTo, newValueNode); } else { arrayNode.add(newValueNode); } insertTo++; } else { arrayNode.add(newValueNode); } ret = true; } if (ret) { MutablePath p = new MutablePath(arraySizeField); p.rewriteIndexes(absPath); LOGGER.debug("Setting {} = {}", p, arrayNode.size()); doc.modify(p, factory.numberNode(arrayNode.size()), false); } } return ret; } ArrayAddExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, ArrayAddExpression expr); @Override void getUpdateFields(Set<Path> fields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath); }
ArrayAddExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; Path absPath = new Path(contextPath, arrayField); JsonNode node = doc.get(absPath); int insertTo = insertionIndex; if (node == null || node instanceof NullNode) { doc.modify(absPath, node = factory.arrayNode(), true); } if (node instanceof ArrayNode) { ArrayNode arrayNode = (ArrayNode) node; for (RValueData rvalueData : values) { LOGGER.debug("add element to {} rvalue:{}", absPath, rvalueData); Object newValue = null; Type newValueType = null; JsonNode newValueNode = null; if (rvalueData.refPath != null) { JsonNode refNode = doc.get(new Path(contextPath, rvalueData.refPath)); if (refNode != null) { newValueNode = refNode.deepCopy(); newValue = rvalueData.refType.fromJson(newValueNode); newValueType = rvalueData.refType; } } else if (rvalueData.value != null) { newValue = rvalueData.value.getValue(); newValueNode = newValue instanceof JsonNode ? (JsonNode) newValue : fieldMd.getElement().getType().toJson(factory, newValue); newValueType = fieldMd.getElement().getType(); } else if (rvalueData.rvalueType == RValueExpression.RValueType._null) { newValueNode = factory.nullNode(); } LOGGER.debug("newValueType:{}, newValue:{}, newValueNode:{} ", newValueType, newValue, newValueNode); if (insertTo >= 0) { while (arrayNode.size() < insertTo) { arrayNode.addNull(); } if (arrayNode.size() > insertTo) { arrayNode.insert(insertTo, newValueNode); } else { arrayNode.add(newValueNode); } insertTo++; } else { arrayNode.add(newValueNode); } ret = true; } if (ret) { MutablePath p = new MutablePath(arraySizeField); p.rewriteIndexes(absPath); LOGGER.debug("Setting {} = {}", p, arrayNode.size()); doc.modify(p, factory.numberNode(arrayNode.size()), false); } } return ret; } ArrayAddExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, ArrayAddExpression expr); @Override void getUpdateFields(Set<Path> fields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath);  }