Skip to content

Commit

Permalink
testutils: revert to call url.Parse and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kylejb committed Dec 15, 2022
1 parent 3f16fcf commit 43c8130
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions internal/testutils/parse_url.go
Expand Up @@ -20,32 +20,24 @@ package testutils

import (
"fmt"
"net"
"net/url"
"strings"
)

// Assume url.Parse may fail when rawTarget represents IPV4 or IPV6.
func parseTargetAsIP(rawTarget string, urlParseError error) *url.URL {
ip := net.ParseIP(rawTarget) // TODO (easwars): should this be added to grpc.parseTarget?
if ip == nil {
panic(fmt.Sprintf("Error parsing target(%s): %v", rawTarget, urlParseError))
}
return &url.URL{
Path: ip.String(),
}
}

// MustParseURL helps create URL struct for resolver.Target by parsing target.
// Target can be represented as either URL, IPV4, or IPV6.
// MustParseURL uses RFC 3986 semantics to parse given target into a
// `URL` struct for `resolver.Target.URL`. The leading "/" are stripped
// to avoid breaking existing resolver implementations.
// See resolver.Target.ToEndpoint() for more information.
//
// rawTarget represents either URL, IPv4, or IPv6 string.
func MustParseURL(rawTarget string) *url.URL {
u, err := url.Parse(rawTarget)
if err != nil {
return parseTargetAsIP(rawTarget, err)
panic(fmt.Sprintf("Error parsing target(%s): %v", rawTarget, err))
}
return &url.URL{
Scheme: u.Scheme,
Path: strings.TrimPrefix(u.Path, "/"),
Opaque: u.Opaque,
Opaque: strings.TrimPrefix(u.Opaque, "/"),
}
}

0 comments on commit 43c8130

Please sign in to comment.