Embeds rewrites `sqlc.embed(param)` to a `ast.ColumnRef` of form `param.*`. The compiler can make use of the returned `EmbedSet` while expanding the `param.*` column refs to produce the correct source edits.
(raw *ast.RawStmt)
| 36 | // The compiler can make use of the returned `EmbedSet` while expanding the |
| 37 | // `param.*` column refs to produce the correct source edits. |
| 38 | func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) { |
| 39 | var embeds []*Embed |
| 40 | |
| 41 | node := astutils.Apply(raw, func(cr *astutils.Cursor) bool { |
| 42 | node := cr.Node() |
| 43 | |
| 44 | switch { |
| 45 | case isEmbed(node): |
| 46 | fun := node.(*ast.FuncCall) |
| 47 | |
| 48 | if len(fun.Args.Items) == 0 { |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | param, _ := flatten(fun.Args) |
| 53 | |
| 54 | node := &ast.ColumnRef{ |
| 55 | Fields: &ast.List{ |
| 56 | Items: []ast.Node{ |
| 57 | &ast.String{Str: param}, |
| 58 | &ast.A_Star{}, |
| 59 | }, |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | embeds = append(embeds, &Embed{ |
| 64 | Table: &ast.TableName{Name: param}, |
| 65 | param: param, |
| 66 | Node: node, |
| 67 | }) |
| 68 | |
| 69 | cr.Replace(node) |
| 70 | return false |
| 71 | default: |
| 72 | return true |
| 73 | } |
| 74 | }, nil) |
| 75 | |
| 76 | return node.(*ast.RawStmt), embeds |
| 77 | } |
| 78 | |
| 79 | func isEmbed(node ast.Node) bool { |
| 80 | call, ok := node.(*ast.FuncCall) |