Exemple #1
0
// Generate generates all python server files
func (ps Server) Generate(dir string) error {

	globAPIDef = ps.APIDef
	// generate input validators helper
	if err := commons.GenerateFile(struct{}{}, "./templates/input_validators_python.tmpl", "input_validators_python",
		filepath.Join(dir, "input_validators.py"), false); err != nil {
		return err
	}

	// generate request body
	if err := generateClassesFromBodies(getAllResources(ps.APIDef, true), dir); err != nil {
		return err
	}

	// python classes
	if err := generateClasses(ps.APIDef.Types, dir); err != nil {
		log.Errorf("failed to generate python clased:%v", err)
		return err
	}

	// security scheme
	if err := generateSecurity(ps.APIDef.SecuritySchemes, dir); err != nil {
		log.Errorf("failed to generate security scheme:%v", err)
		return err
	}

	// genereate resources
	rds, err := generateServerResources(ps.APIDef, dir)
	if err != nil {
		return err
	}
	ps.ResourcesDef = rds

	// libraries
	if err := generateLibraries(ps.APIDef.Libraries, dir); err != nil {
		return err
	}

	// requirements.txt file
	if err := commons.GenerateFile(nil, "./templates/requirements_python.tmpl", "requirements_python", filepath.Join(dir, "requirements.txt"), false); err != nil {
		return err
	}

	// generate main
	if ps.WithMain {
		// generate HTML front page
		if err := commons.GenerateFile(ps, "./templates/index.html.tmpl", "index.html", filepath.Join(dir, "index.html"), false); err != nil {
			return err
		}
		// main file
		return commons.GenerateFile(ps, "./templates/server_main_python.tmpl", "server_main_python", filepath.Join(dir, "app.py"), true)
	}
	return nil

}
Exemple #2
0
// Generate generates all Go server files
func (gs Server) Generate(dir string) error {
	// helper package
	gh := goramlHelper{
		rootImportPath: gs.RootImportPath,
		packageName:    "goraml",
		packageDir:     "goraml",
	}
	if err := gh.generate(dir); err != nil {
		return err
	}

	// generate all Type structs
	if err := generateStructs(gs.apiDef.Types, dir, gs.PackageName); err != nil {
		return err
	}

	// generate all request & response body
	if err := generateBodyStructs(gs.apiDef, dir, gs.PackageName, langGo); err != nil {
		return err
	}

	// security scheme
	if err := generateSecurity(gs.apiDef.SecuritySchemes, dir, gs.PackageName); err != nil {
		log.Errorf("failed to generate security scheme:%v", err)
		return err
	}

	// genereate resources
	rds, err := generateServerResources(gs.apiDef, dir, gs.PackageName)
	if err != nil {
		return err
	}
	gs.ResourcesDef = rds

	// libraries
	if err := generateLibraries(gs.apiDef.Libraries, dir); err != nil {
		return err
	}

	// generate main
	if gs.withMain {
		// HTML front page
		if err := commons.GenerateFile(gs, "./templates/index.html.tmpl", "index.html", filepath.Join(dir, "index.html"), false); err != nil {
			return err
		}
		// main file
		return commons.GenerateFile(gs, "./templates/server_main_go.tmpl", "server_main_go", filepath.Join(dir, "main.go"), true)
	}

	return nil
}
Exemple #3
0
// Generate generates struct code
func (s *Struct) Generate(dir string) error {
	if err := s.generateEnums(dir); err != nil {
		return err
	}
	filename := filepath.Join(dir, s.Name+".capnp")
	return commons.GenerateFile(s, "./templates/struct_capnp.tmpl", "struct_capnp", filename, true)
}
Exemple #4
0
// Generate generates all Nim server files
func (s *Server) Generate() error {
	s.Resources = getAllResources(s.APIDef, true)

	// generate all objects from all RAML types
	if err := generateObjects(s.APIDef.Types, s.Dir); err != nil {
		return err
	}

	// generate all objects from request/response body
	if _, err := generateObjectsFromBodies(s.Resources, s.Dir); err != nil {
		return err
	}

	// main file
	if err := s.generateMain(); err != nil {
		return err
	}

	// API implementation
	if err := generateResourceAPIs(s.Resources, s.Dir); err != nil {
		return err
	}

	// HTML front page
	if err := commons.GenerateFile(s, "./templates/index.html.tmpl", "index.html", filepath.Join(s.Dir, "index.html"), false); err != nil {
		return err
	}

	return nil
}
Exemple #5
0
func (dg dateGen) generate(dir string) error {
	dates := []struct {
		Type     string
		Format   string
		FileName string
	}{
		{"date-only", "", "date_only.go"},
		{"time-only", "", "time_only.go"},
		{"datetime-only", "", "datetime_only.go"},
		{"datetime", "RFC3339", "datetime.go"},
		{"datetime", "RFC2616", "datetime_rfc2616.go"},
	}
	for _, d := range dates {
		b, err := date.Get(d.Type, d.Format)
		if err != nil {
			return err
		}
		ctx := map[string]interface{}{
			"PackageName": dg.PackageName,
			"Content":     string(b),
		}

		err = commons.GenerateFile(ctx, "./templates/date.tmpl", "date", filepath.Join(dir, d.FileName), false)
		if err != nil {
			return err
		}
	}
	return nil

}
Exemple #6
0
// generate nim object representation
func (o *object) generate(dir string) error {
	filename := filepath.Join(dir, o.Name+".nim")
	if err := commons.GenerateFile(o, "./templates/object_nim.tmpl", "object_nim", filename, true); err != nil {
		return err
	}
	registerObject(o)
	return nil
}
Exemple #7
0
func (gc *Client) generateServices(dir string) error {
	for _, s := range gc.Services {
		sort.Sort(resource.ByEndpoint(s.Methods))
		if err := commons.GenerateFile(s, "./templates/client_service_go.tmpl", "client_service_go", s.filename(dir), false); err != nil {
			return err
		}
	}
	return nil
}
Exemple #8
0
// generate input validator helper file
func generateInputValidator(packageName, dir string) error {
	var ctx = struct {
		PackageName string
	}{
		PackageName: packageName,
	}
	fileName := filepath.Join(dir, inputValidatorFileResult)
	return commons.GenerateFile(ctx, inputValidatorTemplateLocation, "struct_input_validator_template", fileName, true)
}
Exemple #9
0
// Generate generates python client library files
func (c Client) Generate(dir string) error {
	globAPIDef = c.APIDef
	// generate empty __init__.py
	if err := generateEmptyInitPy(dir); err != nil {
		return err
	}

	// generate helper
	if err := commons.GenerateFile(nil, "./templates/client_utils_python.tmpl", "client_utils_python", filepath.Join(dir, "client_utils.py"), false); err != nil {
		return err
	}

	if err := c.generateServices(dir); err != nil {
		return err
	}
	// generate main client lib file
	return commons.GenerateFile(c, "./templates/client_python.tmpl", "client_python", filepath.Join(dir, "client.py"), true)
}
Exemple #10
0
// generate Go client lib file
func (gc *Client) generateClientFile(dir string) error {
	fileName := filepath.Join(dir, "/client_"+strings.ToLower(gc.Name)+".go")
	return commons.GenerateFile(gc, "./templates/client_go.tmpl", "client_go", fileName, false)
}
Exemple #11
0
// generate Go struct
func (sd structDef) generate(dir string) error {
	fileName := filepath.Join(dir, sd.Name+".go")
	return commons.GenerateFile(sd, structTemplateLocation, "struct_template", fileName, false)
}
Exemple #12
0
func (c *Client) generateMain() error {
	filename := filepath.Join(c.Dir, "client.nim")
	return commons.GenerateFile(c, "./templates/client_nim.tmpl", "client_nim", filename, true)
}
Exemple #13
0
func (cs *clientService) generate(dir string) error {
	filename := filepath.Join(dir, cs.Name+"_service.nim")
	return commons.GenerateFile(cs, "./templates/client_service_nim.tmpl", "client_service_nim", filename, true)
}
Exemple #14
0
// generate interface file of a resource
func (gr *goResource) generateInterfaceFile(directory string) error {
	filename := directory + "/" + strings.ToLower(gr.Name) + "_if.go"
	return commons.GenerateFile(gr, resourceIfTemplate, "resource_if_template", filename, true)
}
Exemple #15
0
func (r *resource) generate(dir string) error {
	filename := filepath.Join(dir, r.apiName()+".nim")
	return commons.GenerateFile(r, "./templates/server_resources_api_nim.tmpl", "server_resources_api_nim", filename, true)
}
Exemple #16
0
// generate Go client helper
func (gc *Client) generateHelperFile(dir string) error {
	fileName := filepath.Join(dir, "/client_utils.go")
	return commons.GenerateFile(gc, "./templates/client_utils_go.tmpl", "client_utils_go", fileName, false)
}
Exemple #17
0
// generate Go representation of a security scheme
// it implemented as struct based middleware
func (gs *goSecurity) generate(dir string) error {
	fileName := path.Join(dir, "oauth2_"+gs.Name+"_middleware.go")
	return commons.GenerateFile(gs, "./templates/oauth2_middleware.tmpl", "oauth2_middleware", fileName, false)
}
Exemple #18
0
func (e *enum) generate(dir string) error {
	filename := filepath.Join(dir, e.Name+".capnp")
	return commons.GenerateFile(e, "./templates/enum_capnp.tmpl", "enum_capnp", filename, true)
}
Exemple #19
0
// generate security schheme representation in python.
// security scheme is generated as a middleware
func (ps *pythonSecurity) generate(dir string) error {
	fileName := path.Join(dir, "oauth2_"+ps.Name+".py")
	return commons.GenerateFile(ps, "./templates/oauth2_middleware_python.tmpl", "oauth2_middleware_python", fileName, false)
}
Exemple #20
0
// generate a python class file
func (pc *class) generate(dir string) error {
	fileName := filepath.Join(dir, pc.Name+".py")
	return commons.GenerateFile(pc, "./templates/class_python.tmpl", "class_python", fileName, false)
}
Exemple #21
0
// main generates main file
func (s *Server) generateMain() error {
	filename := filepath.Join(s.Dir, "main.nim")
	return commons.GenerateFile(s, "./templates/server_main_nim.tmpl", "server_main_nim", filename, true)
}
Exemple #22
0
// generate API file of a resource
func (gr *goResource) generateAPIFile(directory string) error {
	filename := directory + "/" + strings.ToLower(gr.Name) + "_api.go"
	return commons.GenerateFile(gr, resourceAPITemplate, "resource_api_template", filename, false)
}
Exemple #23
0
// generate flask representation of an RAML resource
// It has one file : an API route and implementation
func (pr *pythonResource) generate(r *raml.Resource, URI, dir string) error {
	pr.GenerateMethods(r, "python", newServerMethod, newClientMethod)
	pr.setMiddlewares()
	filename := dir + "/" + strings.ToLower(pr.Name) + ".py"
	return commons.GenerateFile(pr, resourcePyTemplate, "resource_python_template", filename, true)
}
Exemple #24
0
// generate empty __init__.py without overwrite it
func generateEmptyInitPy(dir string) error {
	return commons.GenerateFile(nil, "./templates/init_py.tmpl", "init_py", filepath.Join(dir, "__init__.py"), false)
}