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

♻️ Refactor: Use func in app.go #2597

Closed
wants to merge 1 commit into from

Conversation

LimJiAn
Copy link
Contributor

@LimJiAn LimJiAn commented Aug 24, 2023

Description

Hello!
How about doing this without using the variable prefix?
If it changes, don't have to use the if statement.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

Remove if statement
Reduce Scope of Variable
@Skyenought
Copy link
Member

@LimJiAn
You've changed the logic of Use.

For example, I did a log print when I registered

// your change
func (app *App) AfterChangeUse(args ...interface{}) Router {
	var prefixes []string
	var handlers []Handler

	for _, arg := range args {
		switch arg := arg.(type) {
		case string:
			prefixes = append(prefixes, arg)
		case []string:
			prefixes = append(prefixes, arg...)
		case Handler:
			handlers = append(handlers, arg)
		default:
			panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
		}
	}
	for _, prefix := range prefixes {
                //
		log.Infof("AfterChange, prefix: %s, handlers: %v", prefix, handlers)
		app.register(methodUse, prefix, nil, handlers...)
	}

	return app
}

func (app *App) Use(args ...interface{}) Router {
	var prefix string
	var prefixes []string
	var handlers []Handler

	for i := 0; i < len(args); i++ {
		switch arg := args[i].(type) {
		case string:
			prefix = arg
		case []string:
			prefixes = arg
		case Handler:
			handlers = append(handlers, arg)
		default:
			panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
		}
	}

	if len(prefixes) == 0 {
		prefixes = append(prefixes, prefix)
	}
	for _, prefix := range prefixes {
		log.Infof("prefix: %s, handlers: %v", prefix, handlers)
		app.register(methodUse, prefix, nil, handlers...)
	}

	return app
}

Test case:

func main() {
	app := fiber.New()
	app1 := fiber.New()
	app.AfterChangeUse("api", "test", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
	app1.Use("api", "test", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
}

result:

2023/08/24 21:16:15.857571 app.go:702: [Info] AfterChange, prefix: api, handlers: [0x614d40]
2023/08/24 21:16:15.862337 app.go:702: [Info] AfterChange, prefix: test, handlers: [0x614d40]
2023/08/24 21:16:15.862374 app.go:731: [Info] prefix: test, handlers: [0x614da0]

Your change caused the route to be registered twice.

@gaby
Copy link
Member

gaby commented Aug 24, 2023

@LimJiAn You've changed the logic of Use.

For example, I did a log print when I registered

// your change
func (app *App) AfterChangeUse(args ...interface{}) Router {
	var prefixes []string
	var handlers []Handler

	for _, arg := range args {
		switch arg := arg.(type) {
		case string:
			prefixes = append(prefixes, arg)
		case []string:
			prefixes = append(prefixes, arg...)
		case Handler:
			handlers = append(handlers, arg)
		default:
			panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
		}
	}
	for _, prefix := range prefixes {
                //
		log.Infof("AfterChange, prefix: %s, handlers: %v", prefix, handlers)
		app.register(methodUse, prefix, nil, handlers...)
	}

	return app
}

func (app *App) Use(args ...interface{}) Router {
	var prefix string
	var prefixes []string
	var handlers []Handler

	for i := 0; i < len(args); i++ {
		switch arg := args[i].(type) {
		case string:
			prefix = arg
		case []string:
			prefixes = arg
		case Handler:
			handlers = append(handlers, arg)
		default:
			panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
		}
	}

	if len(prefixes) == 0 {
		prefixes = append(prefixes, prefix)
	}
	for _, prefix := range prefixes {
		log.Infof("prefix: %s, handlers: %v", prefix, handlers)
		app.register(methodUse, prefix, nil, handlers...)
	}

	return app
}

Test case:

func main() {
	app := fiber.New()
	app1 := fiber.New()
	app.AfterChangeUse("api", "test", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
	app1.Use("api", "test", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
}

result:

2023/08/24 21:16:15.857571 app.go:702: [Info] AfterChange, prefix: api, handlers: [0x614d40]
2023/08/24 21:16:15.862337 app.go:702: [Info] AfterChange, prefix: test, handlers: [0x614d40]
2023/08/24 21:16:15.862374 app.go:731: [Info] prefix: test, handlers: [0x614da0]

Your change caused the route to be registered twice.

That may explain why the tests are failing

@LimJiAn
Copy link
Contributor Author

LimJiAn commented Aug 24, 2023

@Skyenought @gaby
I got it. Thank you for the comments.
Thank you for the test code as well.

@LimJiAn LimJiAn closed this Aug 24, 2023
@LimJiAn LimJiAn deleted the refactor_func branch August 24, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants