Skip to content

Commit

Permalink
fix: upstream nodes metadata miss (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baoyuantop authored Mar 17, 2023
1 parent bf61459 commit 4a2b50f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/internal/core/entity/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func NodesFormat(obj interface{}) interface{} {
if _, ok := val["priority"]; ok {
node.Priority = int(val["priority"].(float64))
}

if _, ok := val["metadata"]; ok {
node.Metadata = val["metadata"].(map[string]interface{})
}

nodes = append(nodes, node)
}
return nodes
Expand Down
37 changes: 37 additions & 0 deletions api/internal/core/entity/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,40 @@ func TestMapKV2Node(t *testing.T) {
})
}
}

func TestNodesFormatWithMetadata(t *testing.T) {
// route data saved in ETCD
routeStr := `{
"uris": ["/*"],
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 80,
"weight": 0,
"priority":10,
"metadata": {
"name": "test"
}
}]
}
}`

// bind struct
var route Route
err := json.Unmarshal([]byte(routeStr), &route)
assert.Nil(t, err)

// nodes format
nodes := NodesFormat(route.Upstream.Nodes)

// json encode for client
res, err := json.Marshal(nodes)
assert.Nil(t, err)
jsonStr := string(res)
assert.Contains(t, jsonStr, `"weight":0`)
assert.Contains(t, jsonStr, `"port":80`)
assert.Contains(t, jsonStr, `"host":"127.0.0.1"`)
assert.Contains(t, jsonStr, `"priority":10`)
assert.Contains(t, jsonStr, `"metadata":{"name":"test"}`)
}

0 comments on commit 4a2b50f

Please sign in to comment.