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

🚀 v3 Request: fiber.Ctx type of interface #1824

Closed
balcieren opened this issue Mar 20, 2022 · 6 comments · Fixed by #1928
Closed

🚀 v3 Request: fiber.Ctx type of interface #1824

balcieren opened this issue Mar 20, 2022 · 6 comments · Fixed by #1928
Assignees
Milestone

Comments

@balcieren
Copy link
Contributor

balcieren commented Mar 20, 2022

If fiber.Ctx will be interface , we can customize it.

Example

type CustomCtx struct {
	fiber.Ctx
}

func (c *CustomCtx) Foo() {
	println("foo")
}

func (c *CustomCtx) Bar() {
	println("bar")
}

app.Get("/", func(c CustomCtx) error {
	c.Foo()
	c.Bar()
	return c.JSON("")
})
@balcieren balcieren changed the title 🚀 fiber.Ctx type of interface{} 🚀 v3 feature: fiber.Ctx type of interface{} Mar 20, 2022
@balcieren balcieren changed the title 🚀 v3 feature: fiber.Ctx type of interface{} 🚀 v3 Request: fiber.Ctx type of interface{} Mar 20, 2022
@ReneWerner87 ReneWerner87 added this to the v3 milestone Mar 20, 2022
@efectn efectn changed the title 🚀 v3 Request: fiber.Ctx type of interface{} 🚀 v3 Request: fiber.Ctx type of interface Mar 20, 2022
@efectn
Copy link
Member

efectn commented Mar 20, 2022

Similar to:

@efectn efectn linked a pull request Jun 2, 2022 that will close this issue
@efectn efectn added this to v3 Jun 5, 2022
@efectn efectn moved this to In Progress in v3 Jun 5, 2022
@efectn efectn removed this from v3 Jun 5, 2022
@efectn efectn added this to v3 Jun 5, 2022
@efectn efectn moved this to In Progress in v3 Jun 5, 2022
@efectn efectn self-assigned this Jun 5, 2022
@trim21
Copy link
Contributor

trim21 commented Jun 15, 2022

I have a question about this, what stop users doing the same thing if Ctx is a struct?

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

type CustomCtx struct {
	*fiber.Ctx
}

func (cc CustomCtx) Foo() {
	fmt.Println("foo")
}

func (cc CustomCtx) Boo() {
	fmt.Println("bar")
}

func main() {
	app := fiber.New(fiber.Config{})

	app.Get("/", func(c *fiber.Ctx) error {
		cc := CustomCtx{c}

		cc.Foo()
		cc.Boo()

		return cc.JSON("")
	})
}

@efectn
Copy link
Member

efectn commented Jun 17, 2022

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

type CustomCtx struct {
	*fiber.Ctx
}

func (cc CustomCtx) Foo() {
	fmt.Println("foo")
}

func (cc CustomCtx) Boo() {
	fmt.Println("bar")
}

func main() {
	app := fiber.New(fiber.Config{})

	app.Get("/", func(c *fiber.Ctx) error {
		cc := CustomCtx{c}

		cc.Foo()
		cc.Boo()

		return cc.JSON("")
	})
}

Yes it works. However, interfaces give us more extendable and readability. I support using interface as Ctx type because of its.

@trim21
Copy link
Contributor

trim21 commented Jun 17, 2022

It doesn't have any difference with your example, did I miss something?

Is there a more complex example?

@efectn
Copy link
Member

efectn commented Jun 19, 2022

It doesn't have any difference with your example, did I miss something?

Is there a more complex example?

If context is an interface, we can use type casting:

cc := c.(*CustomCtx)

But usages like return cc.SendString("") gives an segmentation error. I should find a solution for this problem

@efectn
Copy link
Member

efectn commented Jun 29, 2022

It doesn't have any difference with your example, did I miss something?

Is there a more complex example?

Check #1928 (comment)
i made some changes.

@efectn efectn closed this as completed Jul 13, 2022
Repository owner moved this from In Progress to Done in v3 Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants