-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from markruler/add/swage-spec
add: Swage spec
- Loading branch information
Showing
25 changed files
with
2,072 additions
and
2,028 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,104 @@ | ||
package parser | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/go-openapi/loads" | ||
"github.com/go-openapi/spec" | ||
oas "github.com/go-openapi/spec" | ||
) | ||
|
||
// Parser ... | ||
type Parser struct { | ||
sourcePath string | ||
// Parse `JSON`, `YAML` to Go struct | ||
func Parse(sourcePath string) (*oas.Swagger, error) { | ||
doc, err := loads.Spec(sourcePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return doc.Spec(), nil | ||
} | ||
|
||
// New returns a Parser instance with a resource path | ||
func New(path string) *Parser { | ||
return &Parser{ | ||
sourcePath: path, | ||
// Convert Open API Spec to Swage Spec | ||
func Convert(swagger *oas.Swagger) (*SwageSpec, error) { | ||
swageSpec := &SwageSpec{} | ||
paths := SortMap(swagger.Paths.Paths) | ||
for _, path := range paths { | ||
operations := swagger.Paths.Paths[path] | ||
if operations.PathItemProps.Get != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodGet, operations.PathItemProps.Get) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Put != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodPut, operations.PathItemProps.Put) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Post != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodPost, operations.PathItemProps.Post) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Delete != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodDelete, operations.PathItemProps.Delete) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Options != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodOptions, operations.PathItemProps.Options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Head != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodHead, operations.PathItemProps.Head) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
if operations.PathItemProps.Patch != nil { | ||
swageAPI, err := extractOperation(swagger, path, http.MethodPatch, operations.PathItemProps.Patch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
swageSpec.API = append(swageSpec.API, *swageAPI) | ||
} | ||
} | ||
return swageSpec, nil | ||
} | ||
|
||
// Parse ... | ||
func (p *Parser) Parse() (*spec.Swagger, error) { | ||
doc, err := loads.Spec(p.sourcePath) | ||
if err != nil { | ||
func extractOperation(swagger *oas.Swagger, path, method string, operation *oas.Operation) (api *SwageAPI, err error) { | ||
var requests []APIRequest | ||
if requests, err = extractRequests(swagger, operation); err != nil { | ||
return nil, err | ||
} | ||
return doc.Spec(), nil | ||
|
||
var responses []APIResponse | ||
if responses, err = extractResponses(swagger, operation); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SwageAPI{ | ||
Header: APIHeader{ | ||
Tag: strings.Join(operation.Tags, ","), | ||
ID: operation.ID, | ||
Path: path, | ||
Method: method, | ||
Consumes: strings.Join(operation.Consumes, ", "), | ||
Produces: strings.Join(operation.Produces, ", "), | ||
Summary: operation.Summary, | ||
Description: operation.Description, | ||
}, | ||
Request: requests, | ||
Response: responses, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package parser | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
oas "github.com/go-openapi/spec" | ||
) | ||
|
||
// TODO: convert "#/definitions/Def" | ||
// @source editor.swagger.json | ||
// @method POST | ||
// @path /user | ||
|
||
// TODO: AllOf | ||
// @source docker.v1.41.json | ||
// @method POST | ||
// @path /containers/create | ||
func extractRequests(swagger *oas.Swagger, operation *oas.Operation) (requests []APIRequest, err error) { | ||
requests = []APIRequest{} | ||
for _, param := range operation.Parameters { | ||
request := APIRequest{} | ||
var definition_type, definition_name string | ||
if param.Schema != nil && !reflect.DeepEqual(param.Schema.Ref, oas.Ref{}) { | ||
definition_type, definition_name = DefinitionNameFromRef(param.Schema.Ref) | ||
} | ||
|
||
if definition_type == "parameters" { | ||
param = swagger.Parameters[definition_name] | ||
} | ||
|
||
if definition_type == "definitions" { | ||
definition := swagger.Definitions[definition_name] | ||
param.Name = definition_name | ||
param.In = "body" | ||
param.Type = strings.Join(definition.Type, ",") | ||
param.Example = definition.Example | ||
param.Description = definition.Description | ||
} | ||
|
||
request.Required = checkRequired(param.Required) | ||
request.Schema = param.Name | ||
request.ParameterType = param.In | ||
request.DataType = param.Type | ||
request.Description = param.Description | ||
|
||
if param.Schema != nil && param.Schema.Type != nil { | ||
request.DataType = strings.Join(param.Schema.Type, ",") | ||
if param.Schema.Description != "" { | ||
request.Description = param.Schema.Description | ||
} | ||
} | ||
|
||
if param.Items != nil && param.Items.Enum != nil { | ||
request.Enum = Enum2string(param.Items.Enum...) | ||
} | ||
|
||
if param.Enum != nil { | ||
request.Enum = Enum2string(param.Enum...) | ||
} | ||
|
||
var example string | ||
if param.Example != nil { | ||
example, err = extractExample(param.Example) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
request.Example = example | ||
requests = append(requests, request) | ||
} | ||
return requests, nil | ||
} |
Oops, something went wrong.