Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name function with argument only works once #55

Closed
AlecY opened this issue Oct 23, 2020 · 1 comment
Closed

Name function with argument only works once #55

AlecY opened this issue Oct 23, 2020 · 1 comment

Comments

@AlecY
Copy link
Contributor

AlecY commented Oct 23, 2020

Test Case

func TestXPathName(t *testing.T) {
  doc, err := jsonquery.Parse(strings.NewReader(`{
  "Reviews": {
     "A Tale of Two Cities": "Not Bad"
  },
  "Books": [
    {
       "Title": "Of Mice and Men",
       "Price": 11
    },
    {
       "Title": "A Tale of Two Cities",
       "Price": 10
    }
  ]
}`,
		))
  assert.NoError(t, err)
  currentBookPrice, err := jsonquery.Query(doc, `/Books/*[Title=name(/Reviews/*)]/Price`)
  assert.NoError(t, err)
  assert.NotNil(t, currentBookPrice)
}

Explanation

When running the query the filter evaluates it's expression multiple times. However the name() function always runs Select(t) on the same query. Because query's contain their own iterator the first time the name function is evaluated it returns the name of the first match, and the second time it returns the second match. This can be demonstrated in the example by removing "Of Mice and Men" from the JSON document which will allow the test to pass.

Suggestion

I admit I don't fully understand the expression logic, but it seems as like when a function like name calls Select(t) on its argument it should call Clone() first to avoid using the same iterator.

@zhengchun
Copy link
Contributor

PR #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants