|
| 1 | +package com.bugsnag.android.internal.json |
| 2 | + |
| 3 | +/** |
| 4 | + * A simple path implementation similar to json path, but much simpler. The notation is strictly |
| 5 | + * dot (`'.'`) separated and does not support name escaping. Paths are parsed from strings and |
| 6 | + * can be efficiently evaluated any number of times once parsed, and are thread safe. |
| 7 | + * |
| 8 | + * Paths are in the form `"property.0.-1.*.value"` where `'*'` is a wildcard match, `0` is the |
| 9 | + * first item of an array (or a property named `"0"`) and `-1` is the last element in an array (or |
| 10 | + * a property named `"-1"`). Null values or non-existent values are skipped. |
| 11 | + */ |
| 12 | +internal class JsonCollectionPath private constructor( |
| 13 | + private val root: PathNode, |
| 14 | + private val path: String |
| 15 | +) { |
| 16 | + /** |
| 17 | + * Extract all of the selected values from the given JSON object stored in the given `Map`. |
| 18 | + */ |
| 19 | + fun extractFrom(json: Map<String, *>): List<Any> { |
| 20 | + val out = ArrayList<Any>() |
| 21 | + root.visit(json, out::add) |
| 22 | + return out |
| 23 | + } |
| 24 | + |
| 25 | + override fun toString(): String { |
| 26 | + return path |
| 27 | + } |
| 28 | + |
| 29 | + companion object { |
| 30 | + val IDENTITY_PATH = JsonCollectionPath(PathNode.TerminalNode, "") |
| 31 | + |
| 32 | + fun fromString(path: String): JsonCollectionPath { |
| 33 | + if (path.isEmpty()) { |
| 34 | + return IDENTITY_PATH |
| 35 | + } |
| 36 | + |
| 37 | + val segments = path.split('.') |
| 38 | + .reversed() // we build the path backwards |
| 39 | + |
| 40 | + var node: PathNode = PathNode.TerminalNode |
| 41 | + segments.forEach { segment -> |
| 42 | + node = segment.toPathNode(node) |
| 43 | + } |
| 44 | + |
| 45 | + return JsonCollectionPath(node, path) |
| 46 | + } |
| 47 | + |
| 48 | + private fun String.toPathNode(next: PathNode): PathNode { |
| 49 | + if (this == "*") { |
| 50 | + return PathNode.Wildcard(next) |
| 51 | + } |
| 52 | + |
| 53 | + val index = this.toIntOrNull() |
| 54 | + if (index != null) { |
| 55 | + return if (index < 0) { |
| 56 | + PathNode.NegativeIndex(index, next) |
| 57 | + } else { |
| 58 | + PathNode.PositiveIndex(index, next) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return PathNode.Property(this, next) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private sealed class PathNode { |
| 67 | + abstract fun visit(element: Any, collector: (Any) -> Unit) |
| 68 | + |
| 69 | + abstract class NonTerminalPathNode(protected val next: PathNode) : PathNode() |
| 70 | + |
| 71 | + class Property(val name: String, next: PathNode) : NonTerminalPathNode(next) { |
| 72 | + override fun visit(element: Any, collector: (Any) -> Unit) { |
| 73 | + if (element is Map<*, *>) { |
| 74 | + element[name]?.let { next.visit(it, collector) } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + override fun toString(): String = name |
| 79 | + } |
| 80 | + |
| 81 | + class Wildcard(next: PathNode) : NonTerminalPathNode(next) { |
| 82 | + override fun visit(element: Any, collector: (Any) -> Unit) { |
| 83 | + if (element is Iterable<*>) { |
| 84 | + element.forEach { item -> |
| 85 | + item?.let { next.visit(it, collector) } |
| 86 | + } |
| 87 | + } else if (element is Map<*, *>) { |
| 88 | + element.values.forEach { item -> |
| 89 | + item?.let { next.visit(it, collector) } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + abstract class IndexPathNode( |
| 96 | + protected val index: Int, |
| 97 | + next: PathNode |
| 98 | + ) : NonTerminalPathNode(next) { |
| 99 | + protected abstract fun normalisedIndex(list: List<*>): Int |
| 100 | + |
| 101 | + override fun visit(element: Any, collector: (Any) -> Unit) { |
| 102 | + if (element is List<*>) { |
| 103 | + val normalised = normalisedIndex(element) |
| 104 | + element.getOrNull(normalised)?.let { next.visit(it, collector) } |
| 105 | + } else if (element is Map<*, *>) { |
| 106 | + val value = element[index.toString()] |
| 107 | + value?.let { next.visit(it, collector) } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + class PositiveIndex(index: Int, next: PathNode) : IndexPathNode(index, next) { |
| 113 | + override fun normalisedIndex(list: List<*>): Int { |
| 114 | + return index |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + class NegativeIndex(index: Int, next: PathNode) : IndexPathNode(index, next) { |
| 119 | + override fun normalisedIndex(list: List<*>): Int { |
| 120 | + return list.size + index |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + object TerminalNode : PathNode() { |
| 125 | + override fun visit(element: Any, collector: (Any) -> Unit) { |
| 126 | + collector(element) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments