forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
64 lines (56 loc) · 1.21 KB
/
table.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
package table
import (
"fmt"
"io"
"os"
"strings"
"github.com/yaoapp/gou/helper"
"github.com/yaoapp/kun/exception"
)
// Tables 已载入模型
var Tables = map[string]*Table{}
// Load 载入数据表格
func Load(source string, name string) *Table {
var input io.Reader = nil
if strings.HasPrefix(source, "file://") || strings.HasPrefix(source, "fs://") {
filename := strings.TrimPrefix(source, "file://")
filename = strings.TrimPrefix(filename, "fs://")
file, err := os.Open(filename)
if err != nil {
exception.Err(err, 400).Throw()
}
defer file.Close()
input = file
} else {
input = strings.NewReader(source)
}
table := Table{
Source: source,
Table: name,
}
err := helper.UnmarshalFile(input, &table)
if err != nil {
exception.Err(err, 400).Throw()
}
table.loadColumns()
table.loadFilters()
table.loadAPIs()
Tables[name] = &table
return Tables[name]
}
// Select 读取已加载表格配置
func Select(name string) *Table {
tab, has := Tables[name]
if !has {
exception.New(
fmt.Sprintf("Table:%s; 尚未加载", name),
400,
).Throw()
}
return tab
}
// Reload 更新数据表格配置
func (tab *Table) Reload() *Table {
tab = Load(tab.Source, tab.Name)
return tab
}