-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.go
More file actions
78 lines (71 loc) · 2.01 KB
/
peer.go
File metadata and controls
78 lines (71 loc) · 2.01 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package dndm
import (
"net/url"
"strings"
)
// Peer defines an interface for network peer entities, encapsulating methods
// that provide details about network connection points such as URL components
// and query parameters.
//
// Peer is identified URI such as [schema]://[address]/[path][?params&...].
type Peer interface {
String() string
Values() url.Values
Address() string
Path() string
Scheme() string
// Equal compares this Peer to another Peer interface to determine if they represent
// the same peer.
Equal(v Peer) bool
// HasPrefix compares this Peer path to Route Path.
HasPrefix(r Route) bool
}
type PeerImpl struct {
scheme string
addr string
path string
args url.Values
}
func (r PeerImpl) String() string {
u := url.URL{
Scheme: r.scheme,
Host: r.addr,
Path: r.path,
RawQuery: r.args.Encode(),
}
return u.String()
}
// NewPeer constructs a new PeerImpl object given its components: scheme, address, path,
// and arguments (query parameters). It initializes the PeerImpl with these components.
func NewPeer(scheme, address, path string, args url.Values) (PeerImpl, error) {
return PeerImpl{
scheme: scheme,
addr: address,
path: path,
args: args,
}, nil
}
// PeerFromString parses a string containing a URL into a Peer object. It extracts
// the scheme, host, path, and query parameters from the string.
func PeerFromString(peer string) (PeerImpl, error) {
u, err := url.Parse(peer)
if err != nil {
return PeerImpl{}, err
}
return PeerImpl{
scheme: u.Scheme,
addr: u.Host,
path: strings.TrimPrefix(u.Path, "/"),
args: u.Query(),
}, nil
}
func (p PeerImpl) Values() url.Values { return p.args }
func (p PeerImpl) Address() string { return p.addr }
func (p PeerImpl) Path() string { return p.path }
func (p PeerImpl) Scheme() string { return p.scheme }
func (p PeerImpl) Equal(v Peer) bool {
return p.scheme == v.Scheme() && p.addr == v.Address()
}
func (p PeerImpl) HasPrefix(r Route) bool {
return strings.HasPrefix(r.Path(), p.path)
}