orderAndStubDatabaseFunctions orders the functions in the file and stubs them. This is useful for when we want to add a new function to the database and we want to make sure that it's ordered correctly. querierFuncs is a list of functions that are in the database. file is the path to the file that
(filePath, receiver, structName string, stub func(params stubParams) string)
| 105 | // structName is the name of the struct that contains the functions. |
| 106 | // stub is a string that will be used to stub the functions. |
| 107 | func orderAndStubDatabaseFunctions(filePath, receiver, structName string, stub func(params stubParams) string) error { |
| 108 | declByName := map[string]*dst.FuncDecl{} |
| 109 | packageName := filepath.Base(filepath.Dir(filePath)) |
| 110 | externalMethods, err := loadExternalReceiverMethods( |
| 111 | filepath.Dir(filePath), |
| 112 | filepath.Base(filePath), |
| 113 | structName, |
| 114 | ) |
| 115 | if err != nil { |
| 116 | return xerrors.Errorf("load external receiver methods: %w", err) |
| 117 | } |
| 118 | |
| 119 | contents, err := os.ReadFile(filePath) |
| 120 | if err != nil { |
| 121 | return xerrors.Errorf("read file: %w", err) |
| 122 | } |
| 123 | |
| 124 | // Required to preserve imports! |
| 125 | f, err := decorator.NewDecoratorWithImports(token.NewFileSet(), packageName, goast.New()).Parse(contents) |
| 126 | if err != nil { |
| 127 | return xerrors.Errorf("parse file: %w", err) |
| 128 | } |
| 129 | |
| 130 | pointer := false |
| 131 | for i := 0; i < len(f.Decls); i++ { |
| 132 | funcDecl, ok := f.Decls[i].(*dst.FuncDecl) |
| 133 | if !ok || funcDecl.Recv == nil || len(funcDecl.Recv.List) == 0 { |
| 134 | continue |
| 135 | } |
| 136 | |
| 137 | var ident *dst.Ident |
| 138 | switch t := funcDecl.Recv.List[0].Type.(type) { |
| 139 | case *dst.Ident: |
| 140 | ident = t |
| 141 | case *dst.StarExpr: |
| 142 | ident, ok = t.X.(*dst.Ident) |
| 143 | if !ok { |
| 144 | continue |
| 145 | } |
| 146 | pointer = true |
| 147 | } |
| 148 | if ident == nil || ident.Name != structName { |
| 149 | continue |
| 150 | } |
| 151 | if _, ok := funcByName[funcDecl.Name.Name]; !ok { |
| 152 | continue |
| 153 | } |
| 154 | declByName[funcDecl.Name.Name] = funcDecl |
| 155 | f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) |
| 156 | i-- |
| 157 | } |
| 158 | |
| 159 | for _, fn := range funcs { |
| 160 | if _, ok := externalMethods[fn.Name]; ok { |
| 161 | continue |
| 162 | } |
| 163 | |
| 164 | var bodyStmts []dst.Stmt |
no test coverage detected