-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.go
123 lines (109 loc) · 3.34 KB
/
mapping.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package export
import (
"errors"
"strings"
"github.com/webx-top/com"
)
var ErrSameTableName = errors.New(`same table name`)
type Mapping struct {
FromParent int //如果大于0,则代表从上一层或上几层页面中取值
FromTable string //如果设置,则会从FromTable指定的“表的上一个插入操作”之后的结果集字段中取值
FromField string //来自结果集字段
FromPipe string //管道函数
FromInput string //输入框数据的原始内容
ToTable string //保存到表
ToField string //保存到字段
ToInput string //输入框数据的原始内容
}
func NewMapping(fromInput, toInput string) (*Mapping, error) {
mapping := &Mapping{
FromInput: fromInput,
ToInput: toInput,
}
if arr := strings.SplitN(mapping.ToInput, `.`, 2); len(arr) > 1 {
mapping.ToTable = arr[0]
mapping.ToField = arr[1]
} else {
mapping.ToField = mapping.ToInput
}
inputs := strings.SplitN(mapping.FromInput, `|`, 2)
fromInputField := inputs[0]
if len(inputs) > 1 {
mapping.FromPipe = inputs[1]
}
if strings.HasPrefix(fromInputField, `@`) {
mapping.FromField = strings.TrimPrefix(fromInputField, `@`)
if arr := strings.SplitN(mapping.ToInput, `.`, 2); len(arr) > 1 {
mapping.FromTable = arr[0]
if mapping.FromTable == mapping.ToTable {
return nil, ErrSameTableName
}
mapping.FromField = arr[1]
}
} else {
mapping.FromField = strings.TrimLeft(fromInputField, `./`)
for strings.HasPrefix(mapping.FromField, `../`) {
mapping.FromParent++
mapping.FromField = strings.TrimPrefix(mapping.FromField, `../`)
mapping.FromField = strings.TrimLeft(mapping.FromField, `./`)
}
}
return mapping, nil
}
type Mappings struct {
TableKeys map[string][]int
Slice []*Mapping
TableNames []string
}
func NewMappings() *Mappings {
return &Mappings{
TableKeys: map[string][]int{},
Slice: []*Mapping{},
TableNames: []string{},
}
}
func (m *Mappings) addTableName(tableName string) {
var exists bool
for _, table := range m.TableNames {
if table == tableName {
exists = true
break
}
}
if !exists {
m.TableNames = append(m.TableNames, tableName)
}
}
func (m *Mappings) Add(mp *Mapping) *Mappings {
if len(mp.FromTable) > 0 {
m.addTableName(mp.FromTable)
}
if _, ok := m.TableKeys[mp.ToTable]; !ok {
m.TableKeys[mp.ToTable] = []int{}
m.addTableName(mp.ToTable)
}
m.TableKeys[mp.ToTable] = append(m.TableKeys[mp.ToTable], len(m.Slice))
m.Slice = append(m.Slice, mp)
return m
}
func (m *Mappings) String() (string, error) {
b, err := com.JSONEncode(m)
if err == nil {
return string(b), nil
}
return ``, err
}