// processResource recursively process resources and their nested children // and returns the path so far for the children. The function takes a routerFunc // as an argument that is invoked with the verb, resource path and handler as // the resources are processed, so the calling code can use pat, mux, httprouter // or whatever router they desire and we don't need to know about it. func processResource(parent, name string, resource *raml.Resource, params []*Parameter, routerFunc func(s *Endpoint)) error { var path = parent + name var err error for name, param := range resource.UriParameters { params = append(params, newParam(name, ¶m)) } s := make([]*Endpoint, 0, 6) for _, m := range resource.Methods() { s, err = appendEndpoint(s, m, params) if err != nil { return err } } for _, ep := range s { ep.Path = path routerFunc(ep) } // Get all children. for nestname, nested := range resource.Nested { return processResource(path, nestname, nested, params, routerFunc) } return nil }
// generateRoute builds app/Http/routes.php func generateRoute(parent, uri string, resource *raml.Resource, e *template.Template, f *os.File) { path := parent + uri err := e.Execute(f, Route{path, resource.UriParameters, resource.Methods(), resource.Description, resource.DisplayName}) if err != nil { log.Println("executing template:", err) } // Get all children. for nestname, nested := range resource.Nested { generateRoute(path, nestname, nested, e, f) } }
// generateResource creates a handler struct from an API resource // and executes the associated template. func generateResource(parent, name string, resource *raml.Resource, t *template.Template, f *os.File) string { path := parent + name for _, method := range resource.Methods() { err := t.Execute(f, HandlerInfo{ramlapi.Variableize(method.DisplayName), method.Name, path, method.Description}) if err != nil { log.Println("executing template:", err) } } // Get all children. for nestname, nested := range resource.Nested { return generateResource(path, nestname, nested, t, f) } return path }
// generateMap builds a map of string labels to handler funcs - this is // used by the calling code to link the display name strings that come // from the RAML file to handler funcs in the client code. func generateMap(parent, name string, resource *raml.Resource, e *template.Template, f *os.File) { path := parent + name for _, method := range resource.Methods() { name := ramlapi.Variableize(method.DisplayName) err := e.Execute(f, RouteMapEntry{name, name}) if err != nil { log.Println("executing template:", err) } } // Get all children. for nestname, nested := range resource.Nested { generateMap(path, nestname, nested, e, f) } }