@@ -4,20 +4,34 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"context"
7
- "io"
7
+ _ "embed"
8
+ "encoding/json"
8
9
"strings"
10
+ "text/template"
9
11
10
- easyjson "github.com/mailru/easyjson"
11
- plugin "github.com/tabbed/sqlc-go/codegen"
12
+ "buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
12
13
13
14
"github.com/tabbed/sqlc-gen-kotlin/internal/core"
14
- "github.com/tabbed/sqlc-gen-kotlin/internal/tmpl "
15
+ "github.com/tabbed/sqlc-go/sdk "
15
16
)
16
17
17
- func Generate (ctx context.Context , req * plugin.Request ) (* plugin.Response , error ) {
18
+ //go:embed tmpl/ktmodels.tmpl
19
+ var ktModelsTmpl string
20
+
21
+ //go:embed tmpl/ktsql.tmpl
22
+ var ktSqlTmpl string
23
+
24
+ //go:embed tmpl/ktiface.tmpl
25
+ var ktIfaceTmpl string
26
+
27
+ func Offset (v int ) int {
28
+ return v + 1
29
+ }
30
+
31
+ func Generate (ctx context.Context , req * plugin.CodeGenRequest ) (* plugin.CodeGenResponse , error ) {
18
32
var conf core.Config
19
33
if len (req .PluginOptions ) > 0 {
20
- if err := easyjson .Unmarshal (req .PluginOptions , & conf ); err != nil {
34
+ if err := json .Unmarshal (req .PluginOptions , & conf ); err != nil {
21
35
return nil , err
22
36
}
23
37
}
@@ -36,6 +50,17 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error
36
50
Queries : queries ,
37
51
}
38
52
53
+ funcMap := template.FuncMap {
54
+ "lowerTitle" : sdk .LowerTitle ,
55
+ "comment" : sdk .DoubleSlashComment ,
56
+ "imports" : i .Imports ,
57
+ "offset" : Offset ,
58
+ }
59
+
60
+ modelsFile := template .Must (template .New ("table" ).Funcs (funcMap ).Parse (ktModelsTmpl ))
61
+ sqlFile := template .Must (template .New ("table" ).Funcs (funcMap ).Parse (ktSqlTmpl ))
62
+ ifaceFile := template .Must (template .New ("table" ).Funcs (funcMap ).Parse (ktIfaceTmpl ))
63
+
39
64
core .DefaultImporter = i
40
65
41
66
tctx := core.KtTmplCtx {
@@ -50,11 +75,11 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error
50
75
51
76
output := map [string ]string {}
52
77
53
- execute := func (name string , f func (io. Writer , core. KtTmplCtx ) error ) error {
78
+ execute := func (name string , t * template. Template ) error {
54
79
var b bytes.Buffer
55
80
w := bufio .NewWriter (& b )
56
81
tctx .SourceName = name
57
- err := f (w , tctx )
82
+ err := t . Execute (w , tctx )
58
83
w .Flush ()
59
84
if err != nil {
60
85
return err
@@ -66,13 +91,13 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error
66
91
return nil
67
92
}
68
93
69
- if err := execute ("Models.kt" , tmpl . KtModels ); err != nil {
94
+ if err := execute ("Models.kt" , modelsFile ); err != nil {
70
95
return nil , err
71
96
}
72
- if err := execute ("Queries.kt" , tmpl . KtIface ); err != nil {
97
+ if err := execute ("Queries.kt" , ifaceFile ); err != nil {
73
98
return nil , err
74
99
}
75
- if err := execute ("QueriesImpl.kt" , tmpl . KtSQL ); err != nil {
100
+ if err := execute ("QueriesImpl.kt" , sqlFile ); err != nil {
76
101
return nil , err
77
102
}
78
103
0 commit comments