From 4d92eeffaba1288a852d80b268879be24a5f8e23 Mon Sep 17 00:00:00 2001 From: Sergiy Misyura Date: Fri, 4 Aug 2017 21:15:17 +0000 Subject: [PATCH 1/2] Accepts x-order in the operation object; sorts operation by either order by name --- spec/spec.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/spec.go b/spec/spec.go index b88cde0..cc3e1cb 100644 --- a/spec/spec.go +++ b/spec/spec.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "regexp" + "sort" "strings" "github.com/dapperdox/dapperdox/config" @@ -110,6 +111,7 @@ type Method struct { OperationName string NavigationName string Path string + Order string Consumes []string Produces []string PathParams []Parameter @@ -181,6 +183,12 @@ type Header struct { Enum []string } +type ByOrderString []Method + +func (a ByOrderString) Len() int { return len(a) } +func (a ByOrderString) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByOrderString) Less(i, j int) bool { return a[i].Order < a[j].Order } + // ----------------------------------------------------------------------------- func LoadSpecifications(specHost string, collapse bool) error { @@ -349,12 +357,14 @@ func (c *APISpecification) Load(specLocation string, specHost string) error { // If API was populated (will not be if tags do not match), add to set if !groupingByTag && len(api.Methods) > 0 { logger.Tracef(nil, " + Adding %s\n", name) + sort.Sort(ByOrderString(api.Methods)) c.APIs = append(c.APIs, *api) // All APIs (versioned within) } } if groupingByTag && len(api.Methods) > 0 { logger.Tracef(nil, " + Adding %s\n", name) + sort.Sort(ByOrderString(api.Methods)) c.APIs = append(c.APIs, *api) // All APIs (versioned within) } } @@ -567,6 +577,11 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem, navigationName = o.Summary } + order := o.Summary + if o.Extensions["x-order"] != nil { + order = "!" + o.Extensions["x-order"].(string) + } + method := &Method{ ID: CamelToKebab(id), Name: o.Summary, @@ -576,6 +591,7 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem, Responses: make(map[int]Response), NavigationName: navigationName, OperationName: operationName, + Order: order, APIGroup: api, } if len(o.Consumes) > 0 { From 7987a3ed5f4164d85013921b0ea7ad0c325d8309 Mon Sep 17 00:00:00 2001 From: Sergiy Misyura Date: Mon, 7 Aug 2017 15:10:20 +0000 Subject: [PATCH 2/2] Renamed names of the Swagger sorting extension and variables to be consistent with gudes' SortOrder meta field --- spec/spec.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/spec.go b/spec/spec.go index cc3e1cb..e2f019e 100644 --- a/spec/spec.go +++ b/spec/spec.go @@ -111,7 +111,7 @@ type Method struct { OperationName string NavigationName string Path string - Order string + SortOrder string Consumes []string Produces []string PathParams []Parameter @@ -183,11 +183,11 @@ type Header struct { Enum []string } -type ByOrderString []Method +type BySortOrder []Method -func (a ByOrderString) Len() int { return len(a) } -func (a ByOrderString) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByOrderString) Less(i, j int) bool { return a[i].Order < a[j].Order } +func (a BySortOrder) Len() int { return len(a) } +func (a BySortOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a BySortOrder) Less(i, j int) bool { return a[i].SortOrder < a[j].SortOrder } // ----------------------------------------------------------------------------- @@ -357,14 +357,14 @@ func (c *APISpecification) Load(specLocation string, specHost string) error { // If API was populated (will not be if tags do not match), add to set if !groupingByTag && len(api.Methods) > 0 { logger.Tracef(nil, " + Adding %s\n", name) - sort.Sort(ByOrderString(api.Methods)) + sort.Sort(BySortOrder(api.Methods)) c.APIs = append(c.APIs, *api) // All APIs (versioned within) } } if groupingByTag && len(api.Methods) > 0 { logger.Tracef(nil, " + Adding %s\n", name) - sort.Sort(ByOrderString(api.Methods)) + sort.Sort(BySortOrder(api.Methods)) c.APIs = append(c.APIs, *api) // All APIs (versioned within) } } @@ -577,9 +577,9 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem, navigationName = o.Summary } - order := o.Summary - if o.Extensions["x-order"] != nil { - order = "!" + o.Extensions["x-order"].(string) + sortOrder := o.Summary + if o.Extensions["x-sortOrder"] != nil { + sortOrder = "!" + o.Extensions["x-sortOrder"].(string) } method := &Method{ @@ -591,7 +591,7 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem, Responses: make(map[int]Response), NavigationName: navigationName, OperationName: operationName, - Order: order, + SortOrder: sortOrder, APIGroup: api, } if len(o.Consumes) > 0 {