MCPcopy Index your code
hub / github.com/coder/coder / orderAndStubDatabaseFunctions

Function orderAndStubDatabaseFunctions

scripts/dbgen/main.go:107–263  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
107func 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

Callers 1

runFunction · 0.85

Calls 9

FileFunction · 0.92
compileFuncDeclFunction · 0.85
ReadFileMethod · 0.65
ParseMethod · 0.65
NewMethod · 0.65
NodeMethod · 0.65
ErrorfMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected