Skip to content
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

fix: nodes format not works #847

Merged
merged 7 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/internal/core/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Timeout struct {
type Node struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Weight int `json:"weight,omitempty"`
Weight int `json:"weight"`
Metadata interface{} `json:"metadata,omitempty"`
}

Expand Down
45 changes: 35 additions & 10 deletions api/internal/core/entity/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,30 @@
package entity

import (
"log"
"strconv"
"strings"

"github.com/apisix/manager-api/log"
)

func NodesFormat(obj interface{}) interface{} {
var nodes []*Node
if value, ok := obj.(map[string]float64); ok {
switch objType := obj.(type) {
case map[string]float64:
log.Infof("nodes type: %v", objType)
var strArr []string
value := obj.(map[string]float64)
for key, val := range value {
node := &Node{}
strArr = strings.Split(key, ":")
tokers marked this conversation as resolved.
Show resolved Hide resolved
if len(strArr) != 2 {
log.Println("length of string array is not 2")
log.Warn("length of string array is not 2")
return obj
}

port, err := strconv.Atoi(strArr[1])
if err != nil {
log.Println("parse int fail:", err)
log.Errorf("parse int fail:", err)
tokers marked this conversation as resolved.
Show resolved Hide resolved
return obj
}

Expand All @@ -46,13 +50,35 @@ func NodesFormat(obj interface{}) interface{} {
nodes = append(nodes, node)
tokers marked this conversation as resolved.
Show resolved Hide resolved
}
return nodes
}
case map[string]interface{}:
log.Infof("nodes type: %v", objType)
var strArr []string
value := obj.(map[string]interface{})
for key, val := range value {
node := &Node{}
strArr = strings.Split(key, ":")
tokers marked this conversation as resolved.
Show resolved Hide resolved
if len(strArr) != 2 {
log.Warn("length of string array is not 2")
return obj
}

if nodes, ok := obj.([]*Node); ok {
return nodes
}
port, err := strconv.Atoi(strArr[1])
if err != nil {
log.Errorf("parse int fail:", err)
return obj
}

if list, ok := obj.([]interface{}); ok {
node.Host = strArr[0]
node.Port = port
node.Weight = int(val.(float64))
tokers marked this conversation as resolved.
Show resolved Hide resolved
nodes = append(nodes, node)
}
return nodes
case []*Node:
log.Infof("nodes type: %v", objType)
return nodes
case []interface{}:
list := obj.([]interface{})
for _, v := range list {
val := v.(map[string]interface{})
node := &Node{}
Expand All @@ -61,7 +87,6 @@ func NodesFormat(obj interface{}) interface{} {
node.Weight = int(val["weight"].(float64))
nodes = append(nodes, node)
}

return nodes
}

Expand Down
67 changes: 55 additions & 12 deletions api/internal/core/entity/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,62 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConsumer(t *testing.T) {
nodesStr := `{
"127.0.0.1:8080": 1
}`
nodesMap := map[string]float64{}
err := json.Unmarshal([]byte(nodesStr), &nodesMap)
func TestNodesFormat(t *testing.T){
// route data saved in ETCD
routeStr := `{
"uris": ["/*"],
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "127.0.0.1",
"port": 80,
"weight": 0
}]
}
}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


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

res := NodesFormat(nodesMap)
nodes := res.([]*Node)
// nodes format
nodes := NodesFormat(route.Upstream.Nodes)

assert.Equal(t, 1, len(nodes))
assert.Equal(t, "127.0.0.1", nodes[0].Host)
assert.Equal(t, 8080, nodes[0].Port)
assert.Equal(t, 1, nodes[0].Weight)
// json encode for client
res, err := json.Marshal(nodes)
assert.Nil(t,err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a space after ,.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

jsonStr := string(res)
assert.Contains(t, jsonStr, `"weight":0`)
assert.Contains(t, jsonStr, `"port":80`)
assert.Contains(t, jsonStr, `"host":"127.0.0.1"`)
}

func TestNodesFormat_Map(t *testing.T){
// route data saved in ETCD
routeStr := `{
"uris": ["/*"],
"upstream": {
"type": "roundrobin",
"nodes": {"127.0.0.1:8080": 0}
}
}`

// bind struct
var route Route
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant spaces after var.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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":8080`)
assert.Contains(t, jsonStr, `"host":"127.0.0.1"`)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant empty line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}