Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jun 6, 2024
1 parent 616a245 commit 509c5fb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
12 changes: 6 additions & 6 deletions pkg/utils/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,19 @@ func ParseKey(name string, input map[string]any) ([]byte, string, error) {
}

// ParseHexKeys decodes hexadecimal keys to bytes if the format is "hex".
func ParseHexKeys(format string, keys ...*string) error {
func ParseHexKeys(format string, keys []string) (hexStrings []string, err error) {
if format != "hex" {
return nil
return keys, nil
}

for _, key := range keys {
keyBytes, err := hex.DecodeString(*key)
keyBytes, err := hex.DecodeString(key)
if err != nil {
return err
return keys, err
}
*key = string(keyBytes)
hexStrings = append(hexStrings, string(keyBytes))
}
return nil
return hexStrings, nil
}

// ReadJSON reads a JSON data from r and then closes it.
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/apiutil/apiutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,21 @@ func TestGetIPPortFromHTTPRequest(t *testing.T) {
re.Equal(testCase.port, port, "case %d", idx)
}
}

func TestParseHexKeys(t *testing.T) {
re := require.New(t)
hexKeys := []string{"", "67", "0001020304050607", "08090a0b0c0d0e0f", "f0f1f2f3f4f5f6f7"}
parseKeys, err := ParseHexKeys("hex", hexKeys)
re.NoError(err)
re.Equal(parseKeys, []string{"", "g", "\x00\x01\x02\x03\x04\x05\x06\x07", "\x08\t\n\x0b\x0c\r\x0e\x0f", "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7"})

Check failure on line 213 in pkg/utils/apiutil/apiutil_test.go

View workflow job for this annotation

GitHub Actions / statics

expected-actual: need to reverse actual and expected values (testifylint)
// Test for other format
hexKeys = []string{"hello"}
parseKeys, err = ParseHexKeys("other", hexKeys)
re.NoError(err)
re.Equal(parseKeys, []string{"hello"})

Check failure on line 218 in pkg/utils/apiutil/apiutil_test.go

View workflow job for this annotation

GitHub Actions / statics

expected-actual: need to reverse actual and expected values (testifylint)
// Test for wrong keys
hexKeys = []string{"world"}
parseKeys, err = ParseHexKeys("hex", hexKeys)
re.Error(err)
re.Equal(parseKeys, []string{"world"})

Check failure on line 223 in pkg/utils/apiutil/apiutil_test.go

View workflow job for this annotation

GitHub Actions / statics

expected-actual: need to reverse actual and expected values (testifylint)
}
14 changes: 7 additions & 7 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) {
return
}
// decode hex if query has params with hex format
err = apiutil.ParseHexKeys(r.URL.Query().Get("format"), &key)
params := []string{key}
params, err = apiutil.ParseHexKeys(r.URL.Query().Get("format"), params)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

regionInfo := rc.GetRegionByKey([]byte(key))
regionInfo := rc.GetRegionByKey([]byte(params[0]))
b, err := response.MarshalRegionInfoJSON(r.Context(), regionInfo)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -169,11 +170,10 @@ func (h *regionsHandler) GetRegions(w http.ResponseWriter, r *http.Request) {
// @Router /regions/key [get]
func (h *regionsHandler) ScanRegions(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
startKey := r.URL.Query().Get("key")
endKey := r.URL.Query().Get("end_key")

var err error
// decode hex if query has params with hex format
err := apiutil.ParseHexKeys(r.URL.Query().Get("format"), &startKey, &endKey)
params := []string{r.URL.Query().Get("key"), r.URL.Query().Get("end_key")}
params, err = apiutil.ParseHexKeys(r.URL.Query().Get("format"), params)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
Expand All @@ -185,7 +185,7 @@ func (h *regionsHandler) ScanRegions(w http.ResponseWriter, r *http.Request) {
return
}

regions := rc.ScanRegions([]byte(startKey), []byte(endKey), limit)
regions := rc.ScanRegions([]byte(params[0]), []byte(params[1]), limit)
b, err := response.MarshalRegionsInfoJSON(r.Context(), regions)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down

0 comments on commit 509c5fb

Please sign in to comment.