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

Best-practice go:generate command in generated server code #3000

Open
radeksimko opened this issue Dec 15, 2023 · 1 comment
Open

Best-practice go:generate command in generated server code #3000

radeksimko opened this issue Dec 15, 2023 · 1 comment

Comments

@radeksimko
Copy link

Problem statement

Currently the server code which gets generated contains a line with hard-coded //go:generate part. Specifically it assumes that swagger is always pre-installed somewhere and available in the $PATH.

Managing tools like go-swagger as an external dependency however brings a number of challenges, not least one related to differences between platforms (OS/arch). This is why Go has an established common pattern for maintaining tools like these as regular dependencies via tools.go, see https://go.dev/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module for more. One possible consequence of this approach is that the tool is launched via go run rather than directly.

Meaning, that instead of

//go:generate swagger generate ...

one would have

//go:generate go run github.com/go-swagger/go-swagger/cmd/swagger generate ...

Currently, the server code only generates the top version. While I understand that the particular file involved is safe to edit I still think it would be helpful if it could be generated with best practices in the beginning, so it does not have to be edited.

This could probably be achieved in two ways - either it may become another CLI flag of some kind, or it could just be detected automatically from os.Args.

Swagger specification

{
    "info": {
        "title": "Example",
        "version": "0.0.1"
    },
    "swagger": "2.0",
    "host": "example.com",
    "paths": {
        "/v1/products": {
            "get": {
                "summary": "List product names",
                "operationId": "listProducts",
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

Steps to reproduce

Generate server code

go run github.com/go-swagger/go-swagger/cmd/swagger generate server

Open ./restapi/configure_example.go

// ...

//go:generate swagger generate server --target ../../test --name Example --spec ../swagger.json --principal interface{}

// ...

Environment

swagger version: v0.30.5
go version: go1.21.5
OS: darwin/arm64

@souradeepmajumdar05
Copy link
Contributor

souradeepmajumdar05 commented Jan 7, 2024

working on the approach.
will post pseudo code before implementation
with my current schedule targeting this by 11/01

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

3 participants