Skip to content

Commit

Permalink
fix: fix endpoints parsing in Go Client (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
imp2002 committed Apr 6, 2023
1 parent 54ce7d9 commit 0c9a9b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
47 changes: 25 additions & 22 deletions golang/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,34 @@ func ParseAddress(address *v2.Address) string {
func ParseTarget(target string) (*v2.Endpoints, error) {
ret := &v2.Endpoints{
Scheme: v2.AddressScheme_DOMAIN_NAME,
Addresses: []*v2.Address{
{
Host: "",
Port: 80,
},
},
}
path := target
u, err := url.Parse(target)
if err != nil {
path = target
ret.Scheme = v2.AddressScheme_IPv4
} else {
if u.Host != "" {
path = u.Host
addressRawList := strings.Split(target, ";")
for _, path := range addressRawList {
if len(path) == 0 {
continue
}
}
paths := strings.Split(path, ":")
if len(paths) > 1 {
if port, err2 := strconv.ParseInt(paths[1], 10, 32); err2 == nil {
ret.Addresses[0].Port = int32(port)
address := &v2.Address{
Host: "",
Port: 80,
}
ret.Addresses[0].Host = paths[0]
} else {
return nil, fmt.Errorf("parse target failed, target=%s", target)
if u, err := url.Parse(path); err != nil {
address.Host = path
ret.Scheme = v2.AddressScheme_IPv4
} else {
if u.Host != "" {
address.Host = u.Host
}
}
paths := strings.Split(path, ":")
if len(paths) > 1 {
if port, err2 := strconv.ParseInt(paths[1], 10, 32); err2 == nil {
address.Port = int32(port)
}
address.Host = paths[0]
} else {
return nil, fmt.Errorf("parse target failed, target=%s", target)
}
ret.Addresses = append(ret.Addresses, address)
}
return ret, nil
}
Expand Down
20 changes: 20 additions & 0 deletions golang/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ func TestParseTarget(t *testing.T) {
if err != nil {
t.Error(err)
}

endpointsExpect := &v2.Endpoints{
Scheme: v2.AddressScheme_IPv4,
Addresses: []*v2.Address{
{
Host: "127.0.0.1",
Port: 80,
},
{
Host: "127.0.0.1",
Port: 81,
},
},
}
endpoints, err := ParseTarget("127.0.0.1:80;127.0.0.1:81;")
if err != nil {
t.Error(err)
} else if !CompareEndpoints(endpointsExpect, endpoints) {
t.Errorf("Expected endpoints: %v, but got: %v", endpointsExpect, endpoints)
}
}

func TestMatchMessageType(t *testing.T) {
Expand Down

0 comments on commit 0c9a9b3

Please sign in to comment.