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

[FEATURE] When using iris and gRPC, if a struct contains a parent struct, the parent struct will be handled and parsed. #2103

Closed
AliangCoder opened this issue Mar 16, 2023 · 3 comments

Comments

@AliangCoder
Copy link

Describe the bug
When using iris and gRPC, if a struct contains a parent struct, the parent struct will be handled and parsed.
To Reproduce
Steps to reproduce the behavior:

type myController struct {
	// Ctx iris.Context
	BaseController
	SingletonDependency service
}

When I use it

ctrl := &myController{}
// Register gRPC server.
grpcServer := grpc.NewServer()
pb.RegisterGreeterServer(grpcServer, ctrl)
mvc.New(app).
	Register(new(myService)).
	Handle(ctrl, mvc.GRPC{
		Server:      grpcServer,           // Required.
		ServiceName: "helloworld.Greeter", // Required.
		Strict:      false,
	})

BaseController

package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

type BaseController struct {
	Ctx iris.Context
}

func (c *BaseController) ReadJSON(i interface{}) error {

	return nil
}

func (c *BaseController) MessageResp(success bool, message string) *mvc.Response {

	return nil
}

func (c *BaseController) ErrParamsResp() *mvc.Response {

	return nil
}

stdout

POST: /helloworld.Greeter/ErrParamsResp gRPC myController (./mvc/grpc.go:46)
      • myController.ErrParamsResp (./_examples/mvc/grpc-compatible/base.go:22)
POST: /helloworld.Greeter/MessageResp gRPC myController (./mvc/grpc.go:46)
      • myController.MessageResp (./_examples/mvc/grpc-compatible/base.go:17)
POST: /helloworld.Greeter/ReadJSON gRPC myController (./mvc/grpc.go:46)
      • myController.ReadJSON (./_examples/mvc/grpc-compatible/base.go:12)
POST: /helloworld.Greeter/SayHello gRPC myController (./mvc/grpc.go:46)
      • myController.SayHello (./_examples/mvc/grpc-compatible/main.go:89)

Expected behavior
A clear and concise description of what you expected to happen.
stdout should be

POST: /helloworld.Greeter/SayHello gRPC myController (./mvc/grpc.go:46)
      • myController.SayHello (./_examples/mvc/grpc-compatible/main.go:89)

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: mac m1
    iris.Version
  • v12.2.0
@kataras
Copy link
Owner

kataras commented Mar 19, 2023

Hello @AliangCoder, this is expected, it's a good feature actually. All embedded structs and the controller struct it self are parsed as one request controller. This gives you the ability to make generic base controllers that can be used on any other controller. I don't know why is this a problem but if it makes your life harder, we could provide an option/setting to skip this feature.

@kataras
Copy link
Owner

kataras commented Mar 19, 2023

Hello @AliangCoder, I am back with a solution for you. I've just pushed a commit to the master branch (go get github.com/kataras/iris/v12@master) which gives you two options to ignore embedded struct's methods:

  1. Global
mvc.IgnoreEmbeddedControllers = true
  1. Per Controller using mvc.IgnoreEmbedded option on .Handle method:
mvc.New(app).
	Register(new(myService)).
	Handle(ctrl, mvc.IgnoreEmbedded, mvc.GRPC{
		Server:      grpcServer,           // Required.
		ServiceName: "helloworld.Greeter", // Required.
		Strict:      false,
	})

@kataras kataras changed the title [BUG]When using iris and gRPC, if a struct contains a parent struct, the parent struct will be handled and parsed. [FEATURE] When using iris and gRPC, if a struct contains a parent struct, the parent struct will be handled and parsed. Mar 19, 2023
@AliangCoder
Copy link
Author

Thanks

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

2 participants