-
-
Notifications
You must be signed in to change notification settings - Fork 241
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
Fix Anko converter #218
Changes from 1 commit
a7122be
bee4e40
3f792af
6f1e667
a23a4e3
1eb0bb5
51e3680
6bc2406
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package fix | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gobuffalo/plush" | ||
|
@@ -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) | ||
ll := i | ||
lines[i] = line | ||
for { | ||
if strings.HasPrefix(tl, "})") { | ||
line = "}" | ||
break | ||
} else if strings.HasPrefix(tl, "}") { | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}) | ||
} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. :)