@@ -9,16 +9,55 @@ import (
9
9
)
10
10
11
11
func EncodeEscapeChars (s string ) string {
12
- return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `\t` , `{{\\}} t` ), `\s` , `{{\\}} s` ), `\r` , `{{\\}} r` ), `\n` , `{{\\}} n` )
12
+ return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `\t` , `【\\】 t` ), `\s` , `【\\】 s` ), `\r` , `【\\】 r` ), `\n` , `【\\】 n` )
13
13
}
14
14
15
15
func DecodeEscapeChars (s string ) string {
16
- return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `{{\}}t` , `\t` ), `{{\}}s` , `\s` ), `{{\}}r` , `\r` ), `{{\}}n` , `\n` )
16
+ return strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (strings .ReplaceAll (s , `【\】t` , `\t` ), `【\】s` , `\s` ), `【\】r` , `\r` ), `【\】n` , `\n` )
17
+ }
18
+
19
+ // scenes:
20
+ //
21
+ // return 200 "1" ;
22
+ // return 200 "1";
23
+ // return "1" ;
24
+ // return 200;
25
+ // return "aaa\naa";
26
+ // return 200 "a\n" ;
27
+ // return BACKEND\n;
28
+ func FixReturn (s string ) string {
29
+ var scene1 = regexp .MustCompile (`return\s+(\d+)\s(\S+)\s*;` )
30
+ var scene2 = regexp .MustCompile (`return\s+(\d+)\s"(\S+)"\s*;` )
31
+ var scene3 = regexp .MustCompile (`return\s+(\S+)\s*;` )
32
+ var scene4 = regexp .MustCompile (`return\s+"(\S+)"\s*;` )
33
+ var scene5 = regexp .MustCompile (`return\s+(\d+)\s*;` )
34
+
35
+ if scene1 .MatchString (s ) {
36
+ if scene2 .MatchString (s ) { // eg: `return 200 "ok";`
37
+ s = scene2 .ReplaceAllString (s , "return $1 \" $2\" ;" )
38
+ } else { // eg: `return 200 $content;`
39
+ s = scene1 .ReplaceAllString (s , "return $1 \" $2\" ;" )
40
+ }
41
+ } else if scene3 .MatchString (s ) {
42
+ if scene5 .MatchString (s ) { // eg: `return 200;`
43
+ s = scene5 .ReplaceAllString (s , "return $1;" )
44
+ } else if scene4 .MatchString (s ) { // eg: `return "ok";`
45
+ s = scene4 .ReplaceAllString (s , "return \" $1\" ;" )
46
+ } else { // eg: `return BACKEND\n;`
47
+ found := scene3 .FindString (s )
48
+ if ! (strings .HasPrefix (found , `"` ) && strings .HasSuffix (found , `"` )) {
49
+ s = scene3 .ReplaceAllString (s , "return $1;" )
50
+ } else {
51
+ s = scene3 .ReplaceAllString (s , "return \" $1\" ;" )
52
+ }
53
+ }
54
+ }
55
+ return s
17
56
}
18
57
19
58
func FixVars (s string ) string {
20
59
s = regexp .MustCompile (`(\$)(\{\S+?\})` ).ReplaceAllString (s , "[dollar]$2" )
21
- return regexp . MustCompile ( `(return\s+\d+\s+?)([\s\S]+?);` ). ReplaceAllString ( s , "$1 \" $2 \" ;" )
60
+ return s
22
61
}
23
62
24
63
func UpdateConfInDir (rootDir string , fn func (s string ) (string , error )) error {
@@ -37,7 +76,7 @@ func UpdateConfInDir(rootDir string, fn func(s string) (string, error)) error {
37
76
return err
38
77
}
39
78
40
- modifiedData , err := fn (FixVars (EncodeEscapeChars (string (data ))))
79
+ modifiedData , err := fn (FixVars (FixReturn ( EncodeEscapeChars (string (data ) ))))
41
80
if err != nil {
42
81
return err
43
82
}
0 commit comments