Skip to content

Selector returns empty result on typed input #26

@ndsboy

Description

@ndsboy

First of all, thank you for this library, it's very neat and a pleasure to use.

Today I stumbled upon a limitation: using a slice selector on typed slices will always return empty results. A quick and dirty code to replicate with v0.11.0:

package main

import (
	"fmt"

	"github.com/theory/jsonpath"
)

func main() {
	env := map[string]any{
		"untyped": []any{"1", "2"},
		"typed":   []string{"1", "2"},
	}

	untyped := jsonpath.MustParse("$.untyped[0]")
	typed := jsonpath.MustParse("$.typed[0]")

	fmt.Printf("untyped: %v\n", untyped.Select(env))
	fmt.Printf("typed: %v\n", typed.Select(env))
}

Output:

untyped: [1]
typed: []

I think the reason for this is the checked type assertion like in this line because in go e.g. []any does not match []string.

I can understand that this library is intended for being used on parsed JSON which always uses []any and map[string]any, and I cannot think of an easy solution that would support all types dynamically without messing up the clean code. The simple workaround I'm using is marshalling and unmarshalling the input, then it works flawlessly.

If you want to support the selection of typed arrays, that would be really awesome. But as I said I can totally understand if this is intentional or not desired, in that case a remark of this limitation on the .Select() documentation would be helpful for future adopters. I'm sorry if this is already documented but I missed it!

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions