Exemplo n.º 1
0
func GenerateCapnp(apiDef *raml.APIDefinition, dir, lang, pkg string) error {
	// create directory if needed
	if err := commons.CheckCreateDir(dir); err != nil {
		return err
	}

	structs := []Struct{}

	// generate types
	for name, tipe := range apiDef.Types {
		s, err := NewStruct(tipe, name, lang, pkg)
		if err != nil {
			return err
		}
		structs = append(structs, s)
	}
	for _, s := range structs {
		registerType(s.Name)
	}

	for _, s := range structs {
		if err := s.Generate(dir); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
// GenerateServer generates API server files
func GenerateServer(ramlFile, dir, packageName, lang, apiDocsDir, rootImportPath string, generateMain bool) error {
	apiDef := new(raml.APIDefinition)
	// parse the raml file
	ramlBytes, err := raml.ParseReadFile(ramlFile, apiDef)
	if err != nil {
		return err
	}

	// create directory if needed
	if err := commons.CheckCreateDir(dir); err != nil {
		return err
	}

	switch lang {
	case langGo:
		if rootImportPath == "" {
			return fmt.Errorf("invalid import path = empty")
		}
		gs := golang.NewServer(apiDef, packageName, apiDocsDir, rootImportPath, generateMain)
		err = gs.Generate(dir)
	case langPython:
		ps := python.NewServer(apiDef, apiDocsDir, generateMain)
		err = ps.Generate(dir)
	case langNim:
		ns := nim.NewServer(apiDef, apiDocsDir, dir)
		err = ns.Generate()
	default:
		return errInvalidLang
	}
	if err != nil {
		return err
	}

	if apiDocsDir == "" {
		return nil
	}

	if lang == langNim {
		apiDocsDir = "public/" + apiDocsDir
	}

	log.Infof("Generating API Docs to %v", apiDocsDir)

	return apidocs.Generate(ramlBytes, filepath.Join(dir, apiDocsDir))
}
Exemplo n.º 3
0
// generate code of this library
func (l *library) generate() error {
	// create directory if needed
	if err := commons.CheckCreateDir(l.dir); err != nil {
		return err
	}

	// write empty __init__.py in each dir
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() {
			return nil
		}
		return generateEmptyInitPy(path)
	}

	if err := filepath.Walk(l.baseDir, walkFn); err != nil {
		return err
	}

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

	// security schemes
	if err := generateSecurity(l.SecuritySchemes, l.dir); err != nil {
		return err
	}

	// included libraries
	for _, ramlLib := range l.Libraries {
		childLib := newLibrary(ramlLib, l.baseDir)
		if err := childLib.generate(); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 4
0
func (gh goramlHelper) generate(dir string) error {
	globGoramlPkgDir = gh.packageDir
	pkgDir := filepath.Join(dir, gh.packageDir)

	// create directory if needed
	if err := commons.CheckCreateDir(pkgDir); err != nil {
		return err
	}

	/// dates
	d := dateGen{PackageName: gh.packageName}
	if err := d.generate(pkgDir); err != nil {
		log.Errorf("generate() failed to generate date files:%v", err)
		return err
	}

	// generate struct validator
	if err := generateInputValidator(gh.packageName, pkgDir); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 5
0
// GenerateClient generates client library
func GenerateClient(apiDef *raml.APIDefinition, dir, packageName, lang, rootImportPath string) error {
	//check create dir
	if err := commons.CheckCreateDir(dir); err != nil {
		return err
	}

	switch lang {
	case langGo:
		gc, err := golang.NewClient(apiDef, packageName, rootImportPath)
		if err != nil {
			return err
		}
		return gc.Generate(dir)
	case langPython:
		pc := python.NewClient(apiDef)
		return pc.Generate(dir)
	case langNim:
		nc := nim.NewClient(apiDef, dir)
		return nc.Generate()
	default:
		return errInvalidLang
	}
}
Exemplo n.º 6
0
// generate code of this library
func (l *goLibrary) generate() error {
	if err := commons.CheckCreateDir(l.dir); err != nil {
		return err
	}

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

	// security schemes
	if err := generateSecurity(l.SecuritySchemes, l.dir, l.PackageName); err != nil {
		return err
	}

	// included libraries
	for name, ramlLib := range l.Libraries {
		childLib := newGoLibrary(name, ramlLib, l.baseDir)
		if err := childLib.generate(); err != nil {
			return err
		}
	}
	return nil
}