Skip to content

Commit

Permalink
Merge pull request #48 from aktsk/update-deps
Browse files Browse the repository at this point in the history
依存関係の更新
  • Loading branch information
takanakahiko authored Sep 20, 2023
2 parents 6961df1 + 1ff713c commit f304087
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 13 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@ 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: `
Generete test code according to yaml and template.
`,
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",
},
},
}
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
27 changes: 4 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand All @@ -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=
42 changes: 18 additions & 24 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"os"
"path/filepath"
"strings"
"time"

"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
Expand Down Expand Up @@ -61,16 +62,16 @@ 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)
}
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 {
Expand Down Expand Up @@ -103,7 +104,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 {
Expand Down Expand Up @@ -253,7 +254,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{}) {
Expand Down Expand Up @@ -290,14 +291,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
Expand All @@ -314,7 +315,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 {
Expand Down Expand Up @@ -368,7 +369,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 {
Expand All @@ -390,16 +391,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
Expand All @@ -414,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")
}
Expand All @@ -425,12 +424,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")
Expand Down
2 changes: 1 addition & 1 deletion lib/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

var version = "0.7.0"
Expand All @@ -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
}

0 comments on commit f304087

Please sign in to comment.