Skip to content

Commit

Permalink
Sort definitions by name for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Oct 4, 2018
1 parent 70bed6c commit 0091bac
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
}

0 comments on commit 0091bac

Please sign in to comment.