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

Pointer return type is not supported for endpont generation #32

Closed
flamedmg opened this issue Feb 17, 2020 · 5 comments · Fixed by #33
Closed

Pointer return type is not supported for endpont generation #32

flamedmg opened this issue Feb 17, 2020 · 5 comments · Fixed by #33

Comments

@flamedmg
Copy link

Imaging service with this method:

RegisterUser(ctx context.Context, user User, trackingInfo *schema.TrackingInfo) (*User, string, string, error)

During generation process i'm getting the following error:

panic: unknown type: *extapi/internal/app/extapi/user.User

goroutine 1 [running]:
sagikazarmark.dev/mga/pkg/jenutils.Type(0xc00025a000, 0x1ce90e0, 0xc0009b1020, 0x0, 0x1)
        /home/runner/work/mga/mga/pkg/jenutils/type.go:71 +0x1921
sagikazarmark.dev/mga/internal/generate/kit/endpoint.generateEndpointSet(0xc0000cc320, 0xc0009aeaa0, 0xc0009dd4f0, 0x0, 0x0, 0x1, 0xc0005f8022, 0x7)
        /home/runner/work/mga/mga/internal/generate/kit/endpoint/generator.go:195 +0x28b5
sagikazarmark.dev/mga/internal/generate/kit/endpoint.Generate(0xc0005ee060, 0xa, 0xc0005f8060, 0x2a, 0x0, 0x0, 0xc00051e200, 0x1, 0x1, 0x0, ...)
        /home/runner/work/mga/mga/internal/generate/kit/endpoint/generator.go:73 +0x1192
sagikazarmark.dev/mga/internal/generate/kit/endpoint/endpointgen.Generator.generatePackage(0x0, 0x0, 0x0, 0x0, 0xc0000c27d0, 0x0, 0x0, 0xc00021ff00, 0x0, 0x203000, ...)
        /home/runner/work/mga/mga/internal/generate/kit/endpoint/endpointgen/gen.go:170 +0x280
sagikazarmark.dev/mga/internal/generate/kit/endpoint/endpointgen.Generator.Generate(0x0, 0x0, 0x0, 0x0, 0xc0000c27d0, 0x1a90001, 0xc0000c27d0)
        /home/runner/work/mga/mga/internal/generate/kit/endpoint/endpointgen/gen.go:82 +0x1c2
sigs.k8s.io/controller-tools/pkg/genall.(*Runtime).Run(0xc00052d200, 0x14)
        /home/runner/go/pkg/mod/sigs.k8s.io/controller-tools@v0.2.4/pkg/genall/genall.go:171 +0x15e
sagikazarmark.dev/mga/internal/cmd/commands/generate/kit.runEndpoint(0x0, 0x0, 0x0, 0x0, 0xc0003943a0, 0x1, 0x1, 0x1b39c00, 0x14, 0x115fc04, ...)
        /home/runner/work/mga/mga/internal/cmd/commands/generate/kit/endpoint.go:84 +0x1b5
sagikazarmark.dev/mga/internal/cmd/commands/generate/kit.NewEndpointCommand.func1(0xc000399680, 0xc0003943a0, 0x1, 0x1, 0x0, 0x0)
        /home/runner/work/mga/mga/internal/cmd/commands/generate/kit/endpoint.go:47 +0xe3
github.com/spf13/cobra.(*Command).execute(0xc000399680, 0xc000394370, 0x1, 0x1, 0xc000399680, 0xc000394370)
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:826 +0x460
github.com/spf13/cobra.(*Command).ExecuteC(0xc000398500, 0xc000300e68, 0x1b1ac5f, 0x8)
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:914 +0x2fb
github.com/spf13/cobra.(*Command).Execute(...)
        /home/runner/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:864
main.main()
        /home/runner/work/mga/mga/main.go:48 +0x2c1

Looks like func Type(stmt *jen.Statement, t types.Type) jen.Code function is missing case for the pointer type

@sagikazarmark
Copy link
Owner

@flamedmg thanks for reporting this issue.

Although generally services should probably return values instead of pointers, this is indeed a bug.

I submitted a PR with a fix. Can you give that patch a try? I also added a test case based on your example, it seems to fix the issue.

Thanks!

@flamedmg
Copy link
Author

Yes, it fixes the issue. Thank you! But i have different one. I will create an issue whenever i will have more info.

@flamedmg
Copy link
Author

Interesting, it works with type from the same module, but not works with imported
To reproduce try this:

package pointer_message

import (
	"context"

	"github.com/dgrijalva/jwt-go"
)

type User struct{}
type TrackingInfo struct{}

type Service interface {
	// RegisterUser from https://github.com/sagikazarmark/mga/issues/32#issue-566519866
	RegisterUser(ctx context.Context, user User, trackingInfo *TrackingInfo) (registeredUser *User, r1 string, r2 string, err error)
	Refresh(ctx context.Context, refreshToken string, deviceId string, userName string, jwtToken *jwt.Token) (*User, string, string, error)

}

I'm getting the following error:

panic: invalid basic kind: invalid type [recovered]
	panic: invalid basic kind: invalid type

@flamedmg flamedmg reopened this Feb 18, 2020
@flamedmg
Copy link
Author

Also you mentioned that services should return values. Could you please suggest how to properly implement this:

type Service interface {
	// RegisterUser from https://github.com/sagikazarmark/mga/issues/32#issue-566519866
	GetUserById(ctx context.Context, id int) (*User, error)
}

If user is absent, NotFound error is returned and nil instead of user. If i replace *User to User - should i just return User{} ?

@sagikazarmark
Copy link
Owner

If user is absent, NotFound error is returned and nil instead of user. If i replace *User to User - should i just return User{} ?

That's right!

Interesting, it works with type from the same module, but not works with imported
To reproduce try this:

Can you please open a separate issue for that?

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

Successfully merging a pull request may close this issue.

2 participants