Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
gop / usr / lib / gop / cmd / chore / gopbuiltingen / helper.gop
Size: Mime:
import (
	"go/ast"
)

func recvNameOf(recvType ast.Expr) string {
	var name string
	switch t := recvType.(type) {
	case *ast.Ident:
		name = t.Name[:1]
	case *ast.SelectorExpr:
		name = t.Sel.Name[:1]
	default:
		return "v"
	}
	return name.toLower
}

func importSpec(path string) *ast.ImportSpec {
	return {
		Path: {
			Value: path.quote,
		},
	}
}

func rmExargs(list []*ast.Field, ex *exargs) []*ast.Field {
	if ex == nil {
		return list
	}
	n := len(list)
	ret := make([]*ast.Field, n)
	for i, f <- list {
		ret[i] = f
	}
	exargs := ex.N
	for n > 0 && exargs > 0 {
		f := ret[n-1]
		if c := len(f.Names); c > exargs {
			f.Names = f.Names[:c-exargs]
			break
		} else {
			exargs -= c
		}
		n--
	}
	return ret[:n]
}

func toParams(params *ast.FieldList, at string) *ast.FieldList {
	if params == nil {
		return nil
	}
	list := make([]*ast.Field, len(params.List))
	for i, p <- params.List {
		typ := p.Type
		switch t := typ.(type) {
		case *ast.Ident:
			if t.isExported {
				typ = &ast.SelectorExpr{
					X:   ast.newIdent(at),
					Sel: t,
				}
			}
		case *ast.StarExpr:
			if x, ok := t.X.(*ast.Ident); ok && x.isExported {
				typ = &ast.StarExpr{
					X: &ast.SelectorExpr{
						X:   ast.newIdent(at),
						Sel: x,
					},
				}
			}
		}
		list[i] = {
			Doc:     p.Doc,
			Names:   p.Names,
			Type:    typ,
			Tag:     p.Tag,
			Comment: p.Comment,
		}
	}
	return {List: list}
}

func docForFunc(doc *ast.CommentGroup, oldName, newName string) *ast.CommentGroup {
	if doc == nil {
		return nil
	}
	list := make([]*ast.Comment, len(doc.List))
	for i, c := range doc.List {
		text := c.Text.replaceAll(oldName, newName)
		list[i] = {Text: text}
	}
	return {List: list}
}

func docForMethod(doc *ast.CommentGroup, at, oldName, newName, recvName string, ex *exargs) *ast.CommentGroup {
	if doc == nil {
		return nil
	}
	if ex == nil {
		return docForFunc(doc, oldName, newName)
	}
	list := make([]*ast.Comment, 0, len(doc.List)+2)
	list <- &ast.Comment{Text: "// ${newName} is equivalent to ${at}.${oldName}(${recvName}, ${ex.Code})"}
	list <- &ast.Comment{Text: "//"}
	for _, c := range doc.List {
		list <- &ast.Comment{Text: c.Text}
	}
	return {List: list}
}