forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper_loader.go
89 lines (74 loc) · 1.83 KB
/
mapper_loader.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
package gobatis
import (
"log"
"os"
"path/filepath"
"strings"
)
func lookupMapper(paths ...string) []string {
var fs []string
for _, p := range paths {
_ = filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if nil != info && strings.HasSuffix(info.Name(), ".xml") {
p, err = filepath.Abs(path)
if nil != err {
return err
}
fs = append(fs, p)
return nil
}
return nil
})
}
return fs
}
func loadingMapper(paths ...string) (*mapper,error) {
fs := lookupMapper(paths...)
if len(fs) == 0 {
return nil,ErrorEmptyMapper
}
mp := newMapper()
for _, f := range fs {
r, e := os.Open(f)
if nil != e {
continue
}
rootNode := parse(r)
if rootNode.Name != "mapper" {
log.Fatalln("mapper xml must start with `mapper` tag, please check your xml mapper!")
}
namespace := ""
if val, ok := rootNode.Attrs["namespace"]; ok {
nStr := strings.TrimSpace(val.Value)
if nStr != "" {
nStr += "."
}
namespace = nStr
}
for _, elem := range rootNode.Elements {
if elem.ElementType == eleTpNode {
childNode := elem.Val.(node)
switch childNode.Name {
case "select", "update", "insert", "delete":
if childNode.Id == "" {
log.Fatalln("No id for:", childNode.Name, "Id must be not null, please check your xml mapper!")
}
fid := namespace + childNode.Id
if ok := mp.put(fid, &childNode); !ok {
log.Fatalln("repeat id for:", fid, "Please check your xml mapper!")
}
case "sql":
if childNode.Id == "" {
log.Fatalln("no id for:", childNode.Name, "Id must be not null, please check your xml mapper!")
}
fid := namespace + childNode.Id
if ok := mp.put(fid, &childNode); !ok {
log.Fatalln("repeat id for:", fid, "Please check your xml mapper!")
}
}
}
}
_ = r.Close()
}
return mp,nil
}