Skip to content

Commit 8501c93

Browse files
committed
Fix preserving imports
1 parent ea9a9d3 commit 8501c93

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ coderd/database/dump.sql: coderd/database/gen/dump/main.go $(wildcard coderd/dat
486486
go run ./coderd/database/gen/dump/main.go
487487

488488
# Generates Go code for querying the database.
489-
coderd/database/querier.go: coderd/database/sqlc.yaml coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql) coderd/database/gen/enum/main.go coderd/database/gen/fake/main.go
489+
coderd/database/querier.go: coderd/database/dbfake/dbfake.go coderd/database/sqlc.yaml coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql) coderd/database/gen/enum/main.go coderd/database/gen/fake/main.go
490490
./coderd/database/generate.sh
491491

492492

coderd/database/gen/fake/main.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package main
22

33
import (
4+
"go/format"
5+
"go/token"
46
"log"
57
"os"
68

79
"github.com/dave/dst"
810
"github.com/dave/dst/decorator"
11+
"github.com/dave/dst/decorator/resolver/goast"
12+
"github.com/dave/dst/decorator/resolver/guess"
913
"golang.org/x/xerrors"
1014
)
1115

@@ -27,11 +31,13 @@ func run() error {
2731
}
2832
declByName := map[string]*dst.FuncDecl{}
2933

30-
dbfake, err := os.ReadFile("../../dbfake/dbfake.go")
34+
dbfake, err := os.ReadFile("./dbfake/dbfake.go")
3135
if err != nil {
3236
return xerrors.Errorf("read dbfake: %w", err)
3337
}
34-
f, err := decorator.Parse(dbfake)
38+
39+
// Required to preserve imports!
40+
f, err := decorator.NewDecoratorWithImports(token.NewFileSet(), "dbfake", goast.New()).Parse(dbfake)
3541
if err != nil {
3642
return xerrors.Errorf("parse dbfake: %w", err)
3743
}
@@ -64,7 +70,13 @@ func run() error {
6470
// Not implemented!
6571
decl = &dst.FuncDecl{
6672
Name: dst.NewIdent(fn.Name),
67-
Type: fn.Func,
73+
Type: &dst.FuncType{
74+
Func: true,
75+
TypeParams: fn.Func.TypeParams,
76+
Params: fn.Func.Params,
77+
Results: fn.Func.Results,
78+
Decs: fn.Func.Decs,
79+
},
6880
Recv: &dst.FieldList{
6981
List: []*dst.Field{{
7082
Names: []*dst.Ident{dst.NewIdent("q")},
@@ -81,7 +93,7 @@ func run() error {
8193
Decs: dst.BlockStmtDecorations{
8294
Lbrace: dst.Decorations{
8395
"\n",
84-
"// Implement me!",
96+
"// Not implemented",
8597
},
8698
},
8799
},
@@ -90,18 +102,20 @@ func run() error {
90102
f.Decls = append(f.Decls, decl)
91103
}
92104

93-
file, err := os.OpenFile("../../dbfake/dbfake.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
105+
file, err := os.OpenFile("./dbfake/dbfake.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
94106
if err != nil {
95107
return xerrors.Errorf("open dbfake: %w", err)
96108
}
97109
defer file.Close()
98110

99-
err = decorator.Fprint(file, f)
111+
// Required to preserve imports!
112+
restorer := decorator.NewRestorerWithImports("dbfake", guess.New())
113+
restored, err := restorer.RestoreFile(f)
100114
if err != nil {
101-
return xerrors.Errorf("write dbfake: %w", err)
115+
return xerrors.Errorf("restore dbfake: %w", err)
102116
}
103-
104-
return nil
117+
err = format.Node(file, restorer.Fset, restored)
118+
return err
105119
}
106120

107121
type storeMethod struct {
@@ -110,7 +124,7 @@ type storeMethod struct {
110124
}
111125

112126
func readStoreInterface() ([]storeMethod, error) {
113-
querier, err := os.ReadFile("../../querier.go")
127+
querier, err := os.ReadFile("./querier.go")
114128
if err != nil {
115129
return nil, xerrors.Errorf("read querier: %w", err)
116130
}
@@ -150,6 +164,23 @@ func readStoreInterface() ([]storeMethod, error) {
150164
if !ok {
151165
continue
152166
}
167+
168+
for _, t := range []*dst.FieldList{funcType.Params, funcType.Results} {
169+
if t == nil {
170+
continue
171+
}
172+
for _, f := range t.List {
173+
ident, ok := f.Type.(*dst.Ident)
174+
if !ok {
175+
continue
176+
}
177+
if !ident.IsExported() {
178+
continue
179+
}
180+
ident.Path = "github.com/coder/coder/coderd/database"
181+
}
182+
}
183+
153184
funcs = append(funcs, storeMethod{
154185
Name: method.Names[0].Name,
155186
Func: funcType,

coderd/database/generate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
5959
# Generate enums (e.g. unique constraints).
6060
go run gen/enum/main.go
6161

62-
# Generate the database fake!
62+
# Generate the database fake!
6363
go run gen/fake/main.go
6464
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy