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

Wildcard (*) not working in maputil.GetByPath #109

Closed
shurco opened this issue Jul 12, 2023 · 1 comment
Closed

Wildcard (*) not working in maputil.GetByPath #109

shurco opened this issue Jul 12, 2023 · 1 comment
Assignees
Labels
bug:fixed bug Something isn't working
Milestone

Comments

@shurco
Copy link

shurco commented Jul 12, 2023

System (please complete the following information):

  • OS: linux
  • GO Version: 1.20.5
  • Pkg Version: 0.6.11

Describe the bug

Wildcard (*) not working in maputil.GetByPath

To Reproduce

My Body from API:

{
  "success": true,
  "result": {
    "total": 2,
    "records": [
      {
        "id": "59fab0fa-8f0a-4065-8863-1dae40166015"
      },
      {
        "id": "7c1bd7f9-2ef4-44c8-9756-2e85156ca58f"
      }
    ]
  }
}

This work:

var tcBody := map[string]any{
	"success":                  true,
	"result.records.0.id": "59fab0fa-8f0a-4065-8863-1dae40166015",
	"result.records.1.id": "7c1bd7f9-2ef4-44c8-9756-2e85156ca58f",
}

body, _ := io.ReadAll(response.Body)
var resp map[string]any
json.Unmarshal(body, &resp)
for key, want := range tcBody {
	val, _ := maputil.GetByPath(key, resp)
	if val != want {
		t.Errorf("Body key [%v] was incorrect, got: %v, want: %v", key, val, want)
	}
}

This doesn't work when use wildcard:

var tcBody := map[string]any{
"success":                 true,
"result.records.*.id": []String{
	"59fab0fa-8f0a-4065-8863-0dae40166015",
	"7c1bd7f9-2ef4-44c8-9756-0e85156ca58f",
}

body, _ := io.ReadAll(response.Body)
var resp map[string]any
json.Unmarshal(body, &resp)
for key, want := range tcBody {
	val, _ := maputil.GetByPath(key, resp)
	if val != want {
		t.Errorf("Body key [%v] was incorrect, got: %v, want: %v", key, val, want)
	}
}
@inhere inhere added bug:confirming Waiting for confirmation bug Something isn't working and removed bug:confirming Waiting for confirmation labels Jul 13, 2023
@inhere inhere added this to the v0.6.12 milestone Jul 19, 2023
@inhere
Copy link
Member

inhere commented Jul 25, 2023

Hi @shurco has been fixed in v0.6.12

val, _ := maputil.GetByPath("result.records.*.id", resp)

Note: !!! the val is []any, not a []string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:fixed bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants