-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol.go
57 lines (47 loc) · 1.2 KB
/
protocol.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
49
50
51
52
53
54
55
56
57
// Package protocol provides fundamentals for each protocols.
package protocol
import (
"fmt"
"io"
"net/url"
"github.com/koron/nvgd/resource"
)
// Protocol is abstraction of methods to get source stream.
type Protocol interface {
Open(u *url.URL) (*resource.Resource, error)
}
// ProtocolFunc is Protocol wrapper for function.
type ProtocolFunc func(*url.URL) (*resource.Resource, error)
// Open opens URL as protocol.
func (f ProtocolFunc) Open(u *url.URL) (*resource.Resource, error) {
return f(u)
}
// Postable is set of methods for POST acceptable source/protocol.
type Postable interface {
Protocol
Post(u *url.URL, r io.Reader) (*resource.Resource, error)
}
var protocols = map[string]Protocol{}
// Register registers a protocol with name.
func Register(name string, p Protocol) error {
_, ok := protocols[name]
if ok {
return fmt.Errorf("duplicated protocol name %q", name)
}
protocols[name] = p
return nil
}
// MustRegister registers a protocol, panic if failed.
func MustRegister(name string, p Protocol) {
if err := Register(name, p); err != nil {
panic(err)
}
}
// Find finds a protocol.
func Find(name string) Protocol {
p, ok := protocols[name]
if !ok {
return nil
}
return p
}