-
Notifications
You must be signed in to change notification settings - Fork 3
/
target.go
48 lines (40 loc) · 1 KB
/
target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cloudmap
import (
"errors"
"fmt"
"net/url"
grpcresolver "google.golang.org/grpc/resolver"
)
// BuildTarget builds grpc target string with given namespace and service.
func BuildTarget(namespace, service string) string {
return fmt.Sprintf(
"%s://%s/%s", Scheme, url.PathEscape(namespace), url.PathEscape(service),
)
}
type target struct {
namespace string
service string
}
func parseTarget(t grpcresolver.Target) (*target, error) {
if t.URL.Scheme != Scheme {
return nil, fmt.Errorf("unexpected scheme: %s", t.URL.Scheme)
}
namespace, err := url.PathUnescape(t.URL.Host)
if err != nil {
return nil, fmt.Errorf("cannot parse namespace: %v", err)
}
if namespace == "" {
return nil, errors.New("namespace is required")
}
service, err := url.PathUnescape(t.Endpoint())
if err != nil {
return nil, fmt.Errorf("cannot parse service: %v", err)
}
if service == "" {
return nil, errors.New("service is required")
}
return &target{
namespace: namespace,
service: service,
}, nil
}