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

Update the Network helper to return a tuple. #51

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,27 +398,29 @@ func encode(ch channel.Sender, rsps jmessages) (int, error) {
return len(bits), ch.Send(bits)
}

// Network guesses a network type for the specified address. The assignment of
// a network type uses the following heuristics:
// Network guesses a network type for the specified address and returns a tuple
// of that type and the address.
//
// The assignment of a network type uses the following heuristics:
//
// If s does not have the form [host]:port, the network is assigned as "unix".
// The network "unix" is also assigned if port == "", port contains characters
// other than ASCII letters, digits, and "-", or if host contains a "/".
//
// Otherwise, the network is assigned as "tcp". Note that this function does
// not verify whether the address is lexically valid.
func Network(s string) string {
func Network(s string) (network, address string) {
i := strings.LastIndex(s, ":")
if i < 0 {
return "unix"
return "unix", s
}
host, port := s[:i], s[i+1:]
if port == "" || !isServiceName(port) {
return "unix"
return "unix", s
} else if strings.IndexByte(host, '/') >= 0 {
return "unix"
return "unix", s
}
return "tcp"
return "tcp", s
}

// isServiceName reports whether s looks like a legal service name from the
Expand Down
2 changes: 1 addition & 1 deletion cmd/examples/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
log.Fatal("You must provide -server address to connect to")
}

conn, err := net.Dial(jrpc2.Network(*serverAddr), *serverAddr)
conn, err := net.Dial(jrpc2.Network(*serverAddr))
if err != nil {
log.Fatalf("Dial %q: %v", *serverAddr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/examples/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func main() {
},
}

lst, err := net.Listen(jrpc2.Network(*address), *address)
lst, err := net.Listen(jrpc2.Network(*address))
if err != nil {
log.Fatalln("Listen:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/jcall/jcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func main() {
} else if nc := chanutil.Framing(*chanFraming); nc == nil {
log.Fatalf("Unknown channel framing %q", *chanFraming)
} else {
ntype := jrpc2.Network(flag.Arg(0))
ntype, _ := jrpc2.Network(flag.Arg(0))
conn, err := net.DialTimeout(ntype, flag.Arg(0), *dialTimeout)
if err != nil {
log.Fatalf("Dial %q: %v", flag.Arg(0), err)
Expand Down
7 changes: 5 additions & 2 deletions jrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,9 +1012,12 @@ func TestNetwork(t *testing.T) {
{"localhost:http", "tcp"}, // host and service name
}
for _, test := range tests {
got := jrpc2.Network(test.input)
got, addr := jrpc2.Network(test.input)
if got != test.want {
t.Errorf("Network(%q): got %q, want %q", test.input, got, test.want)
t.Errorf("Network(%q) type: got %q, want %q", test.input, got, test.want)
}
if addr != test.input {
t.Errorf("Network(%q) addr: got %q, want %q", test.input, addr, test.input)
}
}
}
Expand Down