Skip to content

Commit

Permalink
Fix: remove empty row
Browse files Browse the repository at this point in the history
  • Loading branch information
markruler committed Mar 30, 2021
1 parent ac56c85 commit 5fd56c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
10 changes: 5 additions & 5 deletions pkg/excel/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ func (xl *Excel) setAPISheetRequest(operation *spec.Operation) {

xl.setCellWithSchema(param.Name, param.In, param.Type, param.Description)

if param.Schema != nil {
xl.parameterSchema(param)
continue
}

if param.Items != nil && param.Items.Enum != nil {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "E", xl.Context.row), enum2string(param.Items.Enum...))
}

if param.Enum != nil {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "E", xl.Context.row), enum2string(param.Enum...))
}

if param.Schema != nil {
xl.parameterSchema(param)
}

xl.Context.row++
}
xl.Context.row++
Expand Down
22 changes: 12 additions & 10 deletions pkg/excel/api_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
// @path /containers/json
func TestParameterWithoutSchema(t *testing.T) {
xl := New()
var err error
err = xl.createAPISheet("", "", &spec.Operation{
err := xl.createAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -43,7 +42,6 @@ func TestParameterWithoutSchema(t *testing.T) {

func TestParameterSchemaWithRef(t *testing.T) {
xl := New()
var err error
xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
// @source zoom.us.json
Expand Down Expand Up @@ -92,7 +90,7 @@ func TestParameterSchemaWithRef(t *testing.T) {
},
},
}
err = xl.createAPISheet("", "", &spec.Operation{
err := xl.createAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -155,6 +153,8 @@ func TestParameterSchemaWithRef(t *testing.T) {
assert.NoError(t, err)
row, err := xl.File.GetRows("1")
assert.NoError(t, err)

// t.Log(row)
assert.Equal(t, "X", row[12][0])
assert.Equal(t, "page_size", row[12][1])
assert.Equal(t, "query", row[12][2])
Expand All @@ -179,18 +179,22 @@ func TestParameterSchemaWithRef(t *testing.T) {
assert.Equal(t, "string", row[15][3])
assert.Equal(t, "", row[15][6])

// @source docker.v1.41.json
// @method POST
// @path /containers/create
// TODO: AllOf

// TODO: ExtraProps
// assert.Equal(t, "recover meeting recording", row[15][6])
}

func TestParameterSchemaWithoutRef(t *testing.T) {
xl := New()
var err error
var row [][]string
// @source docker.v1.41.json
// @method POST
// @path /build
err = xl.createAPISheet("", "", &spec.Operation{
err := xl.createAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -259,7 +263,6 @@ func TestParameterSchemaWithoutRef(t *testing.T) {
// @path /user/createWithList
func TestParameterSchemaItemsWithRef(t *testing.T) {
xl := New()
var err error
xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Definitions: spec.Definitions{
Expand All @@ -286,7 +289,7 @@ func TestParameterSchemaItemsWithRef(t *testing.T) {
},
},
}
err = xl.createAPISheet("", "", &spec.Operation{
err := xl.createAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Tags: []string{"user"},
Summary: "Creates list of users with given input array",
Expand Down Expand Up @@ -330,8 +333,7 @@ func TestParameterSchemaItemsWithRef(t *testing.T) {
// @path /pet/findByStatus
func TestParameterSchemaItemsWithoutRef(t *testing.T) {
xl := New()
var err error
err = xl.createAPISheet("", "", &spec.Operation{
err := xl.createAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down
46 changes: 16 additions & 30 deletions pkg/excel/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ func (xl *Excel) setCellWithSchema(schemaName, paramType, dataType, description
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), schemaName)
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "C", xl.Context.row), paramType)
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), dataType)
// FIXME:
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "F", xl.Context.row), example)
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "G", xl.Context.row), description)
}

func (xl *Excel) parameterSchema(param spec.Parameter) error {
// TODO: write test code
if param.Schema.Items != nil {
if param.Schema.Items.Schema != nil {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), strings.Join(param.Schema.Type, ","))
}
// TODO: Schema's'
if param.Schema.Items.Schemas != nil {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), strings.Join(param.Schema.Type, ","))
}
}
// FIXME:
// if param.Schema.Items != nil {
// if param.Schema.Items.Schema != nil {
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), strings.Join(param.Schema.Type, ","))
// }
// if param.Schema.Items.Schemas != nil {
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), strings.Join(param.Schema.Type, ","))
// }
// }

if !reflect.DeepEqual(spec.Ref{}, param.Schema.Ref) {
if err := xl.parameterSchemaRef(param); err != nil {
Expand All @@ -94,6 +94,7 @@ func (xl *Excel) parameterSchema(param spec.Parameter) error {
for k, v := range param.Schema.Properties {
xl.setCellWithSchema(k, param.In, strings.Join(v.Type, ","), "")
}
return nil
}

if param.Schema.Type != nil {
Expand All @@ -117,7 +118,7 @@ func (xl *Excel) parameterSchemaRef(param spec.Parameter) error {

schemaName, _ := xl.definitionFromRef(param.Schema.Ref)
xl.setCellWithSchema(schemaName, param.In, strings.Join(schema.Type, ","), param.Description)
xl.Context.row++
return nil
}

if strings.Contains(param.Schema.Ref.GetPointer().String(), "parameters") {
Expand All @@ -128,7 +129,6 @@ func (xl *Excel) parameterSchemaRef(param spec.Parameter) error {
xl.checkRequired(schema.Required)

xl.setCellWithSchema(schema.Name, schema.In, schema.Type, schema.Description)
xl.Context.row++
}
return nil
}
Expand All @@ -155,7 +155,6 @@ func (xl *Excel) responseSchema(response spec.Response) error {
return nil
}

// TODO: write test code
if response.Schema.Properties != nil {
if err := xl.propDefinitionFromSchemaRef(response); err != nil {
return err
Expand Down Expand Up @@ -185,6 +184,9 @@ func (xl *Excel) arrayDefinitionFromSchemaRef(response spec.Response) error {
}

func (xl *Excel) propDefinitionFromSchemaRef(response spec.Response) error {
if reflect.DeepEqual(spec.Response{}, response) {
return errors.New("response is empty")
}
for propertyName, propertySchema := range response.Schema.Properties {
if !reflect.DeepEqual(spec.Ref{}, propertySchema.Ref) {
definitionName, definition := xl.definitionFromRef(propertySchema.Ref)
Expand Down Expand Up @@ -215,23 +217,7 @@ func (xl *Excel) responseSchemaRef(response spec.Response) error {
if schema == nil {
return errors.New("not found response.Schema.Ref definition")
}
// TODO: handle 'AllOf'
// if len(schema.AllOf) != 0 {
// for _, oneSchema := range schema.AllOf {
// fmt.Println("oneSchema:", oneSchema)
// schema, err := spec.ResolveResponse(xl.SwaggerSpec, oneSchema.Ref)
// if err != nil {
// return err
// }
// fmt.Println("schema:", schema)
// schemaName, _ := xl.definitionFromRef(oneSchema.Ref)
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), schemaName)
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "C", xl.Context.row), "body")
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), "object")
// xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "G", xl.Context.row), schema.Description)
// }
// return nil
// }

schemaName, _ := xl.definitionFromRef(response.Schema.Ref)
xl.setCellWithSchema(schemaName, "body", "object", response.Description)
xl.Context.row++
Expand Down
5 changes: 5 additions & 0 deletions pkg/excel/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/stretchr/testify/assert"
)

// func init() {
// xl = New()
// }

func TestSortMap(t *testing.T) {
arr := sortMap("")
assert.Nil(t, arr)
Expand Down Expand Up @@ -64,6 +68,7 @@ func TestGetDefinitionSchema(t *testing.T) {
_, def = xl.definitionFromRef(spec.MustCreateRef(""))
assert.Nil(t, def)

// FIXME:
// _, def = xl.parameterFromRef(spec.MustCreateRef("#/asd/qwe"))
// assert.Nil(t, def)

Expand Down

0 comments on commit 5fd56c6

Please sign in to comment.