当前位置: 首页>>代码示例>>Golang>>正文


Golang ImportSpec.Name方法代码示例

本文整理汇总了Golang中go/ast.ImportSpec.Name方法的典型用法代码示例。如果您正苦于以下问题:Golang ImportSpec.Name方法的具体用法?Golang ImportSpec.Name怎么用?Golang ImportSpec.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在go/ast.ImportSpec的用法示例。


在下文中一共展示了ImportSpec.Name方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parseImportSpec

func (p *parser) parseImportSpec(n *parse.Node) *ast.ImportSpec {
	spec := ast.ImportSpec{}
	if n.Child(0).Is(importPath) {
		spec.Path = p.parseBasicLit(n.Child(0))
	} else {
		name := n.Child(0)
		switch name.Rule() {
		case identifier:
			spec.Name = p.parseIdent(name)
		case term("."):
			spec.Name = &ast.Ident{
				NamePos: token.Pos(name.Pos()),
				Name:    ".",
			}
		}
		spec.Path = p.parseBasicLit(n.Child(1))

	}
	return &spec
}
开发者ID:h12w,项目名称:gombi,代码行数:20,代码来源:parser.go

示例2: fixGoExact

func fixGoExact(f *ast.File) bool {
	// This one is harder because the import name changes.
	// First find the import spec.
	var importSpec *ast.ImportSpec
	walk(f, func(n interface{}) {
		if importSpec != nil {
			return
		}
		spec, ok := n.(*ast.ImportSpec)
		if !ok {
			return
		}
		path, err := strconv.Unquote(spec.Path.Value)
		if err != nil {
			return
		}
		if path == "golang.org/x/tools/go/exact" {
			importSpec = spec
		}

	})
	if importSpec == nil {
		return false
	}

	// We are about to rename exact.* to constant.*, but constant is a common
	// name. See if it will conflict. This is a hack but it is effective.
	exists := renameTop(f, "constant", "constant")
	suffix := ""
	if exists {
		suffix = "_"
	}
	// Now we need to rename all the uses of the import. RewriteImport
	// affects renameTop, but not vice versa, so do them in this order.
	renameTop(f, "exact", "constant"+suffix)
	rewriteImport(f, "golang.org/x/tools/go/exact", "go/constant")
	// renameTop will also rewrite the imported package name. Fix that;
	// we know it should be missing.
	importSpec.Name = nil
	return true
}
开发者ID:achanda,项目名称:go,代码行数:41,代码来源:gotypes.go

示例3: main

func main() {
	flag.Parse()

	src, err := ioutil.ReadFile(*in)
	if err != nil {
		log.Fatal(err)
	}

	fset := token.NewFileSet()
	f, _ := parser.ParseFile(fset, "", src, parser.AllErrors)

	// Find where "crypto/aes" is imported.
	var importSpec *ast.ImportSpec

	for _, imp := range f.Imports {
		if imp.Path.Value == target {
			importSpec = imp
			break
		}
	}

	if importSpec == nil {
		log.Fatalf("File doesn't import %v!", target)
	}

	// Name the import explicitly if it's not already named. Change the import path to a white-box construction.
	if importSpec.Name == nil {
		importSpec.Name = &ast.Ident{0, "aes", nil}
	} else if importSpec.Name.Name == "." {
		log.Fatalf("Can't transform a file's encryption keys if %v is imported into the local scope!", target)
	}

	importSpec.Path.Value = replacement

	// Find everywhere we initialize a block cipher with an explicit key and replace it with a white-box.
	ast.Inspect(f, func(n ast.Node) bool {
		switch n.(type) {
		case *ast.CallExpr:
			callExpr := n.(*ast.CallExpr)

			if IsCallToEncrypt(importSpec.Name.Name, callExpr) {
				key, ok := ExtractKey(callExpr.Args[0]) // aes.NewCipher has only one argument.

				if ok {
					TransformCallToEncrypt(callExpr, key)
					return false
				} else {
					log.Printf("Found encryption call at %v, but couldn't extract the key!", fset.Position(n.Pos()))
					return true
				}
			}
		}

		return true
	})

	dst, err := os.Create(*out)
	if err != nil {
		log.Fatal(err)
	}

	printer.Fprint(dst, fset, f)
}
开发者ID:gaurav1981,项目名称:AES,代码行数:63,代码来源:transform.go


注:本文中的go/ast.ImportSpec.Name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。