|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/dave/dst" |
| 8 | + "github.com/dave/dst/decorator" |
| 9 | + "golang.org/x/xerrors" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + err := run() |
| 14 | + if err != nil { |
| 15 | + log.Fatal(err) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func run() error { |
| 20 | + funcs, err := readStoreInterface() |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + funcByName := map[string]struct{}{} |
| 25 | + for _, f := range funcs { |
| 26 | + funcByName[f.Name] = struct{}{} |
| 27 | + } |
| 28 | + declByName := map[string]*dst.FuncDecl{} |
| 29 | + |
| 30 | + dbfake, err := os.ReadFile("../../dbfake/dbfake.go") |
| 31 | + if err != nil { |
| 32 | + return xerrors.Errorf("read dbfake: %w", err) |
| 33 | + } |
| 34 | + f, err := decorator.Parse(dbfake) |
| 35 | + if err != nil { |
| 36 | + return xerrors.Errorf("parse dbfake: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + for i := 0; i < len(f.Decls); i++ { |
| 40 | + funcDecl, ok := f.Decls[i].(*dst.FuncDecl) |
| 41 | + if !ok || funcDecl.Recv == nil || len(funcDecl.Recv.List) == 0 { |
| 42 | + continue |
| 43 | + } |
| 44 | + // Check if the receiver is the struct we're interested in |
| 45 | + starExpr, ok := funcDecl.Recv.List[0].Type.(*dst.StarExpr) |
| 46 | + if !ok { |
| 47 | + continue |
| 48 | + } |
| 49 | + ident, ok := starExpr.X.(*dst.Ident) |
| 50 | + if !ok || ident.Name != "fakeQuerier" { |
| 51 | + continue |
| 52 | + } |
| 53 | + if _, ok := funcByName[funcDecl.Name.Name]; !ok { |
| 54 | + continue |
| 55 | + } |
| 56 | + declByName[funcDecl.Name.Name] = funcDecl |
| 57 | + f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) |
| 58 | + i-- |
| 59 | + } |
| 60 | + |
| 61 | + for _, fn := range funcs { |
| 62 | + decl, ok := declByName[fn.Name] |
| 63 | + if !ok { |
| 64 | + // Not implemented! |
| 65 | + decl = &dst.FuncDecl{ |
| 66 | + Name: dst.NewIdent(fn.Name), |
| 67 | + Type: fn.Func, |
| 68 | + Recv: &dst.FieldList{ |
| 69 | + List: []*dst.Field{{ |
| 70 | + Names: []*dst.Ident{dst.NewIdent("q")}, |
| 71 | + Type: dst.NewIdent("*fakeQuerier"), |
| 72 | + }}, |
| 73 | + }, |
| 74 | + Decs: dst.FuncDeclDecorations{ |
| 75 | + NodeDecs: dst.NodeDecs{ |
| 76 | + Before: dst.EmptyLine, |
| 77 | + After: dst.EmptyLine, |
| 78 | + }, |
| 79 | + }, |
| 80 | + Body: &dst.BlockStmt{ |
| 81 | + Decs: dst.BlockStmtDecorations{ |
| 82 | + Lbrace: dst.Decorations{ |
| 83 | + "\n", |
| 84 | + "// Implement me!", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + } |
| 90 | + f.Decls = append(f.Decls, decl) |
| 91 | + } |
| 92 | + |
| 93 | + file, err := os.OpenFile("../../dbfake/dbfake.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) |
| 94 | + if err != nil { |
| 95 | + return xerrors.Errorf("open dbfake: %w", err) |
| 96 | + } |
| 97 | + defer file.Close() |
| 98 | + |
| 99 | + err = decorator.Fprint(file, f) |
| 100 | + if err != nil { |
| 101 | + return xerrors.Errorf("write dbfake: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +type storeMethod struct { |
| 108 | + Name string |
| 109 | + Func *dst.FuncType |
| 110 | +} |
| 111 | + |
| 112 | +func readStoreInterface() ([]storeMethod, error) { |
| 113 | + querier, err := os.ReadFile("../../querier.go") |
| 114 | + if err != nil { |
| 115 | + return nil, xerrors.Errorf("read querier: %w", err) |
| 116 | + } |
| 117 | + f, err := decorator.Parse(querier) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + var sqlcQuerier *dst.InterfaceType |
| 123 | + for _, decl := range f.Decls { |
| 124 | + genDecl, ok := decl.(*dst.GenDecl) |
| 125 | + if !ok { |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + for _, spec := range genDecl.Specs { |
| 130 | + typeSpec, ok := spec.(*dst.TypeSpec) |
| 131 | + if !ok { |
| 132 | + continue |
| 133 | + } |
| 134 | + if typeSpec.Name.Name != "sqlcQuerier" { |
| 135 | + continue |
| 136 | + } |
| 137 | + sqlcQuerier, ok = typeSpec.Type.(*dst.InterfaceType) |
| 138 | + if !ok { |
| 139 | + return nil, xerrors.Errorf("unexpected sqlcQuerier type: %T", typeSpec.Type) |
| 140 | + } |
| 141 | + break |
| 142 | + } |
| 143 | + } |
| 144 | + if sqlcQuerier == nil { |
| 145 | + return nil, xerrors.Errorf("sqlcQuerier not found") |
| 146 | + } |
| 147 | + funcs := []storeMethod{} |
| 148 | + for _, method := range sqlcQuerier.Methods.List { |
| 149 | + funcType, ok := method.Type.(*dst.FuncType) |
| 150 | + if !ok { |
| 151 | + continue |
| 152 | + } |
| 153 | + funcs = append(funcs, storeMethod{ |
| 154 | + Name: method.Names[0].Name, |
| 155 | + Func: funcType, |
| 156 | + }) |
| 157 | + } |
| 158 | + return funcs, nil |
| 159 | +} |
0 commit comments