From 286ad8334b5973a530e7392cfe00df50d7945f91 Mon Sep 17 00:00:00 2001 From: takanakahiko Date: Tue, 19 Sep 2023 20:02:41 +0900 Subject: [PATCH 1/5] update github.com/urfave/cli version --- commands.go | 24 +++++++++++++----------- main.go | 6 ++++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/commands.go b/commands.go index 34861db..e67b0c5 100644 --- a/commands.go +++ b/commands.go @@ -8,15 +8,15 @@ import ( atgen "github.com/aktsk/atgen/lib" "github.com/pkg/errors" - "github.com/urfave/cli" + "github.com/urfave/cli/v2" ) -var commands = []cli.Command{ +var commands = []*cli.Command{ commandGen, //commandDiff, } -var commandGen = cli.Command{ +var commandGen = &cli.Command{ Name: "gen", Usage: "Generate test code", Description: ` @@ -24,15 +24,17 @@ var commandGen = cli.Command{ `, Action: doGen, Flags: []cli.Flag{ - cli.StringFlag{ - Name: "templateDir, t", - Value: ".", - Usage: "template directory that has template yaml and code", + &cli.StringFlag{ + Name: "templateDir", + Aliases: []string{"t"}, + Value: ".", + Usage: "template directory that has template yaml and code", }, - cli.StringFlag{ - Name: "outputDir, o", - Value: ".", - Usage: "output directory to write generated test files", + &cli.StringFlag{ + Name: "outputDir", + Aliases: []string{"o"}, + Value: ".", + Usage: "output directory to write generated test files", }, }, } diff --git a/main.go b/main.go index 452bfd2..0227a8c 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "github.com/urfave/cli" + "github.com/urfave/cli/v2" ) var version = "0.7.0" @@ -22,7 +22,9 @@ func newApp() *cli.App { app.Name = "atgen" app.Usage = "Generate API test code from Request/Response definition or show diff between Request/Response definition and API definition" app.Version = version - app.Author = "Akatsuki Inc." + app.Authors = []*cli.Author{ + {Name: "Akatsuki Inc."}, + } app.Commands = commands return app } From 472b6d4d6fe81b291fa1e73939517c628b1f3866 Mon Sep 17 00:00:00 2001 From: takanakahiko Date: Tue, 19 Sep 2023 21:05:19 +0900 Subject: [PATCH 2/5] update gopkg.in/yaml version --- lib/parser.go | 38 ++++++++++++++------------------------ lib/parser_test.go | 2 +- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/lib/parser.go b/lib/parser.go index c4bcf2f..14f7ad6 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -10,7 +10,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/afero" "golang.org/x/tools/go/packages" - yaml "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v3" ) // ParseYaml parses yaml which defines test requests/responses @@ -61,8 +61,8 @@ func (g *Generator) ParseYaml() error { return nil } -func parseYaml(buf []byte) ([]map[interface{}]interface{}, error) { - var parsed []map[interface{}]interface{} +func parseYaml(buf []byte) ([]map[string]interface{}, error) { + var parsed []map[string]interface{} err := yaml.Unmarshal(buf, &parsed) if err != nil { return nil, errors.WithStack(err) @@ -70,7 +70,7 @@ func parseYaml(buf []byte) ([]map[interface{}]interface{}, error) { return parsed, nil } -func convertToTestFuncs(parsed []map[interface{}]interface{}) (TestFuncs, error) { +func convertToTestFuncs(parsed []map[string]interface{}) (TestFuncs, error) { var testFuncs TestFuncs for _, p := range parsed { if p["name"] == nil { @@ -103,7 +103,7 @@ func convertToTestFuncs(parsed []map[interface{}]interface{}) (TestFuncs, error) testFunc.Vars = vars for _, t := range p["tests"].([]interface{}) { - t := t.(map[interface{}]interface{}) + t := t.(map[string]interface{}) if t["path"] != nil { test, err := convertToTest(t) if err != nil { @@ -253,7 +253,7 @@ func validateRouterFuncObj(handlerObj types.Object, routerFuncObj types.Object, return nil } -func convertToTest(t map[interface{}]interface{}) (Test, error) { +func convertToTest(t map[string]interface{}) (Test, error) { var apiVersions []string if t["apiVersions"] != nil { for _, v := range t["apiVersions"].([]interface{}) { @@ -290,14 +290,14 @@ func convertToTest(t map[interface{}]interface{}) (Test, error) { }, nil } -func convertToSubtests(s map[interface{}]interface{}) (Subtests, error) { +func convertToSubtests(s map[string]interface{}) (Subtests, error) { subTests := Subtests{} for _, s := range s["subtests"].([]interface{}) { - s := s.(map[interface{}]interface{}) + s := s.(map[string]interface{}) subtest := Subtest{Name: s["name"].(string)} for _, t := range s["tests"].([]interface{}) { - t := t.(map[interface{}]interface{}) + t := t.(map[string]interface{}) test, err := convertToTest(t) if err != nil { return subTests, err @@ -314,7 +314,7 @@ func convertToReq(r interface{}) (Req, error) { return Req{}, nil } - req := r.(map[interface{}]interface{}) + req := r.(map[string]interface{}) headers, err := convertToHeaders(req["headers"]) if err != nil { @@ -368,7 +368,7 @@ func convertToRes(r interface{}) (Res, error) { return Res{}, nil } - res := r.(map[interface{}]interface{}) + res := r.(map[string]interface{}) headers, err := convertToHeaders(res["headers"]) if err != nil { @@ -390,16 +390,11 @@ func convertToRes(r interface{}) (Res, error) { func convertToParams(p interface{}) (map[string]interface{}, error) { params := make(map[string]interface{}) if p != nil { - for k, v := range p.(map[interface{}]interface{}) { - key, ok := k.(string) - if !ok { - return params, errors.New("key should be string") - } - + for key, v := range p.(map[string]interface{}) { switch t := v.(type) { case string, bool, int: params[key] = t - case map[interface{}]interface{}: + case map[string]interface{}: p, err := convertToParams(t) if err != nil { return params, err @@ -425,12 +420,7 @@ func convertToParams(p interface{}) (map[string]interface{}, error) { func convertToHeaders(h interface{}) (map[string]string, error) { headers := make(map[string]string) if h != nil { - for k, v := range h.(map[interface{}]interface{}) { - key, ok := k.(string) - if !ok { - return headers, errors.New("header key must be string") - } - + for key, v := range h.(map[string]interface{}) { val, ok := v.(string) if !ok { return headers, errors.New("header val must be string") diff --git a/lib/parser_test.go b/lib/parser_test.go index 75b61e3..d47dde4 100644 --- a/lib/parser_test.go +++ b/lib/parser_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func convertToTestFuncsHelper(t *testing.T, parsed []map[interface{}]interface{}) TestFuncs { +func convertToTestFuncsHelper(t *testing.T, parsed []map[string]interface{}) TestFuncs { t.Helper() funcs, err := convertToTestFuncs(parsed) From 70601afe6c97925fbf3b739fd5b0992f419f9b9d Mon Sep 17 00:00:00 2001 From: takanakahiko Date: Tue, 19 Sep 2023 21:05:55 +0900 Subject: [PATCH 3/5] update go version of github-actions --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8ab0fd5..a5b2908 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.11.x] + go-version: [1.21.x] os: [ubuntu-latest] runs-on: ${{ matrix.os }} steps: From 1224f4238062edacf3a6844e7c3993fedf0968e4 Mon Sep 17 00:00:00 2001 From: takanakahiko Date: Tue, 19 Sep 2023 21:06:15 +0900 Subject: [PATCH 4/5] go mod tidy --- go.mod | 6 +----- go.sum | 27 ++++----------------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 6373ff5..74f361d 100644 --- a/go.mod +++ b/go.mod @@ -7,20 +7,16 @@ require ( github.com/lkesteloot/astutil v0.0.0-20130122170032-b6715328cfa5 github.com/pkg/errors v0.9.1 github.com/spf13/afero v1.2.0 - github.com/urfave/cli v1.22.14 github.com/urfave/cli/v2 v2.25.7 golang.org/x/tools v0.0.0-20190117194123-b4b6fe2cb829 - gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect - github.com/gorilla/context v1.1.1 // indirect github.com/kr/pretty v0.1.0 // indirect - github.com/kr/pty v1.1.1 // indirect - github.com/kr/text v0.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index 9789743..48a34db 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,5 @@ -github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= -github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= -github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk= -github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -18,22 +11,14 @@ github.com/lkesteloot/astutil v0.0.0-20130122170032-b6715328cfa5 h1:+nqKYezU5GyI github.com/lkesteloot/astutil v0.0.0-20130122170032-b6715328cfa5/go.mod h1:5EJrn5eOawLeALv3b2JE+RDh3jrm7yrzNBiMW8LZGyQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/afero v1.2.0 h1:O9FblXGxoTc51M+cqr74Bm2Tmt4PvkA5iu/j8HrkNuY= github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= -github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= -github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk= -github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA= +github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -43,9 +28,5 @@ golang.org/x/tools v0.0.0-20190117194123-b4b6fe2cb829/go.mod h1:n7NCudcB/nEzxVGm gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 1ff713cca554a73c0c24695e034f8bdd953fee65 Mon Sep 17 00:00:00 2001 From: takanakahiko Date: Tue, 19 Sep 2023 21:18:38 +0900 Subject: [PATCH 5/5] add error handling when receiving datetime type --- lib/parser.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/parser.go b/lib/parser.go index 14f7ad6..b7326ee 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/pkg/errors" "github.com/spf13/afero" @@ -409,6 +410,9 @@ func convertToParams(p interface{}) (map[string]interface{}, error) { } params[key] = append(params[key].([]map[string]interface{}), p) } + case time.Time: + // yaml.v3 からは日付型がサポートされるが、json の値を比較する atgen の性質上、日時は文字列のまま扱う方が都合が良い + return params, errors.New("atgen does not support datetime type yaml input. Please enclose the datetime in double quotes and enter it as a string.") default: return params, errors.New("invalid type") }