本文整理汇总了Golang中golang.org/x/tools/imports.Process函数的典型用法代码示例。如果您正苦于以下问题:Golang Process函数的具体用法?Golang Process怎么用?Golang Process使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Process函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestBindings
// Tests that packages generated by the binder can be successfully compiled and
// the requested tester run against it.
func TestBindings(t *testing.T) {
// Skip the test if no Go command can be found
gocmd := runtime.GOROOT() + "/bin/go"
if !common.FileExist(gocmd) {
t.Skip("go sdk not found for testing")
}
// Skip the test if the go-ethereum sources are symlinked (https://github.com/golang/go/issues/14845)
linkTestCode := fmt.Sprintf("package linktest\nfunc CheckSymlinks(){\nfmt.Println(backends.NewNilBackend())\n}")
linkTestDeps, err := imports.Process("", []byte(linkTestCode), nil)
if err != nil {
t.Fatalf("failed check for goimports symlink bug: %v", err)
}
if !strings.Contains(string(linkTestDeps), "go-ethereum") {
t.Skip("symlinked environment doesn't support bind (https://github.com/golang/go/issues/14845)")
}
// Create a temporary workspace for the test suite
ws, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("failed to create temporary workspace: %v", err)
}
defer os.RemoveAll(ws)
pkg := filepath.Join(ws, "bindtest")
if err = os.MkdirAll(pkg, 0700); err != nil {
t.Fatalf("failed to create package: %v", err)
}
// Generate the test suite for all the contracts
for i, tt := range bindTests {
// Generate the binding and create a Go source file in the workspace
bind, err := Bind([]string{tt.name}, []string{tt.abi}, []string{tt.bytecode}, "bindtest")
if err != nil {
t.Fatalf("test %d: failed to generate binding: %v", i, err)
}
if err = ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil {
t.Fatalf("test %d: failed to write binding: %v", i, err)
}
// Generate the test file with the injected test code
code := fmt.Sprintf("package bindtest\nimport \"testing\"\nfunc Test%s(t *testing.T){\n%s\n}", tt.name, tt.tester)
blob, err := imports.Process("", []byte(code), nil)
if err != nil {
t.Fatalf("test %d: failed to generate tests: %v", i, err)
}
if err := ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), blob, 0600); err != nil {
t.Fatalf("test %d: failed to write tests: %v", i, err)
}
}
// Test the entire package and report any failures
cmd := exec.Command(gocmd, "test", "-v")
cmd.Dir = pkg
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to run binding test: %v\n%s", err, out)
}
}
开发者ID:Codzart,项目名称:go-ethereum,代码行数:55,代码来源:bind_test.go
示例2: goimports
func goimports(src []byte) (string, string) {
out, err := imports.Process("", src, nil)
if err != nil {
return "", fmt.Sprintf("%v", err)
}
return string(out), ""
}
开发者ID:reusee,项目名称:my-editor-gl,代码行数:7,代码来源:golang.go
示例3: TranspileFile
func TranspileFile(goFilename, phpFilename, phpStr string, gosrc io.Writer) error {
parser := parser.NewParser()
file, err := parser.Parse(phpFilename, phpStr)
if err != nil {
return fmt.Errorf("found errors while parsing %s: %s", phpFilename, err)
}
tg := Togo{currentScope: parser.FileSet.Scope}
nodes := []goast.Node{}
for _, node := range tg.beginScope(tg.currentScope) {
nodes = append(nodes, node)
}
for _, phpNode := range file.Nodes {
nodes = append(nodes, tg.ToGoStmt(phpNode.(phpast.Statement)))
}
buf := &bytes.Buffer{}
if err = format.Node(buf, token.NewFileSet(), File(phpFilename[:len(phpFilename)-4], nodes...)); err != nil {
return fmt.Errorf("error while formatting %s: %s", phpFilename, err)
}
imported, err := imports.Process(goFilename, buf.Bytes(), &imports.Options{AllErrors: true, Comments: true, TabIndent: true, TabWidth: 8})
if err != nil {
return fmt.Errorf("error while getting imports for %s: %s", phpFilename, err)
}
_, err = gosrc.Write(imported)
return err
}
开发者ID:henrylee2cn,项目名称:php,代码行数:31,代码来源:togo.go
示例4: fmtHandler
func fmtHandler(w http.ResponseWriter, r *http.Request) {
var (
in = []byte(r.FormValue("body"))
out []byte
err error
)
if len(in) > maxSnippetSize {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Compile server error.")
return
}
if r.FormValue("imports") != "" {
out, err = imports.Process("prog.go", in, nil)
} else {
out, err = format.Source(in)
}
var resp fmtResponse
if err != nil {
resp.Error = err.Error()
} else {
resp.Body = string(out)
}
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Origin", *flagAllowOriginHeader)
json.NewEncoder(w).Encode(resp)
}
开发者ID:jostyee,项目名称:go-playground,代码行数:31,代码来源:fmt.go
示例5: validFormat
func validFormat(repoPath string, prFiles []*octokat.PullRequestFile) (formatted bool, files []string, err error) {
repo, err := gitreader.OpenRepo(repoPath)
if err != nil {
return false, files, err
}
for _, file := range prFiles {
name := file.FileName
if strings.HasSuffix(name, ".go") && !strings.HasPrefix(name, "vendor/") {
blob, err := repo.CatFile(file.Sha, name)
if err != nil {
return false, files, err
}
src, err := blob.Bytes()
if err != nil {
return false, files, err
}
res, err := imports.Process(name, src, nil)
if err != nil {
return false, files, err
}
if !bytes.Equal(src, res) {
files = append(files, name)
formatted = false
}
}
}
return len(files) == 0, files, err
}
开发者ID:pombredanne,项目名称:gh-patch-parser,代码行数:34,代码来源:format.go
示例6: convertFile
func convertFile(dest_path, source_path string, fixImports bool, types ...string) error {
buf, err := genlib.Generate(source_path, types...)
if err != nil {
return err
}
if fixImports {
buf, err = imports.Process(path.Base(source_path), buf, &imports.Options{
TabWidth: 8,
TabIndent: true,
Comments: true,
Fragment: true,
AllErrors: false,
})
if err != nil {
return err
}
}
f, err := os.Create(dest_path)
if err != nil {
return err
}
_, err = io.Copy(f, bytes.NewBuffer(buf))
if err != nil {
return err
}
return nil
}
开发者ID:james-lawrence,项目名称:gengen,代码行数:31,代码来源:main.go
示例7: assertOutput
func assertOutput(inPath, lang, goldenPath string, t *testing.T) {
assert := assert.New(t)
emptyDS := datas.NewDataStore(chunks.NewMemoryStore()) // Will be DataStore containing imports
depsDir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(depsDir)
inFile, err := os.Open(inPath)
assert.NoError(err)
defer inFile.Close()
goldenFile, err := os.Open(goldenPath)
assert.NoError(err)
defer goldenFile.Close()
goldenBytes, err := ioutil.ReadAll(goldenFile)
d.Chk.NoError(err)
var buf bytes.Buffer
pkg := pkg.ParseNomDL("gen", inFile, filepath.Dir(inPath), emptyDS)
written := map[string]bool{}
gen := newCodeGen(&buf, getBareFileName(inPath), lang, written, depsMap{}, pkg)
gen.WritePackage()
bs := buf.Bytes()
if lang == "go" {
bs, err = imports.Process("", bs, nil)
d.Chk.NoError(err)
}
assert.Equal(string(goldenBytes), string(bs), "%s did not generate the same string", inPath)
}
开发者ID:arv,项目名称:noms-old,代码行数:32,代码来源:codegen_test.go
示例8: FindInterface
// FindInterface returns the import path and identifier of an interface.
// For example, given "http.ResponseWriter", FindInterface returns
// "net/http", "ResponseWriter".
func FindInterface(iface string) (path string, pkg string, id string, err error) {
if len(strings.Fields(iface)) != 1 {
return "", "", "", fmt.Errorf("couldn't parse interface: %s", iface)
}
// Let goimports do the heavy lifting.
src := []byte("package hack\n" + "var i " + iface)
imp, err := imports.Process("", src, nil)
if err != nil {
return "", "", "", fmt.Errorf("couldn't parse interface: %s", iface)
}
// imp should now contain an appropriate import.
// Parse out the import and the identifier.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", imp, 0)
if err != nil {
panic(err)
}
if len(f.Imports) == 0 {
return "", "", "", fmt.Errorf("unrecognized interface: %s", iface)
}
raw := f.Imports[0].Path.Value // "io"
path, err = strconv.Unquote(raw) // io
if err != nil {
panic(err)
}
decl := f.Decls[1].(*ast.GenDecl) // var i io.Reader
spec := decl.Specs[0].(*ast.ValueSpec) // i io.Reader
sel := spec.Type.(*ast.SelectorExpr) // io.Reader
pkg = sel.X.(*ast.Ident).Name // io
id = sel.Sel.Name // Reader
return path, pkg, id, nil
}
开发者ID:lysu,项目名称:thriftpool-go,代码行数:37,代码来源:interfacefinder.go
示例9: format
func format(file string, data []byte) error {
out, err := imports.Process(file, data, nil)
if err != nil {
return err
}
return ioutil.WriteFile(file, out, 0600)
}
开发者ID:kgrz,项目名称:msgp,代码行数:7,代码来源:print.go
示例10: main
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 2 {
usage()
}
args := flag.Args()
n := len(args)
extras := args[:n-2]
a := args[n-2:]
inter, typeName := a[0], a[1]
opts := GenOpts{Inter: inter, NoGoImports: !*goimports, NoNamedReturnValues: !*named, Extra: extras}
if !*existing {
pi, err := parse(typeName)
check(err)
opts.ImplName = pi.ptr + pi.name
opts.PkgName = pi.pkg
} else {
opts.Existing = typeName
if !strings.HasSuffix(typeName, "}") && !strings.HasSuffix(typeName, ")") {
// We need to create a instance of this type.
// Let's it's a struct/slice.
opts.Existing += "{}"
}
}
buf := new(bytes.Buffer)
check(tm.Execute(buf, opts))
src, err := imports.Process("", buf.Bytes(), nil)
check(err, "imports:", buf.String())
check(run(src), "run:", string(src))
}
开发者ID:zhaojkun,项目名称:goimpl,代码行数:34,代码来源:goimpl.go
示例11: genTypesOnlyAPI
func genTypesOnlyAPI(file string) error {
apiGen := &api.API{
NoRemoveUnusedShapes: true,
NoRenameToplevelShapes: true,
}
apiGen.Attach(file)
apiGen.Setup()
// to reset imports so that timestamp has an entry in the map.
apiGen.APIGoCode()
var buf bytes.Buffer
err := typesOnlyTplAPI.Execute(&buf, apiGen)
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(apiGen.PackageName(), 0755)
// Fix imports.
codeWithImports, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", apiGen.PackageName(), code)), nil)
if err != nil {
fmt.Println(err)
return err
}
outFile := filepath.Join(apiGen.PackageName(), "api.go")
err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, codeWithImports)), 0644)
if err != nil {
return err
}
return nil
}
开发者ID:bmanas,项目名称:amazon-ecs-agent,代码行数:33,代码来源:awssdk.go
示例12: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
var funcOut io.Writer
if *functionDest == "-" {
funcOut = os.Stdout
} else {
file, err := os.Create(*functionDest)
if err != nil {
glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
}
defer file.Close()
funcOut = file
}
data := new(bytes.Buffer)
group, version := path.Split(*groupVersion)
group = strings.TrimRight(group, "/")
_, err := data.WriteString(fmt.Sprintf("package %v\n", version))
if err != nil {
glog.Fatalf("error writing package line: %v", err)
}
versionPath := path.Join(pkgBase, group, version)
generator := pkg_runtime.NewConversionGenerator(api.Scheme.Raw(), versionPath)
apiShort := generator.AddImport(path.Join(pkgBase, "api"))
generator.AddImport(path.Join(pkgBase, "api/resource"))
// TODO(wojtek-t): Change the overwrites to a flag.
generator.OverwritePackage(version, "")
for _, knownType := range api.Scheme.KnownTypes(version) {
if !strings.HasPrefix(knownType.PkgPath(), versionPath) {
continue
}
if err := generator.GenerateConversionsForType(version, knownType); err != nil {
glog.Errorf("error while generating conversion functions for %v: %v", knownType, err)
}
}
generator.RepackImports(util.NewStringSet())
if err := generator.WriteImports(data); err != nil {
glog.Fatalf("error while writing imports: %v", err)
}
if err := generator.WriteConversionFunctions(data); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
if err := generator.RegisterConversionFunctions(data, fmt.Sprintf("%s.Scheme", apiShort)); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
b, err := imports.Process("", data.Bytes(), nil)
if err != nil {
glog.Fatalf("error while update imports: %v", err)
}
if _, err := funcOut.Write(b); err != nil {
glog.Fatalf("error while writing out the resulting file: %v", err)
}
}
开发者ID:ngbinh,项目名称:kubernetes,代码行数:59,代码来源:conversion.go
示例13: WriteFormattedFile
// WriteFormattedFile formats the code with goimports and writes the result to
// the given file, if file doesnt exists, it creates it
func WriteFormattedFile(fileName string, model []byte) error {
dest, err := imports.Process("", model, nil)
if err != nil {
return err
}
return Write(fileName, dest)
}
开发者ID:Zenithar,项目名称:gene,代码行数:10,代码来源:filewriter.go
示例14: Generate
func (gf *GoFile) Generate() ([]byte, error) {
var buf bytes.Buffer
if err := printer.Fprint(&buf, gf.FSet, gf.AstFile); err != nil {
return nil, err
}
return imports.Process(gf.FileName, buf.Bytes(), nil)
}
开发者ID:tcnksm,项目名称:gotests,代码行数:8,代码来源:go.go
示例15: main
func main() {
iface := "http.Handler"
src := "package hack; var i " + iface // HL
fmt.Println(src, "\n---")
imp, _ := imports.Process("", []byte(src), nil) // HL
// ignoring errors throughout this presentation
fmt.Println(string(imp))
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:9,代码来源:importpath.go
示例16: formatGoFile
func formatGoFile(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)
}
开发者ID:jacobxk,项目名称:go-swagger,代码行数:9,代码来源:shared.go
示例17: WriteAll
// WriteAll writes the generated code for all Types and TypeWriters in the App to respective files.
func (a *App) WriteAll() ([]string, error) {
var written []string
// one buffer for each file, keyed by file name
buffers := make(map[string]*bytes.Buffer)
// write the generated code for each Type & TypeWriter into memory
for _, p := range a.Packages {
for _, t := range p.Types {
for _, tw := range a.TypeWriters {
var b bytes.Buffer
n, err := write(&b, a, p, t, tw)
if err != nil {
return written, err
}
// don't generate a file if no bytes were written by WriteHeader or WriteBody
if n == 0 {
continue
}
// append _test to file name if the source type is in a _test.go file
f := strings.ToLower(fmt.Sprintf("%s_%s%s.go", t.Name, tw.Name(), t.test))
buffers[f] = &b
}
}
}
// validate generated ast's before committing to files
for f, b := range buffers {
if _, err := parser.ParseFile(token.NewFileSet(), f, b.String(), 0); err != nil {
// TODO: prompt to write (ignored) _file on error? parsing errors are meaningless without.
return written, err
}
}
// format, remove unused imports, and commit to files
for f, b := range buffers {
src, err := imports.Process(f, b.Bytes(), nil)
// shouldn't be an error if the ast parsing above succeeded
if err != nil {
return written, err
}
if err := writeFile(f, src); err != nil {
return written, err
}
written = append(written, f)
}
return written, nil
}
开发者ID:sparkymat,项目名称:typewriter,代码行数:57,代码来源:app.go
示例18: WriteTo
func WriteTo(w io.Writer, b *bytes.Buffer) error {
opt := &imports.Options{Comments: true}
res, err := imports.Process("mock.go", b.Bytes(), opt)
if err != nil {
return err
}
_, err = w.Write(res)
return err
}
开发者ID:piotrkowalczuk,项目名称:mockery,代码行数:10,代码来源:generator.go
示例19: Write
func (g *Generator) Write(w io.Writer) error {
opt := &imports.Options{Comments: true}
res, err := imports.Process("mock.go", g.buf.Bytes(), opt)
if err != nil {
return err
}
w.Write(res)
return nil
}
开发者ID:itsarbit,项目名称:mockery,代码行数:10,代码来源:generator.go
示例20: prettyfy
func prettyfy(input []byte, wr io.Writer) error {
output, err := imports.Process("storable.go", input, nil)
if err != nil {
printDocumentWithNumbers(string(input))
return err
}
_, err = wr.Write(output)
return err
}
开发者ID:vsouza,项目名称:storable,代码行数:10,代码来源:template.go
注:本文中的golang.org/x/tools/imports.Process函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论