tokers commented on a change in pull request #1395:
URL: https://github.com/apache/apisix-dashboard/pull/1395#discussion_r567205704



##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -198,39 +201,36 @@ func (h *Handler) BatchDelete(c droplet.Context) 
(interface{}, error) {
        return nil, nil
 }
 
+type PatchInput struct {
+       ID      string `auto_read:"id,path"`
+       SubPath string `auto_read:"path,path"`
+       Body    []byte `auto_read:"@body"`
+}
+
 func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
-       input := c.Input().(*UpdateInput)
-       arr := strings.Split(input.ID, "/")
-       var subPath string
-       if len(arr) > 1 {
-               input.ID = arr[0]
-               subPath = arr[1]
-       }
+       input := c.Input().(*PatchInput)
+       reqBody := input.Body
+       ID := input.ID

Review comment:
       we should use variables started with letter in small case.

##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -198,39 +201,36 @@ func (h *Handler) BatchDelete(c droplet.Context) 
(interface{}, error) {
        return nil, nil
 }
 
+type PatchInput struct {
+       ID      string `auto_read:"id,path"`
+       SubPath string `auto_read:"path,path"`
+       Body    []byte `auto_read:"@body"`
+}
+
 func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
-       input := c.Input().(*UpdateInput)
-       arr := strings.Split(input.ID, "/")
-       var subPath string
-       if len(arr) > 1 {
-               input.ID = arr[0]
-               subPath = arr[1]
-       }
+       input := c.Input().(*PatchInput)
+       reqBody := input.Body
+       ID := input.ID
+       subPath := input.SubPath
 
-       stored, err := h.upstreamStore.Get(c.Context(), input.ID)
+       stored, err := h.upstreamStore.Get(c.Context(), ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
 
-       var patch jsonpatch.Patch
-       if subPath != "" {
-               patch = jsonpatch.Patch{
-                       Operations: []jsonpatch.PatchOperation{
-                               {Op: jsonpatch.Replace, Path: subPath, Value: 
c.Input()},
-                       },
-               }
-       } else {
-               patch, err = jsonpatch.MakePatch(stored, input.Upstream)
-               if err != nil {
-                       return handler.SpecCodeResponse(err), err
-               }
+       res, err := utils.MergePatch(stored, subPath, reqBody)
+
+       if err != nil {
+               return handler.SpecCodeResponse(err), err
        }
 
-       if err := patch.Apply(&stored); err != nil {
+       var upstream entity.Upstream
+       err = json.Unmarshal(res, &upstream)
+       if err != nil {

Review comment:
       Can be merged into:
   
   ```go
   if err := json.Unmarshal(res, &upstream); err != nil {
   ```

##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -314,3 +314,4 @@ func (h *Handler) listUpstreamNames(c droplet.Context) 
(interface{}, error) {
 
        return output, nil
 }
+

Review comment:
       Why keep an empty line here?

##########
File path: api/internal/handler/upstream/upstream.go
##########
@@ -198,39 +201,36 @@ func (h *Handler) BatchDelete(c droplet.Context) 
(interface{}, error) {
        return nil, nil
 }
 
+type PatchInput struct {
+       ID      string `auto_read:"id,path"`
+       SubPath string `auto_read:"path,path"`
+       Body    []byte `auto_read:"@body"`
+}
+
 func (h *Handler) Patch(c droplet.Context) (interface{}, error) {
-       input := c.Input().(*UpdateInput)
-       arr := strings.Split(input.ID, "/")
-       var subPath string
-       if len(arr) > 1 {
-               input.ID = arr[0]
-               subPath = arr[1]
-       }
+       input := c.Input().(*PatchInput)
+       reqBody := input.Body
+       ID := input.ID
+       subPath := input.SubPath
 
-       stored, err := h.upstreamStore.Get(c.Context(), input.ID)
+       stored, err := h.upstreamStore.Get(c.Context(), ID)
        if err != nil {
                return handler.SpecCodeResponse(err), err
        }
 
-       var patch jsonpatch.Patch
-       if subPath != "" {
-               patch = jsonpatch.Patch{
-                       Operations: []jsonpatch.PatchOperation{
-                               {Op: jsonpatch.Replace, Path: subPath, Value: 
c.Input()},
-                       },
-               }
-       } else {
-               patch, err = jsonpatch.MakePatch(stored, input.Upstream)
-               if err != nil {
-                       return handler.SpecCodeResponse(err), err
-               }
+       res, err := utils.MergePatch(stored, subPath, reqBody)
+
+       if err != nil {
+               return handler.SpecCodeResponse(err), err
        }
 
-       if err := patch.Apply(&stored); err != nil {
+       var upstream entity.Upstream
+       err = json.Unmarshal(res, &upstream)
+       if err != nil {

Review comment:
       Can be merged into:
   
   ```go
   if err := json.Unmarshal(res, &upstream); err != nil {
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to