Skip to content
This repository was archived by the owner on Oct 14, 2021. It is now read-only.

Commit 60d3c8b

Browse files
authored
Implementing RFC-8: Add TCP Protocol (#11)
* Implementing RFC-8 Signed-off-by: bokket <3100563328@qq.com> * Use <host>:<port> as addr Signed-off-by: bokket <3100563328@qq.com>
1 parent b69ddae commit 60d3c8b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

endpoint.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
ProtocolHTTPS = "https"
1414
// ProtocolFile is the file endpoint protocol
1515
ProtocolFile = "file"
16+
// ProtocolTCP is the tcp endpoint protocol
17+
ProtocolTCP = "tcp"
1618
)
1719

1820
// Parse will parse config string to create a endpoint Endpoint.
@@ -44,6 +46,13 @@ func Parse(cfg string) (p Endpoint, err error) {
4446
// See issue: https://github.com/beyondstorage/go-endpoint/issues/3
4547
path := strings.Join(s[1:], ":")
4648
return NewFile(path), nil
49+
case ProtocolTCP:
50+
//See issue: https://github.com/beyondstorage/go-endpoint/issues/7
51+
host, port, err := parseHostPort(s[1:])
52+
if err != nil {
53+
return Endpoint{}, &Error{"parse", ErrInvalidValue, s[0], []string{s[1], s[2]}}
54+
}
55+
return NewTCP(host, port), nil
4756
default:
4857
return Endpoint{}, &Error{"parse", ErrUnsupportedProtocol, s[0], nil}
4958
}
@@ -95,6 +104,13 @@ func NewFile(path string) Endpoint {
95104
}
96105
}
97106

107+
func NewTCP(host string, port int) Endpoint {
108+
return Endpoint{
109+
protocol: ProtocolTCP,
110+
args: hostPort{host, port},
111+
}
112+
}
113+
98114
func (p Endpoint) Protocol() string {
99115
return p.protocol
100116
}
@@ -152,3 +168,16 @@ func (p Endpoint) File() (path string) {
152168

153169
return p.args.(string)
154170
}
171+
172+
func (p Endpoint) TCP() (addr, host string, port int) {
173+
if p.protocol != ProtocolTCP {
174+
panic(Error{
175+
Op: "tcp",
176+
Err: ErrInvalidValue,
177+
Protocol: p.protocol,
178+
Values: p.args,
179+
})
180+
}
181+
hp := p.args.(hostPort)
182+
return fmt.Sprintf("%s:%d", hp.host, hp.port), hp.host, hp.port
183+
}

endpoint_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ func TestParse(t *testing.T) {
7575
Endpoint{ProtocolFile, "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestStorage_Stat286526883\\001\\199446694"},
7676
nil,
7777
},
78+
{
79+
"normal tcp",
80+
"tcp:127.0.0.1:8000",
81+
Endpoint{ProtocolTCP, hostPort{"127.0.0.1", 8000}},
82+
nil,
83+
},
84+
{
85+
"wrong port number in tcp",
86+
"tcp:127.0.0.1:xxx",
87+
Endpoint{},
88+
ErrInvalidValue,
89+
},
7890
}
7991

8092
for _, tt := range cases {
@@ -108,12 +120,25 @@ func TestNewHTTPS(t *testing.T) {
108120
)
109121
}
110122

123+
func TestNewTCP(t *testing.T) {
124+
assert.Equal(t,
125+
Endpoint{ProtocolTCP, hostPort{"127.0.0.1", 8000}},
126+
NewTCP("127.0.0.1", 8000),
127+
)
128+
}
129+
111130
func TestEndpoint_Protocol(t *testing.T) {
112131
ep := NewFile("/test")
113132

114133
assert.Equal(t, ProtocolFile, ep.Protocol())
115134
}
116135

136+
func TestEndpoint_Protocol2(t *testing.T) {
137+
ep := NewTCP("127.0.0.1", 8000)
138+
139+
assert.Equal(t, ProtocolTCP, ep.Protocol())
140+
}
141+
117142
func TestEndpoint_String(t *testing.T) {
118143
cases := []struct {
119144
name string
@@ -145,6 +170,11 @@ func TestEndpoint_String(t *testing.T) {
145170
Endpoint{ProtocolHTTPS, hostPort{"example.com", 4433}},
146171
"https:example.com:4433",
147172
},
173+
{
174+
"tcp with port",
175+
Endpoint{ProtocolTCP, hostPort{"127.0.0.1", 8000}},
176+
"tcp:127.0.0.1:8000",
177+
},
148178
}
149179

150180
for _, tt := range cases {
@@ -164,6 +194,10 @@ func TestEndpoint(t *testing.T) {
164194
p.HTTPS()
165195
})
166196

197+
assert.Panics(t, func() {
198+
p.TCP()
199+
})
200+
167201
assert.Equal(t, "/test", p.File())
168202
}
169203

@@ -197,6 +231,15 @@ func TestEndpoint_HTTPS(t *testing.T) {
197231
assert.Equal(t, 4433, port)
198232
}
199233

234+
func TestEndpoint_TCP(t *testing.T) {
235+
p := NewTCP("127.0.0.1", 8000)
236+
237+
addr, host, port := p.TCP()
238+
assert.Equal(t, "127.0.0.1:8000", addr)
239+
assert.Equal(t, "127.0.0.1", host)
240+
assert.Equal(t, 8000, port)
241+
}
242+
200243
func ExampleParse() {
201244
ep, err := Parse("http:example.com")
202245
if err != nil {
@@ -217,6 +260,11 @@ func ExampleParse() {
217260
case ProtocolFile:
218261
path := ep.File()
219262
log.Println("path: ", path)
263+
case ProtocolTCP:
264+
addr, host, port := ep.TCP()
265+
log.Println("addr:", addr)
266+
log.Println("host:", host)
267+
log.Println("port", port)
220268
default:
221269
panic("unsupported protocol")
222270
}

0 commit comments

Comments
 (0)