From 0091bacca09e4af9964d9c87fb80b1ad0155c91f Mon Sep 17 00:00:00 2001 From: Alexander Staubo Date: Thu, 4 Oct 2018 16:31:28 -0400 Subject: [PATCH] Sort definitions by name for consistency. --- pkg/generator/generate.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pkg/generator/generate.go b/pkg/generator/generate.go index f0a050da..ec98b2e1 100644 --- a/pkg/generator/generate.go +++ b/pkg/generator/generate.go @@ -229,7 +229,8 @@ func (g *schemaGenerator) generateRootType() error { } if g.schema.Type.Type == "" { - for name, def := range g.schema.Definitions { + for _, name := range sortDefinitionsByName(g.schema.Definitions) { + def := g.schema.Definitions[name] _, err := g.generateDeclaredType(def, newNameScope(g.identifierize(name))) if err != nil { return err @@ -475,21 +476,15 @@ func (g *schemaGenerator) generateStructType( }, nil } - propNames := make([]string, 0, len(t.Properties)) - for name := range t.Properties { - propNames = append(propNames, name) - } - sort.Strings(propNames) - requiredNames := make(map[string]bool, len(t.Properties)) for _, r := range t.Required { requiredNames[r] = true } - uniqueNames := make(map[string]int, len(propNames)) + uniqueNames := make(map[string]int, len(t.Properties)) var structType codegen.StructType - for _, name := range propNames { + for _, name := range sortPropertiesByName(t.Properties) { prop := t.Properties[name] isRequired := requiredNames[name] @@ -760,3 +755,21 @@ var ( varNamePlainStruct = "plain" varNameRawMap = "raw" ) + +func sortPropertiesByName(props map[string]*schemas.Type) []string { + names := make([]string, 0, len(props)) + for name := range props { + names = append(names, name) + } + sort.Strings(names) + return names +} + +func sortDefinitionsByName(defs schemas.Definitions) []string { + names := make([]string, 0, len(defs)) + for name := range defs { + names = append(names, name) + } + sort.Strings(names) + return names +}