func MatchFuncDecl()

in tool/util/ast.go [347:387]


func MatchFuncDecl(decl dst.Decl, function string, receiverType string) bool {
	Assert(isValidRegex(function), "invalid function name pattern")

	funcDecl, ok := decl.(*dst.FuncDecl)
	if !ok {
		return false
	}
	re := regexp.MustCompile("^" + function + "$") // strict match
	if !re.MatchString(funcDecl.Name.Name) {
		return false
	}
	if receiverType != "" {
		re = regexp.MustCompile("^" + receiverType + "$") // strict match
		if !HasReceiver(funcDecl) {
			return re.MatchString("")
		}
		switch recvTypeExpr := funcDecl.Recv.List[0].Type.(type) {
		case *dst.StarExpr:
			if _, ok := recvTypeExpr.X.(*dst.Ident); !ok {
				// This is a generic type, we don't support it yet
				return false
			}
			t := "*" + recvTypeExpr.X.(*dst.Ident).Name
			return re.MatchString(t)
		case *dst.Ident:
			t := recvTypeExpr.Name
			return re.MatchString(t)
		case *dst.IndexExpr:
			// This is a generic type, we don't support it yet
			return false
		default:
			msg := fmt.Sprintf("unexpected receiver type: %T", recvTypeExpr)
			UnimplementedT(msg)
		}
	} else {
		if HasReceiver(funcDecl) {
			return false
		}
	}
	return true
}