-
Notifications
You must be signed in to change notification settings - Fork 3
/
register.go
45 lines (35 loc) · 909 Bytes
/
register.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
package formstream
import (
"fmt"
)
// Register registers a stream hook with the given name.
func (p *Parser) Register(name string, fn StreamHookFunc, options ...RegisterOption) error {
if _, ok := p.hookMap[name]; ok {
return DuplicateHookNameError{Name: name}
}
c := ®isterConfig{}
for _, opt := range options {
opt(c)
}
p.hookMap[name] = streamHook{
fn: fn,
requireParts: c.requireParts,
}
return nil
}
type DuplicateHookNameError struct {
Name string
}
func (e DuplicateHookNameError) Error() string {
return fmt.Sprintf("duplicate hook name: %s", e.Name)
}
type registerConfig struct {
requireParts []string
}
type RegisterOption func(*registerConfig)
// WithRequiredPart sets the required part names for the stream hook.
func WithRequiredPart(name string) RegisterOption {
return func(c *registerConfig) {
c.requireParts = append(c.requireParts, name)
}
}