Skip to content

Commit

Permalink
refactor: extract template interface
Browse files Browse the repository at this point in the history
  • Loading branch information
markruler committed Apr 1, 2021
1 parent 6deca82 commit a055cd2
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 413 deletions.
17 changes: 14 additions & 3 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"strings"

"github.com/markruler/swage/parser"
"github.com/markruler/swage/template"
Expand Down Expand Up @@ -46,13 +47,23 @@ func genRun(cmd *cobra.Command, args []string) error {
return err
}

xl := simple.New()
var tmpl template.Template

if err = xl.Generate(swaggerAPI, templateName); err != nil {
switch strings.TrimSpace(templateName) {
case template.Simple:
tmpl = simple.New()
// TODO:
// case "print":
// template = print.New()
default:
return errors.New("the template not found")
}

if err = tmpl.Generate(swaggerAPI); err != nil {
return err
}

if err := xl.File.SaveAs(outputPath); err != nil {
if err := tmpl.GetExcel().File.SaveAs(outputPath); err != nil {
return err
}

Expand Down
15 changes: 8 additions & 7 deletions template/simple/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
"github.com/go-openapi/spec"
)

func (xl *Excel) createAPISheet(path, method string, operation *spec.Operation, definitions spec.Definitions, sheetName int) (err error) {
func (simple *Simple) CreateAPISheet(path, method string, operation *spec.Operation, definitions spec.Definitions, sheetName int) (err error) {
xl := simple.xl
if operation == nil {
return errors.New("operation should not be empty")
}
xl.Context.worksheetName = strconv.Itoa(sheetName)
xl.File.NewSheet(xl.Context.worksheetName)
xl.WorkSheetName = strconv.Itoa(sheetName)
xl.File.NewSheet(xl.WorkSheetName)

xl.Context.row = 1
xl.setAPISheetHeader(path, method, operation)
xl.setAPISheetRequest(operation)
if err = xl.setAPISheetResponse(operation); err != nil {
xl.Context.Row = 1
simple.setAPISheetHeader(path, method, operation)
simple.setAPISheetRequest(operation)
if err = simple.setAPISheetResponse(operation); err != nil {
return err
}
return nil
Expand Down
97 changes: 49 additions & 48 deletions template/simple/api_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,58 @@ import (
"github.com/go-openapi/spec"
)

func (xl *Excel) setAPISheetHeader(path, method string, operation *spec.Operation) {
xl.File.SetColWidth(xl.Context.worksheetName, "A", "A", 12.0)
xl.File.SetColWidth(xl.Context.worksheetName, "B", "B", 33.0)
xl.File.SetColWidth(xl.Context.worksheetName, "C", "C", 12.0)
xl.File.SetColWidth(xl.Context.worksheetName, "D", "D", 12.0)
xl.File.SetColWidth(xl.Context.worksheetName, "E", "E", 20.0)
xl.File.SetColWidth(xl.Context.worksheetName, "F", "F", 40.0)
xl.File.SetColWidth(xl.Context.worksheetName, "G", "G", 90.0)
// xl.File.SetColStyle(xl.Context.worksheetName, "A:G", xl.Style.Line)
func (simple *Simple) setAPISheetHeader(path, method string, operation *spec.Operation) {
xl := simple.xl
xl.File.SetColWidth(xl.WorkSheetName, "A", "A", 12.0)
xl.File.SetColWidth(xl.WorkSheetName, "B", "B", 33.0)
xl.File.SetColWidth(xl.WorkSheetName, "C", "C", 12.0)
xl.File.SetColWidth(xl.WorkSheetName, "D", "D", 12.0)
xl.File.SetColWidth(xl.WorkSheetName, "E", "E", 20.0)
xl.File.SetColWidth(xl.WorkSheetName, "F", "F", 40.0)
xl.File.SetColWidth(xl.WorkSheetName, "G", "G", 90.0)
// xl.File.SetColStyle(xl.WorkSheetName, "A:G", xl.Style.Line)

xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStyle(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "A", xl.Context.row), xl.Style.Button)
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Back to Index")
xl.File.SetCellHyperLink(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "INDEX!A1", "Location")
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Tag")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "A", xl.Context.Row), xl.Style.Button)
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Back to Index")
xl.File.SetCellHyperLink(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "INDEX!A1", "Location")
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Tag")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
if len(operation.Tags) > 0 {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), operation.Tags[0])
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), operation.Tags[0])
}
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "ID")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), operation.ID)
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Path")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), path)
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Method")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), method)
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Consumes")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), strings.Join(operation.Consumes, ", "))
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Produces")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), strings.Join(operation.Produces, ", "))
xl.Context.row++
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Summary")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), operation.Summary)
xl.Context.row++
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "ID")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), operation.ID)
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Path")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), path)
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Method")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), method)
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Consumes")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), strings.Join(operation.Consumes, ", "))
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Produces")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), strings.Join(operation.Produces, ", "))
xl.Context.Row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Summary")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), operation.Summary)
xl.Context.Row++
// https://github.com/360EntSecGroup-Skylar/excelize/issues/573
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "Description")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), operation.Description)
xl.Context.row++
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "Description")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), operation.Description)
xl.Context.Row++

xl.File.SetCellStyle(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", 2), fmt.Sprintf("%s%d", "A", xl.Context.row-1), xl.Style.Column)
xl.Context.row++
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", 2), fmt.Sprintf("%s%d", "A", xl.Context.Row-1), xl.Style.Column)
xl.Context.Row++
}
51 changes: 26 additions & 25 deletions template/simple/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,48 @@ import (
"github.com/markruler/swage/parser"
)

func (xl *Excel) setAPISheetRequest(operation *spec.Operation) {
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "REQUEST")
xl.File.MergeCell(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row))
xl.File.SetRowHeight(xl.Context.worksheetName, xl.Context.row, 20.0)
xl.File.SetCellStyle(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row), xl.Style.Title)
xl.Context.row++

xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), "required")
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "B", xl.Context.row), "schema")
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "C", xl.Context.row), "param-type")
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "D", xl.Context.row), "data-type")
xl.File.SetCellStr(xl.Context.worksheetName, fmt.Sprintf("%s%d", "E", xl.Context.row), "enum")
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")
xl.File.SetCellStyle(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "G", xl.Context.row), xl.Style.Column)
xl.Context.row++
func (simple *Simple) setAPISheetRequest(operation *spec.Operation) {
xl := simple.xl
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "REQUEST")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
xl.File.SetRowHeight(xl.WorkSheetName, xl.Context.Row, 20.0)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row), xl.Style.Title)
xl.Context.Row++

xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "required")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "B", xl.Context.Row), "schema")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "C", xl.Context.Row), "param-type")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "D", xl.Context.Row), "data-type")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "E", xl.Context.Row), "enum")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "F", xl.Context.Row), "example")
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "G", xl.Context.Row), "description")
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row), xl.Style.Column)
xl.Context.Row++

for _, param := range operation.Parameters {
xl.File.SetCellStyle(xl.Context.worksheetName, fmt.Sprintf("%s%d", "A", xl.Context.row), fmt.Sprintf("%s%d", "F", xl.Context.row), xl.Style.Center)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "F", xl.Context.Row), xl.Style.Center)

if !reflect.DeepEqual(param.Ref, spec.Ref{}) {
param = *xl.parameterFromRef(param.Ref)
param = *simple.parameterFromRef(param.Ref)
}

xl.checkRequired(param.Required)
simple.checkRequired(param.Required)

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

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

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

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

xl.Context.row++
xl.Context.Row++
}
xl.Context.row++
xl.Context.Row++
}
27 changes: 16 additions & 11 deletions template/simple/api_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
// @method GET
// @path /containers/json
func TestParameterWithoutSchema(t *testing.T) {
xl := New()
err := xl.createAPISheet("", "", &spec.Operation{
simple := New()
xl := simple.GetExcel()
err := simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -42,7 +43,8 @@ func TestParameterWithoutSchema(t *testing.T) {
}

func TestParameterSchemaWithRef(t *testing.T) {
xl := New()
simple := New()
xl := simple.GetExcel()
xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
// @source zoom.us.json
Expand Down Expand Up @@ -91,7 +93,7 @@ func TestParameterSchemaWithRef(t *testing.T) {
},
},
}
err := xl.createAPISheet("", "", &spec.Operation{
err := simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -191,12 +193,13 @@ func TestParameterSchemaWithRef(t *testing.T) {
}

func TestParameterSchemaWithoutRef(t *testing.T) {
xl := New()
simple := New()
xl := simple.GetExcel()
var row [][]string
// @source docker.v1.41.json
// @method POST
// @path /build
err := xl.createAPISheet("", "", &spec.Operation{
err := simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -232,7 +235,7 @@ func TestParameterSchemaWithoutRef(t *testing.T) {
// @source zoom.us.json
// @method POST
// @path /groups
err = xl.createAPISheet("", "", &spec.Operation{
err = simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down Expand Up @@ -266,7 +269,8 @@ func TestParameterSchemaWithoutRef(t *testing.T) {
// @method POST
// @path /user/createWithList
func TestParameterSchemaItemsWithRef(t *testing.T) {
xl := New()
simple := New()
xl := simple.GetExcel()
xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Definitions: spec.Definitions{
Expand All @@ -293,7 +297,7 @@ func TestParameterSchemaItemsWithRef(t *testing.T) {
},
},
}
err := xl.createAPISheet("", "", &spec.Operation{
err := simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Tags: []string{"user"},
Summary: "Creates list of users with given input array",
Expand Down Expand Up @@ -337,8 +341,9 @@ func TestParameterSchemaItemsWithRef(t *testing.T) {
// @method GET
// @path /pet/findByStatus
func TestParameterSchemaItemsWithoutRef(t *testing.T) {
xl := New()
err := xl.createAPISheet("", "", &spec.Operation{
simple := New()
xl := simple.GetExcel()
err := simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
Expand Down
Loading

0 comments on commit a055cd2

Please sign in to comment.