MCPcopy Create free account
hub / github.com/adhocteam/pushup / genCodeLayout

Function genCodeLayout

codegen.go:200–286  ·  view source on GitHub ↗
(g *layoutCodeGen)

Source from the content-addressed store, hash-verified

198const methodReceiverName = "up"
199
200func genCodeLayout(g *layoutCodeGen) ([]byte, error) {
201 // FIXME(paulsmith): need way to specify this as user
202 packageName := "build"
203
204 g.outPrintf("// this file is mechanically generated, do not edit!\n")
205 g.outPrintf("// version: ")
206 printVersion(&g.outb)
207 g.outPrintf("\n")
208 g.outPrintf("package %s\n\n", packageName)
209
210 type field struct {
211 name string
212 typ string
213 }
214
215 fields := []field{}
216
217 typename := generatedTypename(g.pfile, upFileLayout)
218
219 g.bodyPrintf("type %s struct {\n", typename)
220 for _, field := range fields {
221 g.bodyPrintf("%s %s\n", field.name, field.typ)
222 }
223 g.bodyPrintf("}\n")
224
225 g.bodyPrintf("func (%s *%s) buildCliArgs() []string {\n", methodReceiverName, typename)
226 g.bodyPrintf(" return %#v\n", os.Args)
227 g.bodyPrintf("}\n\n")
228
229 g.bodyPrintf("func init() {\n")
230 g.bodyPrintf(" layouts[\"%s\"] = new(%s)\n", layoutName(g.pfile.relpath()), typename)
231 g.bodyPrintf("}\n\n")
232
233 g.used("net/http", "html/template")
234 g.bodyPrintf("func (%s *%s) Respond(w http.ResponseWriter, req *http.Request, sections map[string]chan template.HTML) error {\n", methodReceiverName, typename)
235
236 // sections support
237 g.used("html/template")
238 g.bodyPrintf(`
239sectionDefined := func(name string) bool {
240 _, ok := sections[name]
241 return ok
242}
243_ = sectionDefined
244
245outputSection := func(name string) template.HTML {
246 return <-sections[name]
247}
248`)
249
250 // Make a new scope for the user's code block and HTML. This will help (but not fully prevent)
251 // name collisions with the surrounding code.
252 g.bodyPrintf("\n// Begin user Go code and HTML\n")
253 g.bodyPrintf("{\n")
254
255 g.generate()
256
257 // Close the scope we started for the user code and HTML.

Callers 1

compileFunction · 0.85

Calls 8

printVersionFunction · 0.85
generatedTypenameFunction · 0.85
layoutNameFunction · 0.85
relpathMethod · 0.80
outPrintfMethod · 0.45
bodyPrintfMethod · 0.45
usedMethod · 0.45
generateMethod · 0.45

Tested by

no test coverage detected