-
Notifications
You must be signed in to change notification settings - Fork 26
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
feat: go type mapping per query argument #73
Changes from all commits
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 |
---|---|---|
|
@@ -203,6 +203,7 @@ func (p *parser) errorExpected(pos gotok.Pos, msg string) { | |
|
||
// Regexp to extract query annotations that control output. | ||
var annotationRegexp = regexp.MustCompile(`name: ([a-zA-Z0-9_$]+)[ \t]+(:many|:one|:exec)[ \t]*(.*)`) | ||
var annotationArgRegexp = regexp.MustCompile(`arg: ([a-zA-Z_][[a-zA-Z0-9_]*)[ \t]+(.*[a-zA-Z_][[a-zA-Z0-9_$]*)[ \t]*(.*)`) | ||
|
||
func (p *parser) parseQuery() ast.Query { | ||
if p.trace { | ||
|
@@ -246,7 +247,40 @@ func (p *parser) parseQuery() ast.Query { | |
p.error(pos, "no comment preceding query") | ||
return &ast.BadQuery{From: pos, To: p.pos} | ||
} | ||
last := doc.List[len(doc.List)-1] | ||
|
||
paramTypeOverrides := make(map[string]string, 4) | ||
annotationPos := len(doc.List) - 1 | ||
|
||
for ; annotationPos > 0; annotationPos-- { | ||
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. This is a bit messier than I'm comfortable with. Might be time to do a proper parse, something like:
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 was taking insipiration from how the Can you elaborate on what you mean by proper parser? Do you want a custom built DSL and parser or do you have something in mind to be reused? Which way would you like to go? 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. To give an example of what I mean with TOML, it would work like this: -- name: GetDailyFlightsFromAircraft :many
-- [arg]
-- day = "github.com/0xjac/custom-project/types.Day"
-- [return]
-- departure = "time.Time"
-- eta = "*time.Time"
SELECT f.flight_number, f.departure, f.arrival, f.eta
FROM flights f
WHERE pggen.arg("day") <= f.departure AND f.departure < pggen.arg("day") + 1
ORDER BY f.departure DESC; We could also add the |
||
argAnnotations := annotationArgRegexp.FindStringSubmatch(doc.List[annotationPos].Text) | ||
if argAnnotations == nil { | ||
break | ||
} | ||
|
||
argName, argGoType := argAnnotations[1], argAnnotations[2] | ||
|
||
if _, present := paramTypeOverrides[argName]; present { | ||
p.error(pos, "duplicate arg type override for "+argName+": "+argAnnotations[0]) | ||
return &ast.BadQuery{From: pos, To: p.pos} | ||
} | ||
|
||
unknownArg := true | ||
for _, arg := range names { | ||
if argName == arg.name { | ||
unknownArg = false | ||
break | ||
} | ||
} | ||
if unknownArg { | ||
p.error(pos, "arg type override for unknown arg "+argName+": "+argAnnotations[0]) | ||
return &ast.BadQuery{From: pos, To: p.pos} | ||
} | ||
|
||
paramTypeOverrides[argName] = argGoType | ||
|
||
} | ||
|
||
last := doc.List[annotationPos] | ||
annotations := annotationRegexp.FindStringSubmatch(last.Text) | ||
if annotations == nil { | ||
p.error(pos, "no 'name: <name> :<type>' token found in comment before query; comment line: \""+last.Text+`"`) | ||
|
@@ -263,15 +297,16 @@ func (p *parser) parseQuery() ast.Query { | |
preparedSQL, params := prepareSQL(templateSQL, names) | ||
|
||
return &ast.SourceQuery{ | ||
Name: annotations[1], | ||
Doc: doc, | ||
Start: pos, | ||
SourceSQL: templateSQL, | ||
PreparedSQL: preparedSQL, | ||
ParamNames: params, | ||
ResultKind: ast.ResultKind(annotations[2]), | ||
Pragmas: pragmas, | ||
Semi: semi, | ||
Name: annotations[1], | ||
Doc: doc, | ||
Start: pos, | ||
SourceSQL: templateSQL, | ||
PreparedSQL: preparedSQL, | ||
ParamNames: params, | ||
ParamTypeOverrides: paramTypeOverrides, | ||
ResultKind: ast.ResultKind(annotations[2]), | ||
Pragmas: pragmas, | ||
Semi: semi, | ||
} | ||
} | ||
|
||
|
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.
nit: I think this can be
ParamGoTypes
with a doc of something like: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.
Sounds good, and for the return value I would then name it accordingly: