-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
listeners.go
81 lines (68 loc) · 2.49 KB
/
listeners.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
/*
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 listeners
import (
"strings"
"github.com/webx-top/db/lib/factory"
"github.com/admpub/nging/application/library/fileupdater/listener"
)
func New() *Listeners {
return &Listeners{}
}
type Listeners map[string]func(m factory.Model) (tableID string, content string, property *listener.Property)
func (a *Listeners) Listen(tableName string, embedded bool, seperatorAndSameFields ...string) *Listeners {
var sameFields []string
var seperator string
if len(seperatorAndSameFields) > 0 {
seperator = seperatorAndSameFields[0]
}
if len(seperatorAndSameFields) > 1 {
sameFields = seperatorAndSameFields[1:]
}
for _field, _listener := range *a {
listener.New(_listener, embedded, seperator).SetTable(tableName, _field, sameFields...).ListenDefault()
delete(*a, _field)
}
return a
}
func (a *Listeners) ListenByField(fieldNames string, tableName string, embedded bool, seperatorAndSameFields ...string) *Listeners {
var sameFields []string
var seperator string
if len(seperatorAndSameFields) > 0 {
seperator = seperatorAndSameFields[0]
}
if len(seperatorAndSameFields) > 1 {
sameFields = seperatorAndSameFields[1:]
}
for _, fieldName := range strings.Split(fieldNames, `,`) {
if _listener, ok := (*a)[fieldName]; ok {
listener.New(_listener, embedded, seperator).SetTable(tableName, fieldName, sameFields...).ListenDefault()
delete(*a, fieldName)
}
}
return a
}
func (a *Listeners) Add(fieldName string, callback func(m factory.Model) (tableID string, content string, property *listener.Property)) *Listeners {
(*a)[fieldName] = callback
return a
}
func (a *Listeners) Delete(fieldNames ...string) *Listeners {
for _, fieldName := range fieldNames {
if _, ok := (*a)[fieldName]; ok {
delete(*a, fieldName)
}
}
return a
}