Skip to content

Commit

Permalink
Merge branch 'master' into parse-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkova committed Dec 7, 2022
2 parents a242444 + 3fe9ca2 commit 36e1dc5
Show file tree
Hide file tree
Showing 121 changed files with 8,128 additions and 1,003 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -10,15 +10,15 @@ jobs:
test:
strategy:
matrix:
go: [ '1.15.x', '1.16.x', '1.17.x', '1.18.x' ]
go: [ '1.16.x', '1.17.x', '1.18.x' ]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v3
with:
path: ./src/github.com/${{ github.repository }}
- name: Set up Go
uses: actions/setup-go@v1
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: deps
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/docker.yml
@@ -0,0 +1,43 @@
name: docker

on:
push:
tags:
- 'v*'

jobs:
docker-build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

- name: Login to Github Packages
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/swaggo/swag

- name: Build image and push to GitHub Container Registry
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: |
ghcr.io/swaggo/swag:latest
ghcr.io/swaggo/swag:${{github.ref_name}}
labels: ${{ steps.meta.outputs.labels }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
2 changes: 2 additions & 0 deletions .goreleaser.yml
Expand Up @@ -10,6 +10,8 @@ build:
ignore:
- goos: darwin
goarch: arm64
env:
- CGO_ENABLED=0
archives:
-
replacements:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,7 +1,7 @@
# Dockerfile References: https://docs.docker.com/engine/reference/builder/

# Start from the latest golang base image
FROM golang:1.17-alpine as builder
FROM golang:1.18.3-alpine as builder

# Set the Current Working Directory inside the container
WORKDIR /app
Expand Down
41 changes: 35 additions & 6 deletions README.md
Expand Up @@ -29,6 +29,7 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
- [Examples](#examples)
- [Descriptions over multiple lines](#descriptions-over-multiple-lines)
- [User defined structure with an array type](#user-defined-structure-with-an-array-type)
- [Function scoped struct declaration](#function-scoped-struct-declaration)
- [Model composition in response](#model-composition-in-response)
- [Add a headers in response](#add-a-headers-in-response)
- [Use multiple path params](#use-multiple-path-params)
Expand All @@ -51,12 +52,9 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie

2. Download swag by using:
```sh
$ go get -u github.com/swaggo/swag/cmd/swag

# 1.16 or newer
$ go install github.com/swaggo/swag/cmd/swag@latest
```
To build from source you need [Go](https://golang.org/dl/) (1.15 or newer).
To build from source you need [Go](https://golang.org/dl/) (1.16 or newer).

Or download a pre-compiled binary from the [release page](https://github.com/swaggo/swag/releases).

Expand Down Expand Up @@ -87,6 +85,7 @@ USAGE:
swag init [command options] [arguments...]

OPTIONS:
--quiet, -q Make the logger quiet. (default: false)
--generalInfo value, -g value Go file path in which 'swagger general API Info' is written (default: "main.go")
--dir value, -d value Directories you want to parse,comma separated and general-info file must be in the first one (default: "./")
--exclude value Exclude directories and files when searching, comma separated
Expand All @@ -100,8 +99,11 @@ OPTIONS:
--parseInternal Parse go files in internal packages, disabled by default (default: false)
--generatedTime Generate timestamp at the top of docs.go, disabled by default (default: false)
--parseDepth value Dependency parse depth (default: 100)
--requiredByDefault Set validation required for all fields by default (default: false)
--instanceName value This parameter can be used to name different swagger document instances. It is optional.
--overridesFile value File to read global type overrides from. (default: ".swaggo")
--parseGoList Parse dependency via 'go list' (default: true)
--tags value, -t value A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded
--help, -h show help (default: false)
```
Expand All @@ -127,9 +129,12 @@ OPTIONS:
- [echo](http://github.com/swaggo/echo-swagger)
- [buffalo](https://github.com/swaggo/buffalo-swagger)
- [net/http](https://github.com/swaggo/http-swagger)
- [gorilla/mux](https://github.com/swaggo/http-swagger)
- [go-chi/chi](https://github.com/swaggo/http-swagger)
- [flamingo](https://github.com/i-love-flamingo/swagger)
- [fiber](https://github.com/arsmn/fiber-swagger)
- [fiber](https://github.com/gofiber/swagger)
- [atreugo](https://github.com/Nerzal/atreugo-swagger)
- [hertz](https://github.com/hertz-contrib/swagger)
## How to use it with Gin
Expand Down Expand Up @@ -488,7 +493,7 @@ type Foo struct {
Field Name | Type | Description
---|:---:|---
<a name="validate"></a>validate | `string` | Determines the validation for the parameter. Possible values are: `required`.
<a name="validate"></a>validate | `string` | Determines the validation for the parameter. Possible values are: `required,optional`.
<a name="parameterDefault"></a>default | * | Declares the value of the parameter that the server will use if none is provided, for example a "count" to control the number of results per page might default to 100 if not supplied by the client in the request. (Note: "default" has no meaning for required parameters.) See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2. Unlike JSON Schema this value MUST conform to the defined [`type`](#parameterType) for this parameter.
<a name="parameterMaximum"></a>maximum | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2.
<a name="parameterMinimum"></a>minimum | `number` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3.
Expand Down Expand Up @@ -537,6 +542,30 @@ type Account struct {
}
```
### Function scoped struct declaration
You can declare your request response structs inside a function body.
You must have to follow the naming convention `<package-name>.<function-name>.<struct-name> `.
```go
package main
// @Param request body main.MyHandler.request true "query params"
// @Success 200 {object} main.MyHandler.response
// @Router /test [post]
func MyHandler() {
type request struct {
RequestField string
}
type response struct {
ResponseField string
}
}
```
### Model composition in response
```go
// JSONResult's data field will be overridden by the specific type proto.Order
Expand Down
12 changes: 8 additions & 4 deletions README_zh-CN.md
Expand Up @@ -47,13 +47,10 @@ Swag将Go的注释转换为Swagger2.0文档。我们为流行的 [Go Web Framewo
2. 使用如下命令下载swag:

```bash
$ go get -u github.com/swaggo/swag/cmd/swag

# 1.16 及以上版本
$ go install github.com/swaggo/swag/cmd/swag@latest
```

从源码开始构建的话,需要有Go环境(1.15及以上版本)。
从源码开始构建的话,需要有Go环境(1.16及以上版本)。

或者从github的release页面下载预编译好的二进制文件。

Expand Down Expand Up @@ -123,6 +120,13 @@ OPTIONS:
- [echo](http://github.com/swaggo/echo-swagger)
- [buffalo](https://github.com/swaggo/buffalo-swagger)
- [net/http](https://github.com/swaggo/http-swagger)
- [net/http](https://github.com/swaggo/http-swagger)
- [gorilla/mux](https://github.com/swaggo/http-swagger)
- [go-chi/chi](https://github.com/swaggo/http-swagger)
- [flamingo](https://github.com/i-love-flamingo/swagger)
- [fiber](https://github.com/gofiber/swagger)
- [atreugo](https://github.com/Nerzal/atreugo-swagger)
- [hertz](https://github.com/hertz-contrib/swagger)

## 如何与Gin集成

Expand Down
56 changes: 35 additions & 21 deletions cmd/swag/main.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"os"
"strings"
Expand All @@ -15,24 +15,26 @@ import (
)

const (
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
outputTypesFlag = "outputTypes"
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
codeExampleFilesFlag = "codeExampleFiles"
parseInternalFlag = "parseInternal"
generatedTimeFlag = "generatedTime"
parseDepthFlag = "parseDepth"
instanceNameFlag = "instanceName"
overridesFileFlag = "overridesFile"
parseGoListFlag = "parseGoList"
quietFlag = "quiet"
parseExtensionFlag = "parseExtension"
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
outputTypesFlag = "outputTypes"
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
codeExampleFilesFlag = "codeExampleFiles"
parseInternalFlag = "parseInternal"
generatedTimeFlag = "generatedTime"
requiredByDefaultFlag = "requiredByDefault"
parseDepthFlag = "parseDepth"
instanceNameFlag = "instanceName"
overridesFileFlag = "overridesFile"
parseGoListFlag = "parseGoList"
quietFlag = "quiet"
tagsFlag = "tags"
parseExtensionFlag = "parseExtension"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -109,6 +111,10 @@ var initFlags = []cli.Flag{
Value: 100,
Usage: "Dependency parse depth",
},
&cli.BoolFlag{
Name: requiredByDefaultFlag,
Usage: "Set validation required for all fields by default",
},
&cli.StringFlag{
Name: instanceNameFlag,
Value: "",
Expand All @@ -129,6 +135,12 @@ var initFlags = []cli.Flag{
Value: "",
Usage: "Parse only those operations that match given extension",
},
&cli.StringFlag{
Name: tagsFlag,
Aliases: []string{"t"},
Value: "",
Usage: "A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded",
},
}

func initAction(ctx *cli.Context) error {
Expand All @@ -144,9 +156,9 @@ func initAction(ctx *cli.Context) error {
if len(outputTypes) == 0 {
return fmt.Errorf("no output types specified")
}
var logger swag.Debugger
logger := log.New(os.Stdout, "", log.LstdFlags)
if ctx.Bool(quietFlag) {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
logger = log.New(io.Discard, "", log.LstdFlags)
}

return gen.New().Build(&gen.Config{
Expand All @@ -162,11 +174,13 @@ func initAction(ctx *cli.Context) error {
MarkdownFilesDir: ctx.String(markdownFilesFlag),
ParseInternal: ctx.Bool(parseInternalFlag),
GeneratedTime: ctx.Bool(generatedTimeFlag),
RequiredByDefault: ctx.Bool(requiredByDefaultFlag),
CodeExampleFilesDir: ctx.String(codeExampleFilesFlag),
ParseDepth: ctx.Int(parseDepthFlag),
InstanceName: ctx.String(instanceNameFlag),
OverridesFile: ctx.String(overridesFileFlag),
ParseGoList: ctx.Bool(parseGoListFlag),
Tags: ctx.String(tagsFlag),
Debugger: logger,
})
}
Expand Down

0 comments on commit 36e1dc5

Please sign in to comment.