@@ -7,16 +7,134 @@ import (
77 "io"
88 "io/fs"
99 "log"
10+ "path/filepath"
1011 "sort"
1112 "strings"
1213 "text/template"
1314
1415 "github.com/sqlc-dev/plugin-sdk-go/plugin"
1516 "github.com/sqlc-dev/sqlc-gen-go/internal/opts"
17+ connecttemplates "github.com/walterwanderley/sqlc-connect/templates"
1618 "github.com/walterwanderley/sqlc-grpc/converter"
1719 "github.com/walterwanderley/sqlc-grpc/metadata"
20+ grpctemplates "github.com/walterwanderley/sqlc-grpc/templates"
1821)
1922
23+ func serverFiles (req * plugin.GenerateRequest , options * opts.Options , enums []Enum , structs []Struct , queries []Query ) ([]* plugin.File , error ) {
24+ var (
25+ tmplFS fs.FS
26+ tmplFuncs template.FuncMap
27+ )
28+
29+ switch options .ServerType {
30+ case "" , "grpc" : // the default server type
31+ tmplFS = grpctemplates .Files
32+ tmplFuncs = grpctemplates .Funcs
33+ case "connect" :
34+ tmplFS = connecttemplates .Files
35+ tmplFuncs = connecttemplates .Funcs
36+ default :
37+ return nil , fmt .Errorf ("invalid server_type %q. Choose 'connect' or 'grpc'" , options .ServerType )
38+ }
39+ def := toServerDefinition (req , options , enums , structs , queries )
40+ if err := def .Validate (); err != nil {
41+ return nil , err
42+ }
43+ pkg := def .Packages [0 ]
44+ depth := make ([]string , 0 )
45+ for i := 0 ; i < len (strings .Split (options .Out , string (filepath .Separator )))+ 1 ; i ++ {
46+ depth = append (depth , ".." )
47+ }
48+ toRootPath := filepath .Join (depth ... )
49+ files := make ([]* plugin.File , 0 )
50+ err := fs .WalkDir (tmplFS , "." , func (path string , d fs.DirEntry , err error ) error {
51+ if err != nil {
52+ log .Println ("ERROR " , err .Error ())
53+ return err
54+ }
55+
56+ if d .IsDir () {
57+ return nil
58+ }
59+
60+ newPath := strings .TrimSuffix (path , ".tmpl" )
61+
62+ if strings .HasSuffix (newPath , "templates.go" ) {
63+ return nil
64+ }
65+ log .Println (path , "..." )
66+ if strings .HasSuffix (newPath , "service.proto" ) {
67+ protoContent , err := execServerTemplate (tmplFS , tmplFuncs , path , pkg , false )
68+ if err != nil {
69+ return err
70+ }
71+ protoFile := filepath .Join (toRootPath , "proto" , converter .ToSnakeCase (pkg .Package ), "v1" , (converter .ToSnakeCase (pkg .Package ) + ".proto" ))
72+ files = append (files , & plugin.File {
73+ Name : protoFile ,
74+ Contents : protoContent ,
75+ })
76+ return nil
77+ }
78+
79+ if strings .HasSuffix (newPath , "adapters.go" ) || strings .HasSuffix (newPath , "service.go" ) || strings .HasSuffix (newPath , "service.factory.go" ) {
80+ if options .Append && strings .HasSuffix (newPath , "service.factory.go" ) {
81+ return nil
82+ }
83+ content , err := execServerTemplate (tmplFS , tmplFuncs , path , pkg , true )
84+ if err != nil {
85+ return err
86+ }
87+ files = append (files , & plugin.File {
88+ Name : newPath ,
89+ Contents : content ,
90+ })
91+ return nil
92+ }
93+
94+ if options .Append {
95+ return nil
96+ }
97+
98+ if strings .HasSuffix (newPath , "tracing.go" ) && ! def .DistributedTracing {
99+ return nil
100+ }
101+
102+ if strings .HasSuffix (newPath , "migration.go" ) && def .MigrationPath == "" {
103+ return nil
104+ }
105+
106+ if strings .HasSuffix (newPath , "litestream.go" ) && ! (def .Database () == "sqlite" && def .Litestream ) {
107+ return nil
108+ }
109+
110+ if (strings .HasSuffix (newPath , "litefs.go" ) || strings .HasSuffix (newPath , "forward.go" )) && ! (def .Database () == "sqlite" && def .LiteFS ) {
111+ return nil
112+ }
113+
114+ content , err := execServerTemplate (tmplFS , tmplFuncs , path , def , strings .HasSuffix (newPath , ".go" ))
115+ if err != nil {
116+ return err
117+ }
118+ files = append (files , & plugin.File {
119+ Name : filepath .Join (toRootPath , newPath ),
120+ Contents : content ,
121+ })
122+
123+ return nil
124+ })
125+ if err != nil {
126+ return nil , err
127+ }
128+ if ! options .SkipGoMod {
129+ files = append (files , & plugin.File {
130+ Name : filepath .Join (toRootPath , "go.mod" ),
131+ Contents : []byte (fmt .Sprintf ("module %s\n " , def .GoModule )),
132+ })
133+ }
134+
135+ return files , nil
136+ }
137+
20138func toServerDefinition (req * plugin.GenerateRequest , options * opts.Options , enums []Enum , structs []Struct , queries []Query ) * metadata.Definition {
21139 module := options .Module
22140 if module == "" {
0 commit comments