Skip to content

Commit

Permalink
feat: allow spaces between the embed keyword and the struct name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Huang0403 authored and kyleconroy committed Nov 25, 2024
1 parent 46ffd02 commit 91af153
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
65 changes: 65 additions & 0 deletions examples/batch/postgresql/batch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/batch/postgresql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions internal/sql/rewrite/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package rewrite

import (
"fmt"
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
)

// Embed is an instance of `sqlc.embed(param)`
type Embed struct {
Table *ast.TableName
param string
Node *ast.ColumnRef
Table *ast.TableName
param string
origin string
Node *ast.ColumnRef
}

// Orig string to replace
func (e Embed) Orig() string {
return fmt.Sprintf("sqlc.embed(%s)", e.param)
return e.origin
}

// EmbedSet is a set of Embed instances
Expand Down Expand Up @@ -60,10 +62,16 @@ func Embeds(raw *ast.RawStmt) (*ast.RawStmt, EmbedSet) {
},
}

// Get the origin string
const noSpaceLen = 11
spaceCount := fun.Args.Items[0].Pos() - fun.Pos() - noSpaceLen
origin := fmt.Sprintf("sqlc.embed%s(%s)", strings.Repeat(" ", spaceCount), param)

embeds = append(embeds, &Embed{
Table: &ast.TableName{Name: param},
param: param,
Node: node,
Table: &ast.TableName{Name: param},
param: param,
origin: origin,
Node: node,
})

cr.Replace(node)
Expand Down

0 comments on commit 91af153

Please sign in to comment.