private def addNode()

in http-core/src/main/scala/com/twitter/finatra/http/jsonpatch/JsonPatchOperator.scala [212:231]


  private def addNode(path: JsonPointer, value: JsonNode, target: JsonNode): Unit = {
    if (path.tail == null) {
      throw new JsonPatchException("invalid path for add operation")
    } else if (path.tail.matches) {
      target match {
        case on: ObjectNode => on.set[JsonNode](path.getMatchingProperty, value)
        case an: ArrayNode =>
          // this does not use the 'getLeafIndex' helper function because 'add' indexes are slightly different.
          // Gotcha: '<= an.size' and not '< an.size' because we may (of course) add at the end of the array
          //         'index = an.size' and not 'index = an.size - 1' because the '/-' pointer means append for the add operation.
          val index: Int = if (path == lastElementPointer) an.size else path.getMatchingIndex
          checkBound("add", index <= an.size && index >= 0)
          an.insert(index, value)

        case _ => throw new JsonPatchException("invalid target for add")
      }
    } else {
      addNode(path.tail, value, nextNodeByPath(path, target, "add"))
    }
  }