Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 45
maligned:
suggest-new: true
dupl:
threshold: 200
goconst:
Expand All @@ -16,7 +10,6 @@ linters-settings:
linters:
enable-all: true
disable:
- maligned
- unparam
- lll
- gochecknoinits
Expand All @@ -29,17 +22,13 @@ linters:
- wrapcheck
- testpackage
- nlreturn
- gomnd
- exhaustivestruct
- goerr113
- errorlint
- nestif
- godot
- gofumpt
- paralleltest
- tparallel
- thelper
- ifshort
- exhaustruct
- varnamelen
- gci
Expand All @@ -52,10 +41,15 @@ linters:
- forcetypeassert
- cyclop
# deprecated linters
- deadcode
- interfacer
- scopelint
- varcheck
- structcheck
- golint
- nosnakecase
#- deadcode
#- interfacer
#- scopelint
#- varcheck
#- structcheck
#- golint
#- nosnakecase
#- maligned
#- goerr113
#- ifshort
#- gomnd
#- exhaustivestruct
54 changes: 30 additions & 24 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (
"github.com/go-openapi/swag"
)

const (
allocLargeMap = 150
allocMediumMap = 64
allocSmallMap = 10
)

type referenceAnalysis struct {
schemas map[string]spec.Ref
responses map[string]spec.Ref
Expand Down Expand Up @@ -169,30 +175,30 @@ type Spec struct {
}

func (s *Spec) reset() {
s.consumes = make(map[string]struct{}, 150)
s.produces = make(map[string]struct{}, 150)
s.authSchemes = make(map[string]struct{}, 150)
s.operations = make(map[string]map[string]*spec.Operation, 150)
s.allSchemas = make(map[string]SchemaRef, 150)
s.allOfs = make(map[string]SchemaRef, 150)
s.references.schemas = make(map[string]spec.Ref, 150)
s.references.pathItems = make(map[string]spec.Ref, 150)
s.references.responses = make(map[string]spec.Ref, 150)
s.references.parameters = make(map[string]spec.Ref, 150)
s.references.items = make(map[string]spec.Ref, 150)
s.references.headerItems = make(map[string]spec.Ref, 150)
s.references.parameterItems = make(map[string]spec.Ref, 150)
s.references.allRefs = make(map[string]spec.Ref, 150)
s.patterns.parameters = make(map[string]string, 150)
s.patterns.headers = make(map[string]string, 150)
s.patterns.items = make(map[string]string, 150)
s.patterns.schemas = make(map[string]string, 150)
s.patterns.allPatterns = make(map[string]string, 150)
s.enums.parameters = make(map[string][]interface{}, 150)
s.enums.headers = make(map[string][]interface{}, 150)
s.enums.items = make(map[string][]interface{}, 150)
s.enums.schemas = make(map[string][]interface{}, 150)
s.enums.allEnums = make(map[string][]interface{}, 150)
s.consumes = make(map[string]struct{}, allocLargeMap)
s.produces = make(map[string]struct{}, allocLargeMap)
s.authSchemes = make(map[string]struct{}, allocLargeMap)
s.operations = make(map[string]map[string]*spec.Operation, allocLargeMap)
s.allSchemas = make(map[string]SchemaRef, allocLargeMap)
s.allOfs = make(map[string]SchemaRef, allocLargeMap)
s.references.schemas = make(map[string]spec.Ref, allocLargeMap)
s.references.pathItems = make(map[string]spec.Ref, allocLargeMap)
s.references.responses = make(map[string]spec.Ref, allocLargeMap)
s.references.parameters = make(map[string]spec.Ref, allocLargeMap)
s.references.items = make(map[string]spec.Ref, allocLargeMap)
s.references.headerItems = make(map[string]spec.Ref, allocLargeMap)
s.references.parameterItems = make(map[string]spec.Ref, allocLargeMap)
s.references.allRefs = make(map[string]spec.Ref, allocLargeMap)
s.patterns.parameters = make(map[string]string, allocLargeMap)
s.patterns.headers = make(map[string]string, allocLargeMap)
s.patterns.items = make(map[string]string, allocLargeMap)
s.patterns.schemas = make(map[string]string, allocLargeMap)
s.patterns.allPatterns = make(map[string]string, allocLargeMap)
s.enums.parameters = make(map[string][]interface{}, allocLargeMap)
s.enums.headers = make(map[string][]interface{}, allocLargeMap)
s.enums.items = make(map[string][]interface{}, allocLargeMap)
s.enums.schemas = make(map[string][]interface{}, allocLargeMap)
s.enums.allEnums = make(map[string][]interface{}, allocLargeMap)
}

func (s *Spec) reload() {
Expand Down
6 changes: 3 additions & 3 deletions flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type context struct {

func newContext() *context {
return &context{
newRefs: make(map[string]*newRef, 150),
newRefs: make(map[string]*newRef, allocMediumMap),
warnings: make([]string, 0),
resolved: make(map[string]string, 50),
resolved: make(map[string]string, allocMediumMap),
}
}

Expand Down Expand Up @@ -745,7 +745,7 @@ func flattenAnonPointer(key string, v SchemaRef, refsToReplace map[string]Schema
if ers != nil {
return fmt.Errorf("schema analysis [%s]: %w", key, ers)
}
callers := make([]string, 0, 64)
callers := make([]string, 0, allocMediumMap)

debugLog("looking for callers")

Expand Down
9 changes: 7 additions & 2 deletions flatten_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ func namesForOperation(parts sortref.SplitKey, operations map[string]operations.
return baseNames, startIndex
}

const (
minStartIndex = 2
minSegments = 2
)

func namesForDefinition(parts sortref.SplitKey) ([][]string, int) {
nm := parts.DefinitionName()
if nm != "" {
return [][]string{{parts.DefinitionName()}}, 2
return [][]string{{parts.DefinitionName()}}, minStartIndex
}

return [][]string{}, 0
Expand All @@ -239,7 +244,7 @@ func namesForDefinition(parts sortref.SplitKey) ([][]string, int) {
// partAdder knows how to interpret a schema when it comes to build a name from parts
func partAdder(aschema *AnalyzedSchema) sortref.PartAdder {
return func(part string) []string {
segments := make([]string, 0, 2)
segments := make([]string, 0, minSegments)

if part == "items" || part == "additionalItems" {
if aschema.IsTuple || aschema.IsTupleWithExtra {
Expand Down
9 changes: 6 additions & 3 deletions internal/flatten/replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/go-openapi/spec"
)

const definitionsPath = "#/definitions"
const (
definitionsPath = "#/definitions"
allocMediumMap = 64
)

var debugLog = debug.GetLogger("analysis/flatten/replace", os.Getenv("SWAGGER_DEBUG") != "")

Expand Down Expand Up @@ -336,8 +339,8 @@ func DeepestRef(sp *spec.Swagger, opts *spec.ExpandOptions, ref spec.Ref) (*Deep
}

currentRef := ref
visited := make(map[string]bool, 64)
warnings := make([]string, 0, 2)
visited := make(map[string]bool, allocMediumMap)
warnings := make([]string, 0)

DOWNREF:
for currentRef.String() != "" {
Expand Down
4 changes: 3 additions & 1 deletion internal/flatten/schutils/flatten_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/go-openapi/swag"
)

const allocLargeMap = 150

// Save registers a schema as an entry in spec #/definitions
func Save(sp *spec.Swagger, name string, schema *spec.Schema) {
if schema == nil {
return
}

if sp.Definitions == nil {
sp.Definitions = make(map[string]spec.Schema, 150)
sp.Definitions = make(map[string]spec.Schema, allocLargeMap)
}

sp.Definitions[name] = *schema
Expand Down
10 changes: 5 additions & 5 deletions mixin.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,23 +474,23 @@ func initPrimary(primary *spec.Swagger) {
}

if primary.Security == nil {
primary.Security = make([]map[string][]string, 0, 10)
primary.Security = make([]map[string][]string, 0, allocSmallMap)
}

if primary.Produces == nil {
primary.Produces = make([]string, 0, 10)
primary.Produces = make([]string, 0, allocSmallMap)
}

if primary.Consumes == nil {
primary.Consumes = make([]string, 0, 10)
primary.Consumes = make([]string, 0, allocSmallMap)
}

if primary.Tags == nil {
primary.Tags = make([]spec.Tag, 0, 10)
primary.Tags = make([]spec.Tag, 0, allocSmallMap)
}

if primary.Schemes == nil {
primary.Schemes = make([]string, 0, 10)
primary.Schemes = make([]string, 0, allocSmallMap)
}

if primary.Paths == nil {
Expand Down
Loading