Skip to content

fix: backtick processing #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions golang/examples/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package examples
import (
"database/sql"
"fmt"

_ "github.com/go-sql-driver/mysql"
psh "github.com/platformsh/config-reader-go/v2"
sqldsn "github.com/platformsh/config-reader-go/v2/sqldsn"
Expand Down Expand Up @@ -31,35 +32,33 @@ func UsageExampleMySQL() string {

// Force MySQL into modern mode.
db.Exec("SET NAMES=utf8")
db.Exec(`SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,
NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER,ONLY_FULL_GROUP_BY'`)
db.Exec("SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES," +
"NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO," +
"NO_AUTO_CREATE_USER,ONLY_FULL_GROUP_BY'")

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

_, err = db.Exec(sqlCreate)
checkErr(err)

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

_, err = db.Exec(sqlInsert)
checkErr(err)

table := `<table>
<thead>
<tr><th>Name</th><th>City</th></tr>
</thead>
<tbody>`
table := "<table>" +
"<thead>" +
"<tr><th>Name</th><th>City</th></tr>" +
"</thead>" +
"<tbody>"

var id int
var name string
Expand Down
29 changes: 14 additions & 15 deletions golang/examples/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package examples
import (
"database/sql"
"fmt"

_ "github.com/lib/pq"
psh "github.com/platformsh/config-reader-go/v2"
libpq "github.com/platformsh/config-reader-go/v2/libpq"
Expand Down Expand Up @@ -31,30 +32,28 @@ func UsageExamplePostgreSQL() string {
defer db.Close()

// Creating a table.
sqlCreate := `
CREATE TABLE IF NOT EXISTS PeopleGo (
id SERIAL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL);`
sqlCreate := "CREATE TABLE IF NOT EXISTS PeopleGo (" +
"id SERIAL PRIMARY KEY," +
"name VARCHAR(30) NOT NULL," +
"city VARCHAR(30) NOT NULL);"

_, err = db.Exec(sqlCreate)
checkErr(err)

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

_, err = db.Exec(sqlInsert)
checkErr(err)

table := `<table>
<thead>
<tr><th>Name</th><th>City</th></tr>
</thead>
<tbody>`
table := "<table>" +
"<thead>" +
"<tr><th>Name</th><th>City</th></tr>" +
"</thead>" +
"<tbody>"

var id int
var name string
Expand Down
9 changes: 5 additions & 4 deletions golang/examples/solr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples

import (
"fmt"

psh "github.com/platformsh/config-reader-go/v2"
gosolr "github.com/platformsh/config-reader-go/v2/gosolr"
solr "github.com/rtt/Go-Solr"
Expand Down Expand Up @@ -55,10 +56,10 @@ func UsageExampleSolr() string {
resDel, err := connection.Update(docDelete, true)
checkErr(err)

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

return message
}
16 changes: 10 additions & 6 deletions golang/generators/include-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
Expand All @@ -12,16 +13,19 @@ import (

const (
sourceDirectory = "examples"
backTick = "\\x60"
outputFile = "sources.go"
)

type debacktickWriter struct {
type noBacktickWriter struct {
writer io.Writer
}

func (dw *debacktickWriter) Write(buf []byte) (int, error) {
_, err := dw.writer.Write(bytes.ReplaceAll(buf, []byte("`"), []byte(backTick)))
func (dw *noBacktickWriter) Write(buf []byte) (int, error) {
if bytes.ContainsRune(buf, '`') {
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")
}

_, err := dw.writer.Write(buf)
return len(buf), err
}

Expand All @@ -30,7 +34,7 @@ func (dw *debacktickWriter) Write(buf []byte) (int, error) {
func main() {
fs, _ := ioutil.ReadDir(sourceDirectory)
out, _ := os.Create(outputFile)
outFilterBacktick := &debacktickWriter{writer: out}
outFilterBacktick := &noBacktickWriter{writer: out}
_, err := out.Write([]byte("package main \n\nconst (\n"))
if err != nil {
log.Fatal("Writing header failed: ", err)
Expand All @@ -47,7 +51,7 @@ func main() {
}
_, err = io.Copy(outFilterBacktick, inputFileStream)
if err != nil {
log.Fatal("Writing inputFileStream content failed: ", err)
log.Fatalf("Writing inputFileStream content failed for file %q: %v.", file.Name(), err)
}
_, err = out.Write([]byte("`\n"))
if err != nil {
Expand Down