Skip to content

Commit

Permalink
Merge pull request #662 from MaitreDede/feat/cgo
Browse files Browse the repository at this point in the history
feat: Skip cgo package
  • Loading branch information
sdghchj authored Apr 16, 2020
2 parents dc90151 + 5b6f1b7 commit 86f9d9c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,29 @@ func TestGen_GeneratedDoc(t *testing.T) {
os.Remove(expectedFile)
}
}

func TestGen_cgoImports(t *testing.T) {
searchDir := "../testdata/simple_cgo"

config := &Config{
SearchDir: searchDir,
MainAPIFile: "./main.go",
OutputDir: "../testdata/simple_cgo/docs",
PropNamingStrategy: "",
ParseDependency: true,
}

assert.NoError(t, New().Build(config))

expectedFiles := []string{
path.Join(config.OutputDir, "docs.go"),
path.Join(config.OutputDir, "swagger.json"),
path.Join(config.OutputDir, "swagger.yaml"),
}
for _, expectedFile := range expectedFiles {
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Fatal(err)
}
os.Remove(expectedFile)
}
}
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,10 @@ func (parser *Parser) getAllGoFileInfoFromDeps(pkg *depth.Pkg) error {
return nil
}

// Skip cgo
if pkg.Raw == nil && pkg.Name == "C" {
return nil
}
srcDir := pkg.Raw.Dir
files, err := ioutil.ReadDir(srcDir) // only parsing files in the dir(don't contains sub dir files)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions testdata/simple_cgo/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

import (
"github.com/gin-gonic/gin"
)

// @Summary Add a new pet to the store
// @Description get string by ID
// @ID get-string-by-int
// @Accept json
// @Produce json
// @Param some_id path int true "Some ID" Format(int64)
// @Param some_id body int true "Some ID"
// @Success 200 {string} string "ok"
// @Failure 400 {object} string "We need ID!!"
// @Failure 404 {object} string "Can not find ID"
// @Router /testapi/get-string-by-int/{some_id} [get]
func GetStringByInt(c *gin.Context) {
}
64 changes: 64 additions & 0 deletions testdata/simple_cgo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

/*
#include <stdio.h>
void Hello(){
printf("Hello world\n");
}
*/
import "C"

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/swag/testdata/simple_cgo/api"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2

// @securityDefinitions.basic BasicAuth

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization

// @securitydefinitions.oauth2.application OAuth2Application
// @tokenUrl https://example.com/oauth/token
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.implicit OAuth2Implicit
// @authorizationurl https://example.com/oauth/authorize
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.password OAuth2Password
// @tokenUrl https://example.com/oauth/token
// @scope.read Grants read access
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.accessCode OAuth2AccessCode
// @tokenUrl https://example.com/oauth/token
// @authorizationurl https://example.com/oauth/authorize
// @scope.admin Grants read and write access to administrative information
func main() {
C.Hello()

r := gin.New()
r.GET("/testapi/get-string-by-int/:some_id", api.GetStringByInt)
r.Run()
}

0 comments on commit 86f9d9c

Please sign in to comment.