(g *layoutCodeGen)
| 198 | const methodReceiverName = "up" |
| 199 | |
| 200 | func 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(` |
| 239 | sectionDefined := func(name string) bool { |
| 240 | _, ok := sections[name] |
| 241 | return ok |
| 242 | } |
| 243 | _ = sectionDefined |
| 244 | |
| 245 | outputSection := 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. |
no test coverage detected