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

How to re-arrange oapi-codegen based petstore example using the Repository Pattern? #70

Closed
suntong opened this issue Sep 12, 2023 · 2 comments

Comments

@suntong
Copy link

suntong commented Sep 12, 2023

The oapi-codegen based petstore example has a very simple and straightforward file structure. Using the fiber server as an example, as far as the server end is concerned, here are all the files it needs:

`--petstore-expanded.yaml
`--fiber
| `--petstore.go
| `--petstore_test.go
| `--api
| | `--petstore.go
| | `--petstore-server.gen.go
| | `--server.cfg.yaml
| | `--petstore-types.gen.go
| | `--types.cfg.yaml

How to re-arrange oapi-codegen based petstore example using the Repository Pattern?

I tried to split the code as per the Repository Pattern myself following the folder structure in #69:

`--api
| `--openapi
| | `--petstore-expanded.yaml
`--internal
| `--petstore
| | `--main.go
| | `--main_test.go
| | `--core
| | | `--app
| | | `--domain
| | | | `--petstore.go
| | | `--services
| | `--interfaces
| | | `--adapters
| | | `--ports
| | | | `--petstore-server.gen.go
| | | | `--petstore-types.gen.go
| | | | `--server.cfg.yaml
| | | | `--types.cfg.yaml

However in file domain/petstore.go (changed a bit from here):

// FindPets implements all the handlers in the ServerInterface
func (p *PetStore) FindPets(c *fiber.Ctx, params ports.FindPetsParams) error {

I.e., I have to use the type FindPetsParams defined in ports (ports/petstore-types.gen.go) in domain, and then extract the server agnostic part of algorithm into function FindPets in the domain package, and call such domain FindPets function in http.go file within the ports package.

In other words, I'll be doing import cycling, which is not allowed in Go. So, all in all,

How to re-arrange oapi-codegen based petstore example using the Repository Pattern? thx.

@m110
Copy link
Member

m110 commented Sep 12, 2023

Hey.

First of all, your domain should have no relation to fiber, since it's a http-specific library. Keep fiber imports in ports and nowhere else.

Then, you need to define the Params inside domain and map between it and fiber's Params in ports. Or you can have domain constructors that take arguments and just pass them from Params.

@suntong
Copy link
Author

suntong commented Sep 12, 2023

OK. thanks.

@suntong suntong closed this as completed Sep 12, 2023
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

No branches or pull requests

2 participants