1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// x2j_findPath - utility functions to retrieve path to node in dot-notation
// Copyright 2012-2018 Charles Banning. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package x2j
import (
"strings"
"github.com/clbanning/mxj"
)
//----------------------------- find all paths to a key --------------------------------
// Want eventually to extract shortest path and call GetValuesAtKeyPath()
// This will get all the possible paths. These can be scanned for len(path) and sequence.
// Get all paths through the doc (in dot-notation) that terminate with the specified tag.
// Results can be used with ValuesAtTagPath() and ValuesFromTagPath().
func PathsForTag(doc string, key string) ([]string, error) {
m, err := mxj.NewMapXml([]byte(doc))
if err != nil {
return nil, err
}
ss := PathsForKey(m, key)
return ss, nil
}
// Extract the shortest path from all possible paths - from PathsForTag().
// Paths are strings using dot-notation.
func PathForTagShortest(doc string, key string) (string, error) {
m, err := mxj.NewMapXml([]byte(doc))
if err != nil {
return "", err
}
s := PathForKeyShortest(m, key)
return s, nil
}
// Get all paths through the doc (in dot-notation) that terminate with the specified tag.
// Results can be used with ValuesAtTagPath() and ValuesFromTagPath().
func BytePathsForTag(doc []byte, key string) ([]string, error) {
m, err := mxj.NewMapXml(doc)
if err != nil {
return nil, err
}
ss := PathsForKey(m, key)
return ss, nil
}
// Extract the shortest path from all possible paths - from PathsForTag().
// Paths are strings using dot-notation.
func BytePathForTagShortest(doc []byte, key string) (string, error) {
m, err := ByteDocToMap(doc)
if err != nil {
return "", err
}
s := PathForKeyShortest(m, key)
return s, nil
}
// Get all paths through the map (in dot-notation) that terminate with the specified key.
// Results can be used with ValuesAtKeyPath() and ValuesFromKeyPath().
func PathsForKey(m map[string]interface{}, key string) []string {
breadbasket := make(map[string]bool,0)
breadcrumb := ""
hasKeyPath(breadcrumb, m, key, &breadbasket)
if len(breadbasket) == 0 {
return nil
}
// unpack map keys to return
res := make([]string,len(breadbasket))
var i int
for k,_ := range breadbasket {
res[i] = k
i++
}
return res
}
// Extract the shortest path from all possible paths - from PathsForKey().
// Paths are strings using dot-notation.
func PathForKeyShortest(m map[string]interface{}, key string) string {
paths := PathsForKey(m,key)
lp := len(paths)
if lp == 0 {
return ""
}
if lp == 1 {
return paths[0]
}
shortest := paths[0]
shortestLen := len(strings.Split(shortest,"."))
for i := 1 ; i < len(paths) ; i++ {
vlen := len(strings.Split(paths[i],"."))
if vlen < shortestLen {
shortest = paths[i]
shortestLen = vlen
}
}
return shortest
}
// hasKeyPath - if the map 'key' exists append it to KeyPath.path and increment KeyPath.depth
// This is really just a breadcrumber that saves all trails that hit the prescribed 'key'.
func hasKeyPath(crumb string, iv interface{}, key string, basket *map[string]bool) {
switch iv.(type) {
case map[string]interface{}:
vv := iv.(map[string]interface{})
if _, ok := vv[key]; ok {
if crumb == "" {
crumb = key
} else {
crumb += "." + key
}
// *basket = append(*basket, crumb)
(*basket)[crumb] = true
}
// walk on down the path, key could occur again at deeper node
for k, v := range vv {
// create a new breadcrumb, add the one we're at to the crumb-trail
var nbc string
if crumb == "" {
nbc = k
} else {
nbc = crumb + "." + k
}
hasKeyPath(nbc, v, key, basket)
}
case []interface{}:
// crumb-trail doesn't change, pass it on
for _, v := range iv.([]interface{}) {
hasKeyPath(crumb, v, key, basket)
}
}
}
|