Skip to content

Commit

Permalink
all test cases passing again, we can probably refactor, but all works
Browse files Browse the repository at this point in the history
  • Loading branch information
olliephillips committed Apr 1, 2023
1 parent 7f44ce7 commit 9bfc1e8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ func obtainRoot(data []byte, rootKey string) ([]byte, bool) {
reg := regexp.MustCompile(regex)
matches := reg.FindAllStringSubmatch(string(data), 1)

if len(matches) == 0 {
// not found
return []byte{}, false
}

// check and employfall back
var check interface{}
if err := json.Unmarshal([]byte(matches[0][1]), &check); err != nil {
return obtainRootFallback(data, rootKey)
}

return []byte(matches[0][1]), true
}

// temp fix for issue2 (https://github.com/spoonboy-io/switch/issues/2)
// the regex is the only difference, just very simple to drop this
// in as a fallback for now, which use if the bytes are unparsable
// after first
func obtainRootFallback(data []byte, rootKey string) ([]byte, bool) {
// helper to extract the array which could be mounted as object key
// traversing the doc to the key in a unmarshaled map was much more work
// so we do it in the JSON data with a regex
regex := fmt.Sprintf(`(?mU)"%s":\s*?(\[[[:ascii:]]*\])`, rootKey)
//regex := fmt.Sprintf(`(?m)"%s":\s*?(\[.*])`, rootKey)
reg := regexp.MustCompile(regex)
matches := reg.FindAllStringSubmatch(string(data), 1)

if len(matches) == 0 {
// not found
return []byte{}, false
Expand Down

0 comments on commit 9bfc1e8

Please sign in to comment.