-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
60 lines (51 loc) · 1.36 KB
/
datasource.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
package main
import (
"errors"
"github.com/Kong/go-pdk"
)
var (
ErrDatasourceNoID = errors.New("datasource's id can't be empty")
ErrDatasourceIDNotAllowed = errors.New("datasource's id is invalid because the keyword is not allowed")
)
type Datasource struct {
ID string `json:"id" yaml:"id"`
Type string `json:"type" yaml:"type"`
Static DatasourceStatic `json:"static" yaml:"static"`
Kong DatasourceKong `json:"kong" yaml:"kong"`
HTTP DatasourceHTTP `json:"http" yaml:"http"`
}
type configDatasource struct {
ID string `json:"id" yaml:"id"`
Type string `json:"type" yaml:"type"`
Static configDatasourceStatic `json:"static" yaml:"static"`
Kong configDatasourceKong `json:"kong" yaml:"kong"`
HTTP configDatasourceHTTP `json:"http" yaml:"http"`
}
func (d *Datasource) Init() (err error) {
if d.ID == "" {
return ErrDatasourceNoID
}
if d.ID == "__self__" {
return ErrDatasourceIDNotAllowed
}
switch d.Type {
case "static":
return d.Static.Init()
case "kong":
return d.Kong.Init()
case "http":
return d.HTTP.Init()
}
return
}
func (d *Datasource) ToData(kong *pdk.PDK, data Data) (m map[string]interface{}, err error) {
switch d.Type {
case "static":
return d.Static.ToData(kong, data)
case "kong":
return d.Kong.ToData(kong, data)
case "http":
return d.HTTP.ToData(kong, data)
}
return
}