goag

package module
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 11 Imported by: 0

README

Goag - Golang OpenAPIv3 Generator

This tool generates boilerplate code for the http handling. It handles the parsing of queries, headers or json bodies.

It makes impossible to return undocumented type/response by endpoint.

Example

var db interface {
    GetPet(_ context.Context, id string) (Pet, error)
}

func ExampleAPI_petsStore() {
    api := &API{
        ShowPetByIDHandler: func(ctx context.Context, r ShowPetByIDRequest) ShowPetByIDResponse {
            req, err := r.Parse()
            if err != nil {
                return NewShowPetByIDResponseDefaultJSON(http.StatusBadRequest, Error{
                    Code:    400,
                    Message: fmt.Sprintf("Bad request: %v", err),
                })
            }

            out, err := db.GetPet(r.HTTP().Context(), req.Path.PetID)
            if err != nil {
                return NewShowPetByIDResponseDefaultJSON(http.StatusInternalServerError, Error{
                    Code:    500,
                    Message: fmt.Sprintf("Internal server error: %v", err),
                })
            }

            return NewShowPetByIDResponse200JSON(out)
        },
        // ...
    }

    _ = http.ListenAndServe(":8080", api)
}
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderToFile

func RenderToFile(filepath string, f generator.GoFile) error

func WriteToFile

func WriteToFile(bs []byte, filepath string) error

Types

type Generator

type Generator struct {
	GenClient bool
	DeleteOld bool
	DoNotEdit bool
}

func (Generator) Generate

func (g Generator) Generate(openapi3Spec *openapi3.Swagger, outDir string, packageName string, specRaw []byte, baseFilename, basePath string, cfg generator.Config) error

func (Generator) GenerateDir

func (g Generator) GenerateDir(dir, out, packageName, specFilename, basePath, cfgFilename string) error

func (Generator) GenerateFile

func (g Generator) GenerateFile(outDir, packageName, specFilename, basePath, cfgFilename string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL