Skip to content

Commit

Permalink
Merge pull request #14 from MZC-CSC/main
Browse files Browse the repository at this point in the history
Changing Swagger JSON parsing logic - bug fix #13
  • Loading branch information
MZC-CSC authored Jul 1, 2024
2 parents 4084522 + 56c8de5 commit dd299c7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 224 deletions.
Binary file modified bin/mayfly
Binary file not shown.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
90 changes: 90 additions & 0 deletions src/cmd/apicall/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package apicall

import (
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tidwall/gjson"
)

var swaggerFile string

// pullCmd represents the pull command
var parseCmd = &cobra.Command{
Use: "tool",
Short: "Swagger JSON file parsing",
Long: `Swagger JSON file parsing to assist in writing api.yaml files`,
Run: func(cmd *cobra.Command, args []string) {
parseJson()
},
}

func parseJson() {
// swagger.json 파일 읽기
data, err := os.ReadFile(swaggerFile)
if err != nil {
log.Fatalf("파일 읽기 오류: %s", err)
}

json := string(data)

// 기본 정보 추출
title := gjson.Get(json, "info.title")
version := gjson.Get(json, "info.version")
host := gjson.Get(json, "host")
basePath := gjson.Get(json, "basePath")

fmt.Println("API Title:", title.String())
fmt.Println("API Version:", version.String())
fmt.Println("Host:", host.String())
fmt.Println("Base Path:", basePath.String())

// paths 하위의 URI, method 및 operationId 추출
paths := gjson.Get(json, "paths").Map()
for path, methods := range paths {
//fmt.Println("URI:", path)
for method, details := range methods.Map() {
operationId := details.Get("operationId")
if strings.ToLower(method) == "parameters" {
continue
}

//fmt.Printf(" Method: %s, OperationId: %s\n", method, operationId.String())
tmpActionName := convertActionlName(operationId.String())
fmt.Printf(" %s:\n", tmpActionName)
fmt.Printf(" method: %s\n", method)
fmt.Printf(" resourcePath: %s\n", path)
fmt.Printf(" description: %q\n", details.Get("description").String())
}
}
}

func convertActionlName(tmpActionName string) string {
//일부 특수 기호들 제거
tmpActionName = strings.ReplaceAll(tmpActionName, ":", "-")
tmpActionName = strings.ReplaceAll(tmpActionName, "`", "")
tmpActionName = strings.ReplaceAll(tmpActionName, "'", "")
//tmpActionName = strings.ReplaceAll(tmpActionName, "\n", " ")

//카멜타입으로 변경
tmpActionName = toCamelCase(tmpActionName)

return tmpActionName
}

func toCamelCase(str string) string {
words := strings.Fields(str) // 문자열을 공백을 기준으로 단어로 분할
var result strings.Builder
for _, word := range words {
result.WriteString(strings.Title(word)) // 각 단어의 첫 글자를 대문자로 만듦
}
return result.String()
}

func init() {
apiCmd.AddCommand(parseCmd)
parseCmd.PersistentFlags().StringVarP(&swaggerFile, "file", "f", "../conf/swagger.json", "Swagger JSON file full path")
}
224 changes: 0 additions & 224 deletions src/cmd/apicall/util.go

This file was deleted.

0 comments on commit dd299c7

Please sign in to comment.