Skip to content

Commit 0e4dea7

Browse files
authored
Merge pull request #83 from daeMOn63/fix_backtick_processing
fix: backtick processing
2 parents 52afcc9 + 621e4fa commit 0e4dea7

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

golang/examples/mysql.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package examples
33
import (
44
"database/sql"
55
"fmt"
6+
67
_ "github.com/go-sql-driver/mysql"
78
psh "github.com/platformsh/config-reader-go/v2"
89
sqldsn "github.com/platformsh/config-reader-go/v2/sqldsn"
@@ -31,35 +32,33 @@ func UsageExampleMySQL() string {
3132

3233
// Force MySQL into modern mode.
3334
db.Exec("SET NAMES=utf8")
34-
db.Exec(`SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,
35-
NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
36-
NO_AUTO_CREATE_USER,ONLY_FULL_GROUP_BY'`)
35+
db.Exec("SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES," +
36+
"NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO," +
37+
"NO_AUTO_CREATE_USER,ONLY_FULL_GROUP_BY'")
3738

3839
// Creating a table.
39-
sqlCreate := `
40-
CREATE TABLE IF NOT EXISTS People (
41-
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
42-
name VARCHAR(30) NOT NULL,
43-
city VARCHAR(30) NOT NULL)`
40+
sqlCreate := "CREATE TABLE IF NOT EXISTS People (" +
41+
"id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
42+
"name VARCHAR(30) NOT NULL," +
43+
"city VARCHAR(30) NOT NULL)"
4444

4545
_, err = db.Exec(sqlCreate)
4646
checkErr(err)
4747

4848
// Insert data.
49-
sqlInsert := `
50-
INSERT INTO People (name, city) VALUES
51-
('Neil Armstrong', 'Moon'),
52-
('Buzz Aldrin', 'Glen Ridge'),
53-
('Sally Ride', 'La Jolla');`
49+
sqlInsert := "INSERT INTO People (name, city) VALUES" +
50+
"('Neil Armstrong', 'Moon')," +
51+
"('Buzz Aldrin', 'Glen Ridge')," +
52+
"('Sally Ride', 'La Jolla');"
5453

5554
_, err = db.Exec(sqlInsert)
5655
checkErr(err)
5756

58-
table := `<table>
59-
<thead>
60-
<tr><th>Name</th><th>City</th></tr>
61-
</thead>
62-
<tbody>`
57+
table := "<table>" +
58+
"<thead>" +
59+
"<tr><th>Name</th><th>City</th></tr>" +
60+
"</thead>" +
61+
"<tbody>"
6362

6463
var id int
6564
var name string

golang/examples/postgresql.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package examples
33
import (
44
"database/sql"
55
"fmt"
6+
67
_ "github.com/lib/pq"
78
psh "github.com/platformsh/config-reader-go/v2"
89
libpq "github.com/platformsh/config-reader-go/v2/libpq"
@@ -31,30 +32,28 @@ func UsageExamplePostgreSQL() string {
3132
defer db.Close()
3233

3334
// Creating a table.
34-
sqlCreate := `
35-
CREATE TABLE IF NOT EXISTS PeopleGo (
36-
id SERIAL PRIMARY KEY,
37-
name VARCHAR(30) NOT NULL,
38-
city VARCHAR(30) NOT NULL);`
35+
sqlCreate := "CREATE TABLE IF NOT EXISTS PeopleGo (" +
36+
"id SERIAL PRIMARY KEY," +
37+
"name VARCHAR(30) NOT NULL," +
38+
"city VARCHAR(30) NOT NULL);"
3939

4040
_, err = db.Exec(sqlCreate)
4141
checkErr(err)
4242

4343
// Insert data.
44-
sqlInsert := `
45-
INSERT INTO PeopleGo(name, city) VALUES
46-
('Neil Armstrong', 'Moon'),
47-
('Buzz Aldrin', 'Glen Ridge'),
48-
('Sally Ride', 'La Jolla');`
44+
sqlInsert := "INSERT INTO PeopleGo(name, city) VALUES" +
45+
"('Neil Armstrong', 'Moon')," +
46+
"('Buzz Aldrin', 'Glen Ridge')," +
47+
"('Sally Ride', 'La Jolla');"
4948

5049
_, err = db.Exec(sqlInsert)
5150
checkErr(err)
5251

53-
table := `<table>
54-
<thead>
55-
<tr><th>Name</th><th>City</th></tr>
56-
</thead>
57-
<tbody>`
52+
table := "<table>" +
53+
"<thead>" +
54+
"<tr><th>Name</th><th>City</th></tr>" +
55+
"</thead>" +
56+
"<tbody>"
5857

5958
var id int
6059
var name string

golang/examples/solr.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package examples
22

33
import (
44
"fmt"
5+
56
psh "github.com/platformsh/config-reader-go/v2"
67
gosolr "github.com/platformsh/config-reader-go/v2/gosolr"
78
solr "github.com/rtt/Go-Solr"
@@ -55,10 +56,10 @@ func UsageExampleSolr() string {
5556
resDel, err := connection.Update(docDelete, true)
5657
checkErr(err)
5758

58-
message := fmt.Sprintf(`Adding one document - %s<br>
59-
Selecting document (1 expected): %d<br>
60-
Deleting document - %s<br>
61-
`, respAdd, resSelect.Results.NumFound, resDel)
59+
message := fmt.Sprintf("Adding one document - %s<br>"+
60+
"Selecting document (1 expected): %d<br>"+
61+
"Deleting document - %s<br>",
62+
respAdd, resSelect.Results.NumFound, resDel)
6263

6364
return message
6465
}

golang/generators/include-source.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"errors"
56
"io"
67
"io/ioutil"
78
"log"
@@ -12,16 +13,19 @@ import (
1213

1314
const (
1415
sourceDirectory = "examples"
15-
backTick = "\\x60"
1616
outputFile = "sources.go"
1717
)
1818

19-
type debacktickWriter struct {
19+
type noBacktickWriter struct {
2020
writer io.Writer
2121
}
2222

23-
func (dw *debacktickWriter) Write(buf []byte) (int, error) {
24-
_, err := dw.writer.Write(bytes.ReplaceAll(buf, []byte("`"), []byte(backTick)))
23+
func (dw *noBacktickWriter) Write(buf []byte) (int, error) {
24+
if bytes.ContainsRune(buf, '`') {
25+
return 0, errors.New("source must not contains backtick, as golang does not allow to escape nested backticks in multiline strings. Use newlines and string concatenation instead")
26+
}
27+
28+
_, err := dw.writer.Write(buf)
2529
return len(buf), err
2630
}
2731

@@ -30,7 +34,7 @@ func (dw *debacktickWriter) Write(buf []byte) (int, error) {
3034
func main() {
3135
fs, _ := ioutil.ReadDir(sourceDirectory)
3236
out, _ := os.Create(outputFile)
33-
outFilterBacktick := &debacktickWriter{writer: out}
37+
outFilterBacktick := &noBacktickWriter{writer: out}
3438
_, err := out.Write([]byte("package main \n\nconst (\n"))
3539
if err != nil {
3640
log.Fatal("Writing header failed: ", err)
@@ -47,7 +51,7 @@ func main() {
4751
}
4852
_, err = io.Copy(outFilterBacktick, inputFileStream)
4953
if err != nil {
50-
log.Fatal("Writing inputFileStream content failed: ", err)
54+
log.Fatalf("Writing inputFileStream content failed for file %q: %v.", file.Name(), err)
5155
}
5256
_, err = out.Write([]byte("`\n"))
5357
if err != nil {

0 commit comments

Comments
 (0)