Advertisement
titovtima

Untitled

Mar 7th, 2021
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.93 KB | None | 0 0
  1. private fun doReplace(replace: Command.Replace): CommandResult {
  2.         val elementPath = currentPath.plus(replace.path)
  3.         val toReplace = elementPath.getElement(rootElement)
  4.         if (toReplace != null) {
  5.             val parent = ElementPath(elementPath.segments.dropLast(1)).getElement(rootElement)
  6.             when (parent) {
  7.                 is MutableElement.ArrayElement -> {
  8.                     val index = replace.path.segments.last().substring(1,2).toInt()
  9.                     parent.items[index] = replace.newElement.invoke()
  10.                     return CommandResult.Success
  11.                 }
  12.                 is MutableElement.ObjectElement -> {
  13.                     val index = replace.path.segments.last()
  14.                     parent.items[index] = replace.newElement.invoke()
  15.                     return CommandResult.Success
  16.                 }
  17.             }
  18.         }
  19.         return findInvalidPath(replace.path)
  20.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement