forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
82 lines (70 loc) · 1.36 KB
/
import.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
package gen
import (
"regexp"
"strings"
)
var (
importList = new(importPkgS).Add(
"context",
"database/sql",
"strings",
"",
"gorm.io/gorm",
"gorm.io/gorm/schema",
"gorm.io/gorm/clause",
"",
"gen \"github.com/dieagenturverwaltung/gorm-gen\"",
"github.com/dieagenturverwaltung/gorm-gen/field",
"github.com/dieagenturverwaltung/gorm-gen/helper",
"",
"gorm.io/plugin/dbresolver",
)
unitTestImportList = new(importPkgS).Add(
"context",
"fmt",
"strconv",
"testing",
"",
"gorm.io/driver/sqlite",
"gorm.io/gorm",
)
)
type importPkgS struct {
paths []string
}
func (ip importPkgS) Add(paths ...string) *importPkgS {
purePaths := make([]string, 0, len(paths)+1)
for _, p := range paths {
p = strings.TrimSpace(p)
if p == "" {
purePaths = append(purePaths, p)
continue
}
if p[len(p)-1] != '"' {
p = `"` + p + `"`
}
reg := regexp.MustCompile(`^.*"(.*)"$`)
var exists bool
for _, existsP := range ip.paths {
submatch := reg.FindStringSubmatch(existsP)
if len(submatch) < 2 {
continue
}
if submatch[1] == p {
exists = true
break
}
if p == existsP {
exists = true
break
}
}
if !exists {
purePaths = append(purePaths, p)
}
}
purePaths = append(purePaths, "")
ip.paths = append(ip.paths, purePaths...)
return &ip
}
func (ip importPkgS) Paths() []string { return ip.paths }