Skip to content
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

Fix Anko converter #218

Merged
merged 8 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 26 additions & 20 deletions fix/anko.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fix

import (
"bytes"
"fmt"
"strings"

"github.com/gobuffalo/plush"
Expand All @@ -12,30 +13,35 @@ func Anko(content string) (string, error) {
bb := &bytes.Buffer{}

lines := strings.Split(content, "\n")
l := len(lines)

// fix create_table
inCreateTable := false
for i := 0; i < len(lines); i++ {
for i := 0; i < l; i++ {
line := lines[i]
tl := strings.TrimSpace(line)
if strings.HasPrefix(tl, "create_table") {
line = strings.Replace(line, ", func(t) {", ") {", -1)
inCreateTable = true
}
if strings.HasPrefix(tl, "}") && inCreateTable {
inCreateTable = false
}
if strings.HasPrefix(tl, "})") && inCreateTable {
line = "}"
inCreateTable = false
}
lines[i] = line
}

// fix raw
for i, line := range lines {
tl := strings.TrimSpace(line)
if strings.HasPrefix(tl, "raw(") {
// skip already converted create_table
if strings.Contains(line, ", func(t) {") {
// fix create_table
line = strings.Replace(line, ", func(t) {", ") {", -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are re-doing this, it would be nice to use regex.ReplaceAllString() so that it'll not care about any whitespace. (We didn't have , func, but ,func and it wouldn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. :)

ll := i
lines[i] = line
for {
if strings.HasPrefix(tl, "})") {
line = "}"
break
} else if strings.HasPrefix(tl, "}") {
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to clear line before breaking and then we are good!

}
i++
line = lines[i]
tl = strings.TrimSpace(line)
if l == i {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are checking for overflow after trying to access an item past the length of the array on 38.

return "", fmt.Errorf("unclosed create_table statement line %d", ll+1)
}
}
}
} else if strings.HasPrefix(tl, "raw(") {
// fix raw
line = strings.Replace(line, "raw(", "sql(", -1)
}
lines[i] = line
Expand Down
7 changes: 6 additions & 1 deletion fix/fixtures/pass/0001_with_raw_backticks.anko.fizz
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ create_table("users", func(t) {
t.Column("joined_at", "timestamp", {})
})

create_table("domains") {
create_table("domains", func(t) {
t.Column("id","uuid",{"primary":true})
})

create_table("sodas", func(t) {
t.Column("id","uuid",{"primary":true})
}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm clearly just missing it since the tests are passing, but at some point you are going to have to explain to me how it's catching this straggler seeing as it looks to me as though it would have moved out of the create_table logic the line before.


raw(`
INSERT INTO users (email, twitter_handle, joined_at, created_at, updated_at)
Expand Down