From 4606715f5a95aa06106cff2438ed76fd209f97e1 Mon Sep 17 00:00:00 2001 From: James Pogran Date: Mon, 4 Oct 2021 09:56:01 -0400 Subject: [PATCH] sort --- decoder/label_candidates.go | 8 ++++++-- schema/body_schema.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/decoder/label_candidates.go b/decoder/label_candidates.go index e4d22b32..78a1af29 100644 --- a/decoder/label_candidates.go +++ b/decoder/label_candidates.go @@ -115,7 +115,9 @@ func generateRequiredFieldsSnippet(label string, bodySchema *schema.BodySchema, snippetText += " {\n" } - for attrName, attr := range bodySchema.Attributes { + attrNames := bodySchema.SortAttributes() + for _, attrName := range attrNames { + attr := bodySchema.Attributes[attrName] if attr.IsRequired { valueSnippet := snippetForExprContraint(uint(placeholder), attr.Expr) snippetText += fmt.Sprintf("%s%s = %s\n", indent, attrName, valueSnippet) @@ -123,7 +125,9 @@ func generateRequiredFieldsSnippet(label string, bodySchema *schema.BodySchema, } } - for blockName, blockSchema := range bodySchema.Blocks { + blockNames := bodySchema.SortBlocks() + for _, blockName := range blockNames { + blockSchema := bodySchema.Blocks[blockName] if blockSchema.MinItems > 0 { snippetText += fmt.Sprintf("%s%s {\n", indent, blockName) snippetText += generateRequiredFieldsSnippet("", blockSchema.Body, blockSchema.Labels, placeholder, indentCount+1) diff --git a/schema/body_schema.go b/schema/body_schema.go index 8a79cf70..fdc72f54 100644 --- a/schema/body_schema.go +++ b/schema/body_schema.go @@ -2,6 +2,7 @@ package schema import ( "fmt" + "sort" "github.com/hashicorp/go-multierror" "github.com/hashicorp/hcl-lang/lang" @@ -50,6 +51,24 @@ func NewBodySchema() *BodySchema { } } +func (as *BodySchema) SortAttributes() []string { + keys := make([]string, 0, len(as.Attributes)) + for k := range as.Attributes { + keys = append(keys, k) + } + sort.Strings(keys) + return keys +} + +func (as *BodySchema) SortBlocks() []string { + keys := make([]string, 0, len(as.Blocks)) + for k := range as.Blocks { + keys = append(keys, k) + } + sort.Strings(keys) + return keys +} + func (bs *BodySchema) Validate() error { if len(bs.Attributes) > 0 && bs.AnyAttribute != nil { return fmt.Errorf("one of Attributes or AnyAttribute must be set, not both")