-
Notifications
You must be signed in to change notification settings - Fork 1
fix: normalize --label flag from key=value to key:value for APISIX API #13
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ package cmdutil | |||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| "github.com/api7/a6/pkg/api" | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -79,3 +80,15 @@ func IsOptionalResourceError(err error) bool { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return apiErr.StatusCode == 400 || apiErr.StatusCode == 404 | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // NormalizeLabel converts "key=value" to "key:value" for the APISIX Admin API. | ||||||||||||||||||||||||||||||||||||||||
| func NormalizeLabel(label string) string { | ||||||||||||||||||||||||||||||||||||||||
| if label == "" { | ||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| parts := strings.SplitN(label, "=", 2) | ||||||||||||||||||||||||||||||||||||||||
| if len(parts) == 2 { | ||||||||||||||||||||||||||||||||||||||||
| return parts[0] + ":" + parts[1] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return label | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+89
to
+93
|
||||||||||||||||||||||||||||||||||||||||
| parts := strings.SplitN(label, "=", 2) | |
| if len(parts) == 2 { | |
| return parts[0] + ":" + parts[1] | |
| } | |
| return label | |
| parts := strings.SplitN(label, "=", 2) | |
| if len(parts) != 2 { | |
| return label | |
| } | |
| key := strings.TrimSpace(parts[0]) | |
| value := strings.TrimSpace(parts[1]) | |
| if key == "" || value == "" { | |
| // Leave invalid/ambiguous labels unchanged so callers can surface a clear error. | |
| return label | |
| } | |
| return key + ":" + value |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package cmdutil | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNormalizeLabel(t *testing.T) { | ||
| tests := []struct { | ||
| input string | ||
| expected string | ||
| }{ | ||
| {"", ""}, | ||
| {"env=test", "env:test"}, | ||
| {"env=prod", "env:prod"}, | ||
| {"team=backend", "team:backend"}, | ||
| {"env:test", "env:test"}, | ||
| {"env", "env"}, | ||
| {"key=value=extra", "key:value=extra"}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.input, func(t *testing.T) { | ||
| assert.Equal(t, tc.expected, NormalizeLabel(tc.input)) | ||
| }) | ||
|
Comment on lines
+22
to
+25
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label-filter test doesn't actually validate filtering behavior: the mock server always returns only the matching route, regardless of what label query was sent. That means the test would still pass even if the command ignored
--labelentirely (as long as the query string check flips the boolean). Consider having the round tripper return different bodies depending on thelabelquery (filtered vs unfiltered) and re-adding an assertion that non-matching routes are not present in the output.