-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
88 lines (82 loc) · 1.99 KB
/
functions.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
package main
import (
"bytes"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
)
func getFuncMap(schemaParam []string) template.FuncMap {
return template.FuncMap{
"add": func(a, b int) int {
return a + b
},
"upper": func(s string) string {
return strings.ToUpper(s)
},
"oneLineSQL": func(s string) string {
// Remove inline comments (e.g., "-- comment")
re := regexp.MustCompile(`--.*`)
withoutComments := re.ReplaceAllString(s, "")
// Replace newlines and extra spaces with a single space
cleaned := strings.ReplaceAll(withoutComments, "\n", " ")
cleaned = strings.Join(strings.Fields(cleaned), " ") // Removes extra spaces
return cleaned
},
"schema": func() string {
schema := ""
for _, schemaPath := range schemaParam {
fileInfo, err := os.Stat(schemaPath)
if err != nil {
log.Fatalf("Error accessing schema path: %v", err)
}
if !fileInfo.IsDir() {
content, err := readFile(schemaPath)
if err != nil {
log.Fatalf("Error reading file %s: %v", schemaPath, err)
}
schema += content + "\n"
continue
}
files, err := os.ReadDir(schemaPath)
if err != nil {
log.Fatalf("Error reading directory: %v", err)
}
for _, file := range files {
if file.IsDir() { // Skip subdirectories
continue
}
filePath := filepath.Join(schemaPath, file.Name())
content, err := readFile(filePath)
if err != nil {
log.Fatalf("Error reading file %s: %v", filePath, err)
}
schema += content + "\n"
}
}
return schema
},
"regexMatch": func(pattern, s string) bool {
match, err := regexp.MatchString(pattern, s)
if err != nil {
log.Fatalf("Error matching regex: %v", err)
}
return match
},
}
}
func readFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(file)
if err != nil {
return "", err
}
return buf.String(), nil
}