示例#1
0
func genTypesOnlyAPI(file string) error {
	api := &api.API{
		NoInflections:        true,
		NoRemoveUnusedShapes: true,
	}
	api.Attach(file)
	// to reset imports so that timestamp has an entry in the map.
	api.APIGoCode()

	var buf bytes.Buffer
	err := typesOnlyTplAPI.Execute(&buf, api)
	if err != nil {
		panic(err)
	}
	code := strings.TrimSpace(buf.String())
	code = util.GoFmt(code)

	// Ignore dir error, filepath will catch it for an invalid path.
	os.Mkdir(api.PackageName(), 0755)
	// Fix imports.
	codeWithImports, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", api.PackageName(), code)), nil)
	if err != nil {
		fmt.Println(err)
		return err
	}
	outFile := filepath.Join(api.PackageName(), "api.go")
	err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, codeWithImports)), 0644)
	if err != nil {
		return err
	}
	return nil
}
示例#2
0
func genFull(file string) error {
	api := &api.API{}
	api.Attach(file)

	// Ignore dir error, filepath will catch it for an invalid path.
	os.Mkdir(api.PackageName(), 0755)
	outFile := filepath.Join(api.PackageName(), "api.go")
	err := ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\npackage %s\n\n%s", copyrightHeader, api.PackageName(), api.APIGoCode())), 0644)
	if err != nil {
		return err
	}

	outFile = filepath.Join(api.PackageName(), "service.go")
	err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\npackage %s\n\n%s", copyrightHeader, api.PackageName(), api.ServiceGoCode())), 0644)
	if err != nil {
		return err
	}
	return nil
}