-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
79 lines (71 loc) · 2 KB
/
util.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
package pgGenerate
import (
"os"
"fmt"
"log"
"strings"
"github.com/pkg/errors"
)
func checkErr(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func checkSourceDirExist() {
checkDirExist(config.ModelDir, "Create directory for 'model' files.")
checkDirExist(config.TemplateDir, "Create directory for 'template' files.")
}
func checkDirExist(pathArr []string, msg string) {
for _, path := range pathArr {
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatal(fmt.Sprintf("Directory %s not exist. %s\n", path, msg))
}
}
}
func castArrInterfaceToArrString(arr []interface{}) []string {
newArr := make([]string, len(arr))
for i, v := range arr {
newArr[i] = v.(string)
}
return newArr
}
func joinWithQuotes(arr []string, sep string, quote string) (s string) {
for _, item := range arr {
s += fmt.Sprintf("%s%s%s, ", quote, item, quote)
}
s = strings.TrimSuffix(s, ", ")
return
}
func dict(values ...interface{}) (map[string]interface{}, error) {
if len(values) % 2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values) / 2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i + 1]
}
return dict, nil
}
// функция используется в шаблоне для вставки блоков для проверки прав
func rightsTmpl(docType string, method string) (tmpl string, err error) {
if _, ok := docModels.Docs[docType]; !ok {
return "", errors.New(fmt.Sprintf("rightsTmpl: not found doc for docType: '%s'.", docType))
}
doc := docModels.Docs[docType]
for _, r := range doc.TmplMain.Rights {
if r.Method == method {
str := fmt.Sprintf(`
IF (params ->> 'userRole') != ALL('{%s}'::text[])
THEN
RETURN json_build_object('code', 'error', 'message', 'insufficient rights');
END IF;
`, joinWithQuotes(r.Allow, ",", "\""))
return str, nil
}
}
return "", nil
}