-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_loader.go
71 lines (57 loc) · 1.39 KB
/
template_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
package core
const (
RepositoryTemplateType = "repository"
BranchTemplateType = "branch"
BranchProtectionTemplateType = "branch protection"
TemplateMaxDepth = 10
TemplateMaxCount = 10
)
func LoadTemplate[T any](
tplName string,
loaderFn func(s string) *T,
finderFn func(c *T) *[]string,
tplType string,
path ...string,
) ([]*T, error) {
if len(path) > TemplateMaxDepth {
return nil, MaxTemplateDepthReachedError(tplType, path)
}
var (
err error
tplList []*T
tpl *T
)
if tpl = loaderFn(tplName); tpl == nil {
return nil, UnknownTemplateError(tplType, tplName)
}
if tplList, err = LoadTemplateList(finderFn(tpl), loaderFn, finderFn, tplType, append(path, tplName)...); err != nil {
return nil, err
}
tplList = append(tplList, tpl)
return tplList, nil
}
func LoadTemplateList[T any](
tplNameList *[]string,
loaderFn func(s string) *T,
finderFn func(c *T) *[]string,
tplType string,
path ...string,
) ([]*T, error) {
var (
tplList []*T
err error
)
if tplNameList != nil {
if len(*tplNameList) > TemplateMaxCount {
return nil, MaxTemplateCountReachedError(tplType, path)
}
for _, tplName := range *tplNameList {
var subTplList []*T
if subTplList, err = LoadTemplate[T](tplName, loaderFn, finderFn, tplType, path...); err != nil {
return nil, err
}
tplList = append(tplList, subTplList...)
}
}
return tplList, nil
}