-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin.go
91 lines (78 loc) · 2.15 KB
/
origin.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package psqlfront
import (
"context"
"errors"
"fmt"
"time"
)
type CacheWriter interface {
DeleteRows(ctx context.Context) error
ReplaceCacheTable(ctx context.Context, t *Table) error
AppendRows(context.Context, [][]interface{}) error
TargetTable() *Table
}
type Origin interface {
ID() string
GetTables(ctx context.Context) ([]*Table, error)
RefreshCache(context.Context, CacheWriter) error
}
type OriginConfig interface {
Type() string
Restrict() error
NewOrigin(id string) (Origin, error)
}
var originConfigConstructors = make(map[string]func() OriginConfig)
func RegisterOriginType(typeName string, originConfigConstructor func() OriginConfig) {
originConfigConstructors[typeName] = originConfigConstructor
}
func UnregisterOriginType(typeName string) {
delete(originConfigConstructors, typeName)
}
func GetOriginConfig(typeName string) (OriginConfig, bool) {
originConfigConstructor, ok := originConfigConstructors[typeName]
if !ok {
return nil, false
}
if originConfigConstructor == nil {
return nil, true
}
return originConfigConstructor(), true
}
type CommonOriginConfig struct {
ID string `yaml:"id"`
Type string `yaml:"type"`
TTL *time.Duration `yaml:"ttl"`
OriginConfig OriginConfig `yaml:"-"`
}
func (cfg *CommonOriginConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type alias CommonOriginConfig
var aux alias
if err := unmarshal(&aux); err != nil {
return fmt.Errorf("aux decode: %w", err)
}
*cfg = CommonOriginConfig(aux)
originCfg, ok := GetOriginConfig(cfg.Type)
if !ok {
return fmt.Errorf("type `%s` not registerd", cfg.Type)
}
if originCfg == nil {
return fmt.Errorf("type `%s` is invalid", cfg.Type)
}
cfg.OriginConfig = originCfg
return unmarshal(originCfg)
}
func (cfg *CommonOriginConfig) Ristrict() error {
if cfg.ID == "" {
return errors.New("origin_id is required")
}
if cfg.OriginConfig == nil {
return errors.New("origin config is nil")
}
if cfg.Type != cfg.OriginConfig.Type() {
return errors.New("origin type missmatch")
}
return cfg.OriginConfig.Restrict()
}
func (cfg *CommonOriginConfig) NewOrigin() (Origin, error) {
return cfg.OriginConfig.NewOrigin(cfg.ID)
}