|  | 
|  | 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 | +        fun fromString(path: String): JsonCollectionPath { | 
|  | 31 | +            val segments = path.split('.') | 
|  | 32 | +                .reversed() // we build the path backwards | 
|  | 33 | + | 
|  | 34 | +            var node: PathNode = PathNode.TerminalNode | 
|  | 35 | +            segments.forEach { segment -> | 
|  | 36 | +                node = segment.toPathNode(node) | 
|  | 37 | +            } | 
|  | 38 | + | 
|  | 39 | +            return JsonCollectionPath(node, path) | 
|  | 40 | +        } | 
|  | 41 | + | 
|  | 42 | +        private fun String.toPathNode(next: PathNode): PathNode { | 
|  | 43 | +            if (this == "*") { | 
|  | 44 | +                return PathNode.Wildcard(next) | 
|  | 45 | +            } | 
|  | 46 | + | 
|  | 47 | +            val index = this.toIntOrNull() | 
|  | 48 | +            if (index != null) { | 
|  | 49 | +                return if (index < 0) { | 
|  | 50 | +                    PathNode.NegativeIndex(index, next) | 
|  | 51 | +                } else { | 
|  | 52 | +                    PathNode.PositiveIndex(index, next) | 
|  | 53 | +                } | 
|  | 54 | +            } | 
|  | 55 | + | 
|  | 56 | +            return PathNode.Property(this, next) | 
|  | 57 | +        } | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    private sealed class PathNode { | 
|  | 61 | +        abstract fun visit(element: Any, collector: (Any) -> Unit) | 
|  | 62 | + | 
|  | 63 | +        abstract class NonTerminalPathNode(protected val next: PathNode) : PathNode() | 
|  | 64 | + | 
|  | 65 | +        class Property(val name: String, next: PathNode) : NonTerminalPathNode(next) { | 
|  | 66 | +            override fun visit(element: Any, collector: (Any) -> Unit) { | 
|  | 67 | +                if (element is Map<*, *>) { | 
|  | 68 | +                    element[name]?.let { next.visit(it, collector) } | 
|  | 69 | +                } | 
|  | 70 | +            } | 
|  | 71 | + | 
|  | 72 | +            override fun toString(): String = name | 
|  | 73 | +        } | 
|  | 74 | + | 
|  | 75 | +        class Wildcard(next: PathNode) : NonTerminalPathNode(next) { | 
|  | 76 | +            override fun visit(element: Any, collector: (Any) -> Unit) { | 
|  | 77 | +                if (element is Iterable<*>) { | 
|  | 78 | +                    element.forEach { item -> | 
|  | 79 | +                        item?.let { next.visit(it, collector) } | 
|  | 80 | +                    } | 
|  | 81 | +                } else if (element is Map<*, *>) { | 
|  | 82 | +                    element.values.forEach { item -> | 
|  | 83 | +                        item?.let { next.visit(it, collector) } | 
|  | 84 | +                    } | 
|  | 85 | +                } | 
|  | 86 | +            } | 
|  | 87 | +        } | 
|  | 88 | + | 
|  | 89 | +        abstract class IndexPathNode( | 
|  | 90 | +            protected val index: Int, | 
|  | 91 | +            next: PathNode | 
|  | 92 | +        ) : NonTerminalPathNode(next) { | 
|  | 93 | +            protected abstract fun normalisedIndex(list: List<*>): Int | 
|  | 94 | + | 
|  | 95 | +            override fun visit(element: Any, collector: (Any) -> Unit) { | 
|  | 96 | +                if (element is List<*>) { | 
|  | 97 | +                    val normalised = normalisedIndex(element) | 
|  | 98 | +                    element.getOrNull(normalised)?.let { next.visit(it, collector) } | 
|  | 99 | +                } else if (element is Map<*, *>) { | 
|  | 100 | +                    val value = element[index.toString()] | 
|  | 101 | +                    value?.let { next.visit(it, collector) } | 
|  | 102 | +                } | 
|  | 103 | +            } | 
|  | 104 | +        } | 
|  | 105 | + | 
|  | 106 | +        class PositiveIndex(index: Int, next: PathNode) : IndexPathNode(index, next) { | 
|  | 107 | +            override fun normalisedIndex(list: List<*>): Int { | 
|  | 108 | +                return index | 
|  | 109 | +            } | 
|  | 110 | +        } | 
|  | 111 | + | 
|  | 112 | +        class NegativeIndex(index: Int, next: PathNode) : IndexPathNode(index, next) { | 
|  | 113 | +            override fun normalisedIndex(list: List<*>): Int { | 
|  | 114 | +                return list.size + index | 
|  | 115 | +            } | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        object TerminalNode : PathNode() { | 
|  | 119 | +            override fun visit(element: Any, collector: (Any) -> Unit) { | 
|  | 120 | +                collector(element) | 
|  | 121 | +            } | 
|  | 122 | +        } | 
|  | 123 | +    } | 
|  | 124 | +} | 
0 commit comments