func PrintSortedAttrTxt()

in rules/helper.go [47:78]


func PrintSortedAttrTxt(src []byte, attr *hclsyntax.Attribute) (string, bool) {
	isSorted := true
	exp, isMap := attr.Expr.(*hclsyntax.ObjectConsExpr)
	if !isMap {
		return string(attr.Range().SliceBytes(src)), isSorted
	}
	var keys []string
	object := make(map[string]string)
	for _, item := range exp.Items {
		key := string(item.KeyExpr.Range().SliceBytes(src))
		value := fmt.Sprintf("%s = %s", key, string(item.ValueExpr.Range().SliceBytes(src)))
		keys = append(keys, key)
		object[key] = value
	}
	isSorted = sort.StringsAreSorted(keys)
	if !isSorted {
		sort.Strings(keys)
	}
	var objectAttrs []string
	for _, key := range keys {
		objectAttrs = append(objectAttrs, object[key])
	}
	sortedExpTxt := strings.Join(objectAttrs, "\n")
	var sortedAttrTxt string
	if RemoveSpaceAndLine(sortedExpTxt) == "" {
		sortedAttrTxt = fmt.Sprintf("%s = {}", attr.Name)
	} else {
		sortedAttrTxt = fmt.Sprintf("%s = {\n%s\n}", attr.Name, sortedExpTxt)
	}
	formattedTxt := string(hclwrite.Format([]byte(sortedAttrTxt)))
	return formattedTxt, isSorted
}