-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
67 lines (51 loc) · 1.09 KB
/
Copy pathhelpers.go
File metadata and controls
67 lines (51 loc) · 1.09 KB
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
// path: codemode/helpers.go
package codemode
import (
"fmt"
"strings"
)
func preprocessUserCode(code string) string {
code = strings.TrimSpace(code)
if !strings.Contains(code, "__out") &&
!strings.Contains(code, "return") {
code += "\n__out = nil"
}
return code
}
func wrapIntoProgram(clean string) string {
return fmt.Sprintf(`package main
import (
codemode "codemode_helpers/codemode_helpers"
)
func run() any {
var __out any
// ----- BEGIN USER CODE -----
%s
// ----- END USER CODE -----
return __out
}
`, indent(clean, "\t"))
}
func indent(s, prefix string) string {
lines := strings.Split(s, "\n")
for i := range lines {
lines[i] = prefix + lines[i]
}
return strings.Join(lines, "\n")
}
type CacheStats struct {
Hits int
Misses int
}
func extractJSON(s string) string {
start := strings.Index(s, "{")
end := strings.LastIndex(s, "}")
if start != -1 && end != -1 && start <= end {
return s[start : end+1]
}
return ""
}
func isValidSnippet(code string) bool {
// Simple validation, actual compilation will happen in Yaegi
return strings.TrimSpace(code) != ""
}