Skip to content

Commit

Permalink
feat: Extract prints into separate package. Add more output
Browse files Browse the repository at this point in the history
  • Loading branch information
idsulik committed Sep 17, 2024
1 parent 6cb6d21 commit 891861e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 38 deletions.
41 changes: 41 additions & 0 deletions internal/printer/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package printer

import (
"fmt"
"maps"
"slices"
"strings"

"github.com/getkin/kin-openapi/openapi3"
)

func getProperties(value *openapi3.Schema) []string {
properties := make([]string, 0)

if value.Properties != nil {
sortedPropertiesName := slices.Sorted(maps.Keys(value.Properties))
for _, propertyName := range sortedPropertiesName {
prop := value.Properties[propertyName]
propertyName = enrichPropertyName(propertyName, prop)
properties = append(properties, propertyName)
}
}

return properties
}

func enrichPropertyName(propertyName string, prop *openapi3.SchemaRef) string {
if prop == nil || prop.Value == nil {
return propertyName
}

if prop.Value.Type != nil {
if prop.Value.Format == "" {
propertyName += fmt.Sprintf(" (%s)", strings.Join(prop.Value.Type.Slice(), ", "))
} else {
propertyName += fmt.Sprintf(" (%s: %s)", strings.Join(prop.Value.Type.Slice(), ", "), prop.Value.Format)
}
}

return propertyName
}
33 changes: 33 additions & 0 deletions internal/printer/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package printer

import (
"fmt"
"os"

"github.com/getkin/kin-openapi/openapi3"
"github.com/olekukonko/tablewriter"
)

func PrintParameters(operation *openapi3.Operation) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetRowLine(true)
table.SetHeader([]string{"In", "Parameter", "Type", "Required", "Description"})
for _, p := range operation.Parameters {
value := p.Value
description := "-"
if value.Description != "" {
description = value.Description
}
table.Append(
[]string{
value.In,
value.Name,
enrichPropertyName(value.Name, value.Schema),
fmt.Sprintf("%v", value.Required),
description,
},
)
}
table.Render()
}
25 changes: 25 additions & 0 deletions internal/printer/request_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package printer

import (
"os"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/olekukonko/tablewriter"
)

func PrintRequestBody(operation *openapi3.Operation) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Type", "Properties"})
propertiesToContentTypes := make(map[string][]string)
for contentType, content := range operation.RequestBody.Value.Content {
properties := strings.Join(getProperties(content.Schema.Value), "\n")
propertiesToContentTypes[properties] = append(propertiesToContentTypes[properties], contentType)
}

for properties, contentTypes := range propertiesToContentTypes {
table.Append([]string{strings.Join(contentTypes, "\n"), properties})
}
table.Render()
}
44 changes: 44 additions & 0 deletions internal/printer/responses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package printer

import (
"fmt"
"maps"
"os"
"slices"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/olekukonko/tablewriter"
)

func PrintResponses(operation *openapi3.Operation) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetRowLine(true)
table.SetHeader([]string{"Name", "Content", "Description"})
sortedCodes := slices.Sorted(maps.Keys(operation.Responses.Map()))
for _, code := range sortedCodes {
response := operation.Responses.Value(code)
content := "-"
if response.Value.Content != nil {
propertiesToContentTypes := make(map[string][]string)
for contentType := range response.Value.Content {
properties := strings.Join(getProperties(response.Value.Content[contentType].Schema.Value), "\n")
propertiesToContentTypes[properties] = append(propertiesToContentTypes[properties], contentType)
}

for properties, contentTypes := range propertiesToContentTypes {
content = fmt.Sprintf("types:\n%s\n\nproperties:\n%s", strings.Join(contentTypes, "\n"), properties)
}
}

description := "-"
if response.Value.Description != nil {
description = *response.Value.Description
}

table.Append([]string{code, content, description})
}

table.Render()
}
42 changes: 4 additions & 38 deletions internal/swagger/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/getkin/kin-openapi/openapi3"
converter2 "github.com/idsulik/swama/internal/converter"
"github.com/idsulik/swama/internal/printer"
"github.com/olekukonko/tablewriter"
)

Expand Down Expand Up @@ -144,52 +145,17 @@ func (e *endpoints) ViewEndpoint(method, endpoint string) error {

if len(operation.Parameters) > 0 {
fmt.Println("Parameters:")

table = tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Parameter", "Type", "Required", "Description"})
for _, p := range operation.Parameters {
value := p.Value
table.Append([]string{value.Name, value.In, fmt.Sprintf("%v", value.Required), value.Description})
}
table.Render()
printer.PrintParameters(operation)
}

if operation.RequestBody != nil {
fmt.Println("Request Body:")

table = tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Type", "Description"})
for contentType, content := range operation.RequestBody.Value.Content {
table.Append([]string{contentType, content.Schema.Value.Description})
}
table.Render()
printer.PrintRequestBody(operation)
}

if operation.Responses != nil {
fmt.Println("Responses:")

table = tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Response", "Types", "Description"})
for code, response := range operation.Responses.Map() {
value := response.Value
var contentTypes, description string

if value.Description != nil {
description = *value.Description
}

if value.Content != nil {
for contentType := range value.Content {
contentTypes += contentType + " "
}
}

table.Append([]string{code, contentTypes, description})
}
table.Render()
printer.PrintResponses(operation)
}

return nil
Expand Down

0 comments on commit 891861e

Please sign in to comment.