Skip to content

Commit

Permalink
refactor: Removed unused StandardController
Browse files Browse the repository at this point in the history
  • Loading branch information
akeemphilbert committed Feb 4, 2022
1 parent b332981 commit d82e22b
Showing 1 changed file with 0 additions and 237 deletions.
237 changes: 0 additions & 237 deletions controllers/rest/middleware_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,240 +208,3 @@ func addRelations(struc ds.Builder, relations map[string]string, structs map[str
}
return struc, nil
}

//AddStandardController adds controller to the path
func AddStandardController(e *echo.Echo, pathData *openapi3.PathItem, method string, swagger *openapi3.Swagger, operationConfig *PathConfig) (bool, error) {
autoConfigure := false
switch strings.ToUpper(method) {
case "POST":
if pathData.Post.RequestBody == nil {
e.Logger.Warnf("unexpected error: expected request body but got nil")
break
}
//check to see if the path can be autoconfigured. If not show a warning to the developer is made aware
for _, value := range pathData.Post.RequestBody.Value.Content {
if strings.Contains(value.Schema.Ref, "#/components/schemas/") {

operationConfig.Handler = "CreateHandler"
autoConfigure = true
} else if value.Schema.Value.Type == "array" && value.Schema.Value.Items != nil && strings.Contains(value.Schema.Value.Items.Ref, "#/components/schemas/") {

for _, compare := range pathData.Post.RequestBody.Value.Content {
if compare.Schema.Value.Items.Ref != value.Schema.Value.Items.Ref {
e.Logger.Warnf("unexpected error: cannot assign different schemas for different content types")
return autoConfigure, nil
}
}

operationConfig.Handler = "CreateBatch"
autoConfigure = true

}
}
case "PUT":
allParam := true
if pathData.Put.RequestBody == nil {
break
}
//check to see if the path can be autoconfigured. If not show a warning to the developer is made aware
for _, value := range pathData.Put.RequestBody.Value.Content {
if strings.Contains(value.Schema.Ref, "#/components/schemas/") {
var identifiers []string
identifierExtension := swagger.Components.Schemas[strings.Replace(value.Schema.Ref, "#/components/schemas/", "", -1)].Value.ExtensionProps.Extensions[IdentifierExtension]
if identifierExtension != nil {
bytesId := identifierExtension.(json.RawMessage)
json.Unmarshal(bytesId, &identifiers)
}
var contextName string
//check for identifiers
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
foundIdentifier := false
//check the parameters for the identifiers
for _, param := range pathData.Put.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
foundIdentifier = true
break
}
}
if !foundIdentifier {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
if allParam {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
}
//if there is no identifiers then id is the default identifier
for _, param := range pathData.Put.Parameters {

if "id" == param.Value.Name {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
interfaceContext := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if interfaceContext != nil {
bytesContext := interfaceContext.(json.RawMessage)
json.Unmarshal(bytesContext, &contextName)
if "id" == contextName {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
}
}
}
}

case "PATCH":
allParam := true
if pathData.Patch.RequestBody == nil {
break
}
//check to see if the path can be autoconfigured. If not show a warning to the developer is made aware
for _, value := range pathData.Patch.RequestBody.Value.Content {
if strings.Contains(value.Schema.Ref, "#/components/schemas/") {
var identifiers []string
identifierExtension := swagger.Components.Schemas[strings.Replace(value.Schema.Ref, "#/components/schemas/", "", -1)].Value.ExtensionProps.Extensions[IdentifierExtension]
if identifierExtension != nil {
bytesId := identifierExtension.(json.RawMessage)
json.Unmarshal(bytesId, &identifiers)
}
var contextName string
//check for identifiers
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
//check the parameters for the identifiers
for _, param := range pathData.Patch.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
break
}
if !(identifier == param.Value.Name) && !(cName != nil && identifier == cName.(string)) {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
}
if allParam {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
}
//if there is no identifiers then id is the default identifier
for _, param := range pathData.Patch.Parameters {

if "id" == param.Value.Name {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
interfaceContext := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if interfaceContext != nil {
bytesContext := interfaceContext.(json.RawMessage)
json.Unmarshal(bytesContext, &contextName)
if "id" == contextName {
operationConfig.Handler = "Update"
autoConfigure = true
break
}
}
}
}
}
case "GET":
allParam := true
//check to see if the path can be autoconfigured. If not show a warning to the developer is made aware
//checks if the response refers to a schema
if pathData.Get.Responses != nil && pathData.Get.Responses["200"].Value.Content != nil {
for _, val := range pathData.Get.Responses["200"].Value.Content {
if strings.Contains(val.Schema.Ref, "#/components/schemas/") {
var identifiers []string
identifierExtension := swagger.Components.Schemas[strings.Replace(val.Schema.Ref, "#/components/schemas/", "", -1)].Value.ExtensionProps.Extensions[IdentifierExtension]
if identifierExtension != nil {
bytesId := identifierExtension.(json.RawMessage)
err := json.Unmarshal(bytesId, &identifiers)
if err != nil {
return autoConfigure, err
}
}
var contextName string
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
foundIdentifier := false
//check the parameters
for _, param := range pathData.Get.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
foundIdentifier = true
break
}
}
if !foundIdentifier {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
}
//check the parameters for id
if pathData.Get.Parameters != nil && len(pathData.Get.Parameters) != 0 {
for _, param := range pathData.Get.Parameters {
if "id" == param.Value.Name {
allParam = true
}
contextInterface := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if contextInterface != nil {
bytesContext := contextInterface.(json.RawMessage)
json.Unmarshal(bytesContext, &contextName)
if "id" == contextName {
allParam = true
}
}
}
}
if allParam {
operationConfig.Handler = "View"
autoConfigure = true
break
}
}
//checks if the response refers to an array schema
if val.Schema.Value.Properties != nil && val.Schema.Value.Properties["items"] != nil && val.Schema.Value.Properties["items"].Value.Type == "array" && val.Schema.Value.Properties["items"].Value.Items != nil && strings.Contains(val.Schema.Value.Properties["items"].Value.Items.Ref, "#/components/schemas/") {
operationConfig.Handler = "List"
autoConfigure = true
break
}
if val.Schema.Value.Properties != nil {
var alias string
for _, prop := range val.Schema.Value.Properties {
aliasInterface := prop.Value.ExtensionProps.Extensions[AliasExtension]
if aliasInterface != nil {
bytesContext := aliasInterface.(json.RawMessage)
json.Unmarshal(bytesContext, &alias)
if alias == "items" {
if prop.Value.Type == "array" && prop.Value.Items != nil && strings.Contains(prop.Value.Items.Ref, "#/components/schemas/") {
operationConfig.Handler = "List"
autoConfigure = true
break
}
}
}

}
}

}
}
}

return autoConfigure, nil
}

0 comments on commit d82e22b

Please sign in to comment.