Example #1
0
// create new library instance
func newGoLibrary(name string, lib *raml.Library, baseDir string) *goLibrary {
	return &goLibrary{
		Library:     lib,
		baseDir:     baseDir,
		PackageName: commons.NormalizePkgName(name),
		dir:         commons.NormalizePkgName(filepath.Join(baseDir, goLibPackageDir(name, lib.Filename))),
	}
}
Example #2
0
func libImportPath(typ, prefix string) (string, string) {
	// library use '.'
	if strings.Index(typ, ".") < 0 {
		return prefix + typ, prefix + typ
	}

	splitted := strings.Split(typ, ".")
	if len(splitted) != 2 {
		log.Fatalf("pythonLibImportPath invalid typ:" + typ)
	}
	// library name in the current document
	libName := splitted[0]

	// raml file of this lib
	libRAMLFile := globAPIDef.FindLibFile(commons.DenormalizePkgName(libName))

	if libRAMLFile == "" {
		log.Fatalf("pythonLibImportPath() can't find library : %v", libName)
	}

	// relative lib package
	libPkg := libRelDir(libRAMLFile)

	return strings.Replace(commons.NormalizePkgName(libPkg), "/", ".", -1) + "." + prefix + splitted[1], prefix + splitted[1]
}
Example #3
0
// convert from raml type to go type
func convertToGoType(tip string) string {
	if v, ok := typeMap[tip]; ok {
		return v
	}
	goramlPkgDir := func() string {
		if globGoramlPkgDir == "" {
			return ""
		}
		return globGoramlPkgDir + "."
	}()
	dateMap := map[string]string{
		"date":          goramlPkgDir + "Date",
		"date-only":     goramlPkgDir + "DateOnly",
		"time-only":     goramlPkgDir + "TimeOnly",
		"datetime-only": goramlPkgDir + "DatetimeOnly",
		"datetime":      goramlPkgDir + "DateTime",
	}

	if v, ok := dateMap[tip]; ok {
		return v
	}

	// other types that need some processing
	switch {
	case strings.HasSuffix(tip, "[][]"): // bidimensional array
		return "[][]" + convertToGoType(tip[:len(tip)-4])
	case strings.HasSuffix(tip, "[]"): // array
		return "[]" + convertToGoType(tip[:len(tip)-2])
	case strings.HasSuffix(tip, "{}"): // map
		return "map[string]" + convertToGoType(tip[:len(tip)-2])
	case strings.Index(tip, "|") > 0:
		return convertUnion(tip)
	}
	return commons.NormalizePkgName(tip)
}
Example #4
0
func newLibrary(lib *raml.Library, baseDir string) *library {
	pl := library{
		Library: lib,
		baseDir: baseDir,
	}

	// package directory : filename without the extension
	relDir := libRelDir(pl.Filename)
	pl.dir = commons.NormalizePkgName(filepath.Join(pl.baseDir, relDir))

	return &pl
}
Example #5
0
// returns Go package directory of a library
// name is library name. filename is library file name.
// for the rule, see comment of `type goLibrary struct`
func goLibPackageDir(name, filename string) string {
	return commons.NormalizePkgName(filepath.Join(filepath.Dir(filename), name))
}