liuxiran commented on pull request #1812:
URL: https://github.com/apache/apisix-dashboard/pull/1812#issuecomment-825440571


   > Hi, @liuxiran
   > In golang, you should distinguish `nil slice` and `empty slice`
   > 
   > ```
   > var x []*entity.Node
   > ```
   > 
   > is a nil slice.
   > 
   > ```
   > y := make([]*entity.Node, 0)
   > ```
   > 
   > is an empty slice. The length and value are 0.
   > If you use printf or println to print them, the result is both "[]".
   > 
   > In this situation, because the upstream.Nodes' type is interface{}.So it's 
a little more complex.
   > You can use
   > 
   > ```
   > reflect.ValueOf(upstream.Nodes).IsNil()
   > ```
   > 
   > to see the true value.
   
   @starsz Thanks for your detailed explanation, after added 
`reflect.ValueOf(upstream.Nodes).IsNil()` and debug again, I find out the 
confusion before:
   debug step:
   1. create an upstream with no nodes
   2. visit uptream page, call `GET /apisix/admin/upstreams`
   3. config the upstream ,call `GET /apisix/admin/upstream/:id`
   
   before the advice change, I got the logs:
   ![2021-04-23 
14-32-00屏幕截图](https://user-images.githubusercontent.com/2561857/115829386-853b0800-a441-11eb-8ee3-8703f220bc4e.png)
   
   after the advice change, I got the logs:
   ![2021-04-23 
14-39-50屏幕截图](https://user-images.githubusercontent.com/2561857/115829615-c7644980-a441-11eb-88c0-93964e82b626.png)
   
   The conclusion is that: 
   the original `upstream.nodes` type got from etcd is `[]interface{}`, and it 
is an empty slice, 
   it will be changed to type nodes , `[]*Node`, in FormatNodes `case 
[]interface{}` when I visit upstream page(step2), 
   and  the when I got the upstream info(step3), the data come from catch 
instead of stroge, that is to say, in this scene, the `upstream.nodes` type is 
already `[]*Node`.
   
   so the advice will solve the problem. 
   Thanks again for all your help, then I will try to add related test cases
   


-- 
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:
us...@infra.apache.org


Reply via email to