/
source_file.go
95 lines (77 loc) · 1.64 KB
/
source_file.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
92
93
94
95
package agents
import (
"errors"
"github.com/TeaWeb/code/teaconfigs/forms"
"github.com/iwind/TeaGo/files"
)
// 数据文件
type FileSource struct {
Source `yaml:",inline"`
Path string `yaml:"path" json:"path"`
}
// 获取新对象
func NewFileSource() *FileSource {
return &FileSource{}
}
// 校验
func (this *FileSource) Validate() error {
if len(this.Path) == 0 {
return errors.New("path should not be empty")
}
return nil
}
// 名称
func (this *FileSource) Name() string {
return "数据文件"
}
// 代号
func (this *FileSource) Code() string {
return "file"
}
// 描述
func (this *FileSource) Description() string {
return "通过读取本地文件获取数据"
}
// 执行
func (this *FileSource) Execute(params map[string]string) (value interface{}, err error) {
if len(this.Path) == 0 {
return nil, errors.New("path should not be empty")
}
file := files.NewFile(this.Path)
if !file.Exists() {
return nil, errors.New("file does not exist")
}
data, err := file.ReadAll()
if err != nil {
return nil, err
}
return DecodeSource(data, this.DataFormat)
}
// 选项表单
func (this *FileSource) Form() *forms.Form {
form := forms.NewForm(this.Code())
group := form.NewGroup()
{
field := forms.NewTextField("数据文件路径", "")
field.IsRequired = true
field.Code = "path"
field.ValidateCode = `
if (value.length == 0) {
throw new Error("请输入数据文件路径");
}
return value;
`
group.Add(field)
}
return form
}
// 显示信息
func (this *FileSource) Presentation() *forms.Presentation {
return &forms.Presentation{
HTML: `
<tr>
<td>数据文件路径</td>
<td>{{source.path}}</td>
</tr>`,
}
}