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

Allocate parent controllers just once #41

Open
ghost opened this issue Feb 15, 2016 · 4 comments
Open

Allocate parent controllers just once #41

ghost opened this issue Feb 15, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 15, 2016

Intro

There is a controller with its special action:

type GrandParent struct {
    Context map[string]interface{}
}

func (c *GrandParent) Before() http.Handler {
    println("This is GrandParent")
    c.Context = map[string]interface{}{}
}

The controller above is embedded by a number of other controllers.

type Parent1 struct {
    *GrandParent
}
type Parent2 struct {
    *GrandParent
}

Those controllers are embedded by our main controller.

type Child struct {
    *Parent1
    *Parent2
}

func (c *Child) Index() http.Handler {
}

Problem

The problem is the GrandParent controller will be allocated twice, meaning c.Parent1.GrandParent != c.Parent2.GrandParent. The output of the GrandParent's Before will be:

This is GrandParent
This is GrandParent

As a result, we cannot share a context between controllers of different levels of embedding.

Use-case

This is the problem I've faced trying to implement #39. There is controllers/errors controller that is responsible for rendering error pages. We embed it in our main controller and mount its actions to * /errors:

type Controllers struct {
    errors.Errors `@route:"/errors"`
}

Now if we want to pass some value to the actions of that Errors controller it doesn't work:

func (c *Controllers) Before() http.Handler {
    c.Context["someKey"] = "someValue"
}

Because, Context of Controllers and Context of Errors are different instances. But they must be pointers to the same object.

Expectations

A single allocation per object per request. Special (Before and After) actions must be called just once: Parents must have priority over their Children.

@ghost ghost self-assigned this Feb 15, 2016
@ghost ghost added this to the v0.0.2 milestone Feb 15, 2016
@xpbliss
Copy link

xpbliss commented Feb 17, 2016

solve?
How about the autoform?

@ghost
Copy link
Author

ghost commented Feb 21, 2016

This is ready. Right after I finish refactoring controllers I'll push all the commits (there are a lot of them). Autoform is still a high priority.

@ghost ghost mentioned this issue Apr 9, 2016
@ghost
Copy link
Author

ghost commented May 15, 2016

I'm thinking over this one as well. Should we allocate controllers? Isn't it too much magic? Especially when it comes to these single allocation rules. Can we come up with another solution for sharing state between actions of different controllers?

@xpbliss
Copy link

xpbliss commented May 20, 2016

Glad to see the goal is still activate!

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

No branches or pull requests

1 participant