// GoLangOpts for rendering items as golang code
GoLangOpts for rendering items as golang code
// GoLangOpts for rendering items as golang code
func GoLangOpts() *LanguageOpts {
	var goOtherReservedSuffixes = map[string]bool{
		"android":   true,
		"darwin":    true,
		"dragonfly": true,
		"freebsd":   true,
		"js":        true,
		"linux":     true,
		"nacl":      true,
		"netbsd":    true,
		"openbsd":   true,
		"plan9":     true,
		"solaris":   true,
		"windows":   true,
		"zos":       true,
		"386":         true,
		"amd64":       true,
		"amd64p32":    true,
		"arm":         true,
		"armbe":       true,
		"arm64":       true,
		"arm64be":     true,
		"mips":        true,
		"mipsle":      true,
		"mips64":      true,
		"mips64le":    true,
		"mips64p32":   true,
		"mips64p32le": true,
		"ppc":         true,
		"ppc64":       true,
		"ppc64le":     true,
		"riscv":       true,
		"riscv64":     true,
		"s390":        true,
		"s390x":       true,
		"sparc":       true,
		"sparc64":     true,
		"wasm":        true,
		"test": true,
	}
	opts := new(LanguageOpts)
	opts.ReservedWords = []string{
		"break", "default", "func", "interface", "select",
		"case", "defer", "go", "map", "struct",
		"chan", "else", "goto", "package", "switch",
		"const", "fallthrough", "if", "range", "type",
		"continue", "for", "import", "return", "var",
	}
	opts.formatFunc = func(ffn string, content []byte) ([]byte, error) {
		opts := new(imports.Options)
		opts.TabIndent = true
		opts.TabWidth = 2
		opts.Fragment = true
		opts.Comments = true
		return imports.Process(ffn, content, opts)
	}
	opts.fileNameFunc = func(name string) string {
		parts := strings.Split(swag.ToFileName(name), "_")
		if goOtherReservedSuffixes[parts[len(parts)-1]] {
			parts = append(parts, "swagger")
		}
		return strings.Join(parts, "_")
	}
	opts.BaseImportFunc = func(tgt string) string {
		tgt = filepath.Clean(tgt)
		if tgt == "" {
			tgt = "."
		}
		tgtAbsPath, err := filepath.Abs(tgt)
		if err != nil {
			log.Fatalf("could not evaluate base import path with target \"%s\": %v", tgt, err)
		}
		var tgtAbsPathExtended string
		tgtAbsPathExtended, err = filepath.EvalSymlinks(tgtAbsPath)
		if err != nil {
			log.Fatalf("could not evaluate base import path with target \"%s\" (with symlink resolution): %v", tgtAbsPath, err)
		}
		gopath := os.Getenv("GOPATH")
		if gopath == "" {
			gopath = filepath.Join(os.Getenv("HOME"), "go")
		}
		var pth string
		for _, gp := range filepath.SplitList(gopath) {
			gopathExtended, err := filepath.EvalSymlinks(gp)
			if err != nil {
				log.Fatalln(err)
			}
			gopathExtended = filepath.Join(gopathExtended, "src")
			gp = filepath.Join(gp, "src")
			if ok, relativepath := checkPrefixAndFetchRelativePath(tgtAbsPath, gp); ok {
				pth = relativepath
				break
			}
			if ok, relativepath := checkPrefixAndFetchRelativePath(tgtAbsPath, gopathExtended); ok {
				pth = relativepath
				break
			}
			if ok, relativepath := checkPrefixAndFetchRelativePath(tgtAbsPathExtended, gopathExtended); ok {
				pth = relativepath
				break
			}
		}
		mod, goModuleAbsPath, err := tryResolveModule(tgtAbsPath)
		switch {
		case err != nil:
			log.Fatalf("Failed to resolve module using go.mod file: %s", err)
		case mod != "":
			relTgt := relPathToRelGoPath(goModuleAbsPath, tgtAbsPath)
			if !strings.HasSuffix(mod, relTgt) {
				return mod + relTgt
			}
			return mod
		}
		if pth == "" {
			log.Fatalln("target must reside inside a location in the $GOPATH/src or be a module")
		}
		return pth
	}
	opts.Init()
	return opts
}
func GoLangOpts ( ) * LanguageOpts { var goOtherReservedSuffixes = map [ string ] bool { " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , " " : true , } opts := new ( LanguageOpts ) opts . ReservedWords = [ ] string { " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , } opts . formatFunc = func ( ffn string , content [ ] byte ) ( [ ] byte , error ) { opts := new ( imports . Options ) opts . TabIndent = true opts . TabWidth = 2 opts . Fragment = true opts . Comments = true return imports . Process ( ffn , content , opts ) } opts . fileNameFunc = func ( name string ) string { parts := strings . Split ( swag . ToFileName ( name ) , " " ) if goOtherReservedSuffixes [ parts [ len ( parts ) - 1 ] ] { parts = append ( parts , " " ) } return strings . Join ( parts , " " ) } opts . BaseImportFunc = func ( tgt string ) string { tgt = filepath . Clean ( tgt ) if tgt == " " { tgt = " " } tgtAbsPath , err := filepath . Abs ( tgt ) if err != nil { log . Fatalf ( " \" \" " , tgt , err ) } var tgtAbsPathExtended string tgtAbsPathExtended , err = filepath . EvalSymlinks ( tgtAbsPath ) if err != nil { log . Fatalf ( " \" \" " , tgtAbsPath , err ) } gopath := os . Getenv ( " " ) if gopath == " " { gopath = filepath . Join ( os . Getenv ( " " ) , " " ) } var pth string for _ , gp := range filepath . SplitList ( gopath ) { gopathExtended , err := filepath . EvalSymlinks ( gp ) if err != nil { log . Fatalln ( err ) } gopathExtended = filepath . Join ( gopathExtended , " " ) gp = filepath . Join ( gp , " " ) if ok , relativepath := checkPrefixAndFetchRelativePath ( tgtAbsPath , gp ) ; ok { pth = relativepath break } if ok , relativepath := checkPrefixAndFetchRelativePath ( tgtAbsPath , gopathExtended ) ; ok { pth = relativepath break } if ok , relativepath := checkPrefixAndFetchRelativePath ( tgtAbsPathExtended , gopathExtended ) ; ok { pth = relativepath break } } mod , goModuleAbsPath , err := tryResolveModule ( tgtAbsPath ) switch { case err != nil : log . Fatalf ( " " , err ) case mod != " " : relTgt := relPathToRelGoPath ( goModuleAbsPath , tgtAbsPath ) if ! strings . HasSuffix ( mod , relTgt ) { return mod + relTgt } return mod } if pth == " " { log . Fatalln ( " " ) } return pth } opts . Init ( ) return opts }
GoLangOpts for rendering items as golang code
GoLangOpts for rendering items as golang code
// see: // https://golang.org/src/go/build/syslist.go // https://golang.org/doc/install/source#environment // goos // arch // other reserved suffixes // whenever a generated file name ends with a suffix // that is meaningful to go build, adds a "swagger" // suffix // file name ending with a reserved arch or os name // are appended an innocuous suffix "swagger" // On Windows, filepath.Abs("") behaves differently than on Unix. // Windows: yields an error, since Abs() does not know the volume. // UNIX: returns current working directory // EvalSymLinks also calls the Clean // At this stage we have expanded and unexpanded target path. GOPATH is fully expanded. // Expanded means symlink free. // We compare both types of targetpath<s> with gopath. // If any one of them coincides with gopath , it is imperative that // target path lies inside gopath. How? // 		- Case 1: Irrespective of symlinks paths coincide. Both non-expanded paths. // 		- Case 2: Symlink in target path points to location inside GOPATH. (Expanded Target Path) //    - Case 3: Symlink in target path points to directory outside GOPATH (Unexpanded target path) // Case 1: - Do nothing case. If non-expanded paths match just genrate base import path as if //				   there are no symlinks. // Case 2: - Symlink in target path points to location inside GOPATH. (Expanded Target Path) //					 First if will fail. Second if will succeed. // Case 3: - Symlink in target path points to directory outside GOPATH (Unexpanded target path) // 					 First if will succeed and break. //compares non expanded path for both // Compares non-expanded target path // Compares expanded target path.