Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There seem to be a bug #8

Open
potatoxz14 opened this issue May 5, 2022 · 1 comment
Open

There seem to be a bug #8

potatoxz14 opened this issue May 5, 2022 · 1 comment

Comments

@potatoxz14
Copy link

no matter which one I choose between method or invoke,the 'login' is always to be false,even I changed 'return false' to 'return true' in the function login.

@potatoxz14
Copy link
Author

the code is as following :

package main

import (
"fmt"

"github.com/gogap/aop"

)

type Auth struct {
}

func (p *Auth) Login(userName, password string) bool {
fmt.Println("@Login", userName, password)
if userName == "zeal" && password == "gogap" {
return true
}
return false
}

// use join point to get Args from real method
func (p *Auth) Before(jp aop.JoinPointer) {
username := ""
jp.Args().MapTo(func(u, p string) {
username = u
})

fmt.Printf("Before Login: %s\n", username)

}

// the args is same as Login
func (p *Auth) After(username, password string) {
fmt.Printf("After Login: %s %s\n", username, password)
}

// use join point to around the real func of login
func (p *Auth) Around(pjp aop.ProceedingJoinPointer) {
fmt.Println("@begin Around")

ret := pjp.Proceed("zeal", "gogap")
ret.MapTo(func(loginResult bool) {
	fmt.Println("@Proceed Result is", loginResult)
})

fmt.Println("@End Around")

}

type Foo struct {
}

// @AfterReturning, the method could have args of aop.Result,
// it will get the result from real func return values
func (p *Foo) Bar(result aop.Result) {
result.MapTo(func(v bool) {
fmt.Println("Bar Bar Bar .... Result is:", v)
})
}

func main() {
beanFactory := aop.NewClassicBeanFactory()
beanFactory.RegisterBean("auth", new(Auth))
beanFactory.RegisterBean("foo", new(Foo))

aspect := aop.NewAspect("aspect_1", "auth")
aspect.SetBeanFactory(beanFactory)

aspectFoo := aop.NewAspect("aspect_2", "foo")
aspectFoo.SetBeanFactory(beanFactory)

pointcut := aop.NewPointcut("pointcut_1").Execution(`Login()`)
pointcut.Execution(`Login()`)

aspect.AddPointcut(pointcut)
aspectFoo.AddPointcut(pointcut)

aspect.AddAdvice(&aop.Advice{Ordering: aop.Before, Method: "Before", PointcutRefID: "pointcut_1"})
aspect.AddAdvice(&aop.Advice{Ordering: aop.After, Method: "After", PointcutRefID: "pointcut_1"})
aspect.AddAdvice(&aop.Advice{Ordering: aop.Around, Method: "Around", PointcutRefID: "pointcut_1"})
aspectFoo.AddAdvice(&aop.Advice{Ordering: aop.AfterReturning, Method: "Bar", PointcutRefID: "pointcut_1"})

gogapAop := aop.NewAOP()
gogapAop.SetBeanFactory(beanFactory)
gogapAop.AddAspect(aspect)
gogapAop.AddAspect(aspectFoo)

var err error
var proxy *aop.Proxy

// Get proxy
if proxy, err = gogapAop.GetProxy("auth"); err != nil {
	fmt.Println("get proxy failed", err)
	return
}

// start trace for debug
aop.StartTrace()

fmt.Println("==========Func Type Assertion==========")

login := proxy.Method(new(Auth).Login).(func(string, string) bool)("zeal", "gogap")

fmt.Println("Login result:", login)

fmt.Println("================Invoke=================")

if err = proxy.Invoke(new(Auth).Login, "zeal", "gogap").End(
	func(result bool) {
		login = result
	}); err != nil {
	fmt.Println("invoke proxy func error", err)
} else {
	fmt.Println("Login result:", login)
}

t, _ := aop.StopTrace()

// print trace result
for _, item := range t.Items() {
	fmt.Println(item.ID, item.InvokeID, item.BeanRefID, item.Pointcut, item.Method)
}

}
but the result is confusing,the Login is false,but the Proceed Result is true
==========Func Type Assertion==========
Before Login: zeal
@begin Around
@Login zeal gogap
@proceed Result is true
@EnD Around
After Login: zeal gogap
Login result: false
================Invoke=================
Before Login: zeal
@begin Around
@Login zeal gogap
@proceed Result is true
@EnD Around
After Login: zeal gogap
Login result: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant