Repository: segmentio/terraform-docs
Pull request #119 (2019-08-18 17:08:58)

Refactor Settings for better performance
### Prerequisites

Put an `x` into the box(es) that apply:

- [ ] This pull request fixes a bug.
- [ ] This pull request adds a feature.
- [x] This pull request enhances existing functionality.
- [ ] This pull request introduces breaking change.

For more information, see the [Contributing Guide](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

### Description

This PR enhances `settings.Settings` and better use of pointer to get the better performance.

### Issues Resolved

List any existing issues this pull request resolves.

### Checklist

Put an `x` into all boxes that apply:

- [x] I have read the [Contributing Guidelines](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

#### Tests

- [ ] I have added tests to cover my changes.
- [x] All tests pass when I run `make test`.

#### Documentation

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.

#### Code Style

- [x] My code follows the code style of this project.



Patch:
diff --git a/cmd/json.go b/cmd/json.go
index 12d5162..06c40ae 100644
--- a/cmd/json.go
+++ b/cmd/json.go
@@ -14,7 +14,7 @@ var jsonCmd = &cobra.Command{
 	PreRun: commandsPreRun,
 	Run: func(cmd *cobra.Command, args []string) {
 		fmt.Println(doPrint(args, func(docs *doc.Doc) (string, error) {
-			return json.Print(docs, *settings)
+			return json.Print(docs, settings)
 		}))
 	},
 }
diff --git a/cmd/markdown.go b/cmd/markdown.go
index 7251a3a..811302e 100644
--- a/cmd/markdown.go
+++ b/cmd/markdown.go
@@ -16,7 +16,7 @@ var markdownCmd = &cobra.Command{
 	PreRun:  commandsPreRun,
 	Run: func(cmd *cobra.Command, args []string) {
 		fmt.Println(doPrint(args, func(docs *doc.Doc) (string, error) {
-			return table.Print(docs, *settings)
+			return table.Print(docs, settings)
 		}))
 	},
 }
@@ -28,7 +28,7 @@ var mdTableCmd = &cobra.Command{
 	PreRun:  commandsPreRun,
 	Run: func(cmd *cobra.Command, args []string) {
 		fmt.Println(doPrint(args, func(docs *doc.Doc) (string, error) {
-			return table.Print(docs, *settings)
+			return table.Print(docs, settings)
 		}))
 	},
 }
@@ -40,7 +40,7 @@ var mdDocumentCmd = &cobra.Command{
 	PreRun:  commandsPreRun,
 	Run: func(cmd *cobra.Command, args []string) {
 		fmt.Println(doPrint(args, func(docs *doc.Doc) (string, error) {
-			return document.Print(docs, *settings)
+			return document.Print(docs, settings)
 		}))
 	},
 }
diff --git a/cmd/pretty.go b/cmd/pretty.go
index 5ad0b32..ba68c3c 100644
--- a/cmd/pretty.go
+++ b/cmd/pretty.go
@@ -14,7 +14,7 @@ var prettyCmd = &cobra.Command{
 	PreRun: commandsPreRun,
 	Run: func(cmd *cobra.Command, args []string) {
 		fmt.Println(doPrint(args, func(docs *doc.Doc) (string, error) {
-			return pretty.Print(docs, *settings)
+			return pretty.Print(docs, settings)
 		}))
 	},
 }
diff --git a/cmd/root.go b/cmd/root.go
index 0a9ebe6..d39aedc 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -28,20 +28,15 @@ var rootCmd = &cobra.Command{
 	},
 }
 
-var settings = &_settings.Settings{}
+var settings = _settings.NewSettings()
 
 func init() {
-	var nosort bool
-
-	rootCmd.PersistentFlags().BoolVar(&nosort, "no-sort", false, "omit sorted rendering of inputs and outputs")
+	rootCmd.PersistentFlags().BoolVar(new(bool), "no-sort", false, "omit sorted rendering of inputs and outputs")
 	rootCmd.PersistentFlags().BoolVar(&settings.SortInputsByRequired, "sort-inputs-by-required", false, "sort inputs by name and prints required inputs first")
 	rootCmd.PersistentFlags().BoolVar(&settings.AggregateTypeDefaults, "with-aggregate-type-defaults", false, "print default values of aggregate types")
 
-	var norequired bool
-	var noesape bool
-
-	markdownCmd.PersistentFlags().BoolVar(&norequired, "no-required", false, "omit \"Required\" column when generating Markdown")
-	markdownCmd.PersistentFlags().BoolVar(&noesape, "no-escape", false, "do not escape special characters")
+	markdownCmd.PersistentFlags().BoolVar(new(bool), "no-required", false, "omit \"Required\" column when generating Markdown")
+	markdownCmd.PersistentFlags().BoolVar(new(bool), "no-escape", false, "do not escape special characters")
 }
 
 // Execute adds all child commands to the root command and sets flags appropriately.
diff --git a/internal/pkg/print/json/json.go b/internal/pkg/print/json/json.go
index 445229c..55a83eb 100644
--- a/internal/pkg/print/json/json.go
+++ b/internal/pkg/print/json/json.go
@@ -13,7 +13,7 @@ const (
 )
 
 // Print prints a document as json.
-func Print(document *doc.Doc, settings settings.Settings) (string, error) {
+func Print(document *doc.Doc, settings *settings.Settings) (string, error) {
 	if document.HasInputs() {
 		if settings.SortByName {
 			if settings.SortInputsByRequired {
diff --git a/internal/pkg/print/markdown/document/document.go b/internal/pkg/print/markdown/document/document.go
index 75c8961..9d9697b 100644
--- a/internal/pkg/print/markdown/document/document.go
+++ b/internal/pkg/print/markdown/document/document.go
@@ -11,7 +11,7 @@ import (
 )
 
 // Print prints a document as Markdown document.
-func Print(document *doc.Doc, settings settings.Settings) (string, error) {
+func Print(document *doc.Doc, settings *settings.Settings) (string, error) {
 	var buffer bytes.Buffer
 
 	if document.HasComment() {
@@ -45,7 +45,7 @@ func Print(document *doc.Doc, settings settings.Settings) (string, error) {
 	return markdown.Sanitize(buffer.String()), nil
 }
 
-func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
+func getInputDefaultValue(input *doc.Input, settings *settings.Settings) string {
 	var result = "n/a"
 
 	if input.HasDefault() {
@@ -59,7 +59,7 @@ func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
 	return result
 }
 
-func printComment(buffer *bytes.Buffer, comment string, settings settings.Settings) {
+func printComment(buffer *bytes.Buffer, comment string, settings *settings.Settings) {
 	buffer.WriteString(fmt.Sprintf("%s\n", comment))
 }
 
@@ -75,7 +75,7 @@ func printFencedCodeBlock(code string) string {
 	return buffer.String()
 }
 
-func printInput(buffer *bytes.Buffer, input doc.Input, settings settings.Settings) {
+func printInput(buffer *bytes.Buffer, input doc.Input, settings *settings.Settings) {
 	buffer.WriteString("\n")
 	buffer.WriteString(fmt.Sprintf("### %s\n\n", markdown.SanitizeName(input.Name, settings)))
 	buffer.WriteString(fmt.Sprintf("Description: %s\n\n", markdown.SanitizeDescription(input.Description, settings)))
@@ -87,7 +87,7 @@ func printInput(buffer *bytes.Buffer, input doc.Input, settings settings.Setting
 	}
 }
 
-func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings settings.Settings) {
+func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings *settings.Settings) {
 	if settings.ShowRequired {
 		buffer.WriteString("## Required Inputs\n\n")
 		buffer.WriteString("The following input variables are required:\n")
@@ -117,7 +117,7 @@ func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings settings.Set
 	}
 }
 
-func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings settings.Settings) {
+func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings *settings.Settings) {
 	buffer.WriteString("## Outputs\n\n")
 	buffer.WriteString("The following outputs are exported:\n")
 
diff --git a/internal/pkg/print/markdown/markdown.go b/internal/pkg/print/markdown/markdown.go
index ec68b5e..4c5503e 100644
--- a/internal/pkg/print/markdown/markdown.go
+++ b/internal/pkg/print/markdown/markdown.go
@@ -8,7 +8,7 @@ import (
 )
 
 // SanitizeName escapes underscore character which have special meaning in Markdown.
-func SanitizeName(s string, settings settings.Settings) string {
+func SanitizeName(s string, settings *settings.Settings) string {
 	if settings.EscapeMarkdown {
 		// Escape underscore
 		s = strings.Replace(s, "_", "\\_", -1)
@@ -18,7 +18,7 @@ func SanitizeName(s string, settings settings.Settings) string {
 }
 
 // SanitizeDescription converts description to suitable Markdown representation. (including line-break, illegal characters, etc)
-func SanitizeDescription(s string, settings settings.Settings) string {
+func SanitizeDescription(s string, settings *settings.Settings) string {
 	s = ConvertMultiLineText(s)
 	s = EscapeIllegalCharacters(s, settings)
 
@@ -39,7 +39,7 @@ func ConvertMultiLineText(s string) string {
 }
 
 // EscapeIllegalCharacters escapes characters which have special meaning in Markdown into their corresponding literal.
-func EscapeIllegalCharacters(s string, settings settings.Settings) string {
+func EscapeIllegalCharacters(s string, settings *settings.Settings) string {
 	// Escape pipe
 	s = strings.Replace(s, "|", "\\|", -1)
 
diff --git a/internal/pkg/print/markdown/table/table.go b/internal/pkg/print/markdown/table/table.go
index a1043cd..5858673 100644
--- a/internal/pkg/print/markdown/table/table.go
+++ b/internal/pkg/print/markdown/table/table.go
@@ -11,7 +11,7 @@ import (
 )
 
 // Print prints a document as Markdown tables.
-func Print(document *doc.Doc, settings settings.Settings) (string, error) {
+func Print(document *doc.Doc, settings *settings.Settings) (string, error) {
 	var buffer bytes.Buffer
 
 	if document.HasComment() {
@@ -45,7 +45,7 @@ func Print(document *doc.Doc, settings settings.Settings) (string, error) {
 	return markdown.Sanitize(buffer.String()), nil
 }
 
-func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
+func getInputDefaultValue(input *doc.Input, settings *settings.Settings) string {
 	var result = "n/a"
 
 	if input.HasDefault() {
@@ -55,11 +55,11 @@ func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
 	return result
 }
 
-func printComment(buffer *bytes.Buffer, comment string, settings settings.Settings) {
+func printComment(buffer *bytes.Buffer, comment string, settings *settings.Settings) {
 	buffer.WriteString(fmt.Sprintf("%s\n", comment))
 }
 
-func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings settings.Settings) {
+func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings *settings.Settings) {
 	buffer.WriteString("## Inputs\n\n")
 	buffer.WriteString("| Name | Description | Type | Default |")
 
@@ -101,7 +101,7 @@ func printIsInputRequired(input *doc.Input) string {
 	return "no"
 }
 
-func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings settings.Settings) {
+func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings *settings.Settings) {
 	buffer.WriteString("## Outputs\n\n")
 	buffer.WriteString("| Name | Description |\n")
 	buffer.WriteString("|------|-------------|\n")
diff --git a/internal/pkg/print/pretty/pretty.go b/internal/pkg/print/pretty/pretty.go
index 005576b..9ba6fdc 100644
--- a/internal/pkg/print/pretty/pretty.go
+++ b/internal/pkg/print/pretty/pretty.go
@@ -10,7 +10,7 @@ import (
 )
 
 // Print prints a pretty document.
-func Print(document *doc.Doc, settings settings.Settings) (string, error) {
+func Print(document *doc.Doc, settings *settings.Settings) (string, error) {
 	var buffer bytes.Buffer
 
 	if document.HasComment() {
@@ -40,7 +40,7 @@ func Print(document *doc.Doc, settings settings.Settings) (string, error) {
 	return buffer.String(), nil
 }
 
-func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
+func getInputDefaultValue(input *doc.Input, settings *settings.Settings) string {
 	var result = "required"
 
 	if input.HasDefault() {
@@ -50,11 +50,11 @@ func getInputDefaultValue(input *doc.Input, settings settings.Settings) string {
 	return result
 }
 
-func printComment(buffer *bytes.Buffer, comment string, settings settings.Settings) {
+func printComment(buffer *bytes.Buffer, comment string, settings *settings.Settings) {
 	buffer.WriteString(fmt.Sprintf("\n%s\n", comment))
 }
 
-func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings settings.Settings) {
+func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings *settings.Settings) {
 	buffer.WriteString("\n")
 
 	for _, input := range inputs {
@@ -70,7 +70,7 @@ func printInputs(buffer *bytes.Buffer, inputs []doc.Input, settings settings.Set
 	buffer.WriteString("\n")
 }
 
-func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings settings.Settings) {
+func printOutputs(buffer *bytes.Buffer, outputs []doc.Output, settings *settings.Settings) {
 	buffer.WriteString("\n")
 
 	for _, output := range outputs {
diff --git a/internal/pkg/print/print.go b/internal/pkg/print/print.go
index 7d1aa21..0700b76 100644
--- a/internal/pkg/print/print.go
+++ b/internal/pkg/print/print.go
@@ -10,7 +10,7 @@ import (
 )
 
 // GetPrintableValue returns a printable representation of a Terraform value.
-func GetPrintableValue(value *doc.Value, settings settings.Settings, pretty bool) string {
+func GetPrintableValue(value *doc.Value, settings *settings.Settings, pretty bool) string {
 	var result string
 
 	if value == nil {
diff --git a/internal/pkg/settings/settings.go b/internal/pkg/settings/settings.go
index 0768a78..fd8f377 100644
--- a/internal/pkg/settings/settings.go
+++ b/internal/pkg/settings/settings.go
@@ -2,14 +2,18 @@ package settings
 
 // Settings represents all settings
 type Settings struct {
-	// ShowRequired show "Required" column when generating Markdown (default: true)
-	// scope: Markdown
-	ShowRequired bool
+	// AggregateTypeDefaults print default values of aggregate types (default: false)
+	// scope: Global
+	AggregateTypeDefaults bool
 
 	// EscapeMarkdown escapes special Markdown characters (such as | _ * and etc) (default: true)
 	// scope: Markdown
 	EscapeMarkdown bool
 
+	// ShowRequired show "Required" column when generating Markdown (default: true)
+	// scope: Markdown
+	ShowRequired bool
+
 	// SortByName sorted rendering of inputs and outputs (default: true)
 	// scope: Global
 	SortByName bool
@@ -17,8 +21,15 @@ type Settings struct {
 	// SortInputsByRequired sort inputs by name and prints required inputs first (default: false)
 	// scope: Global
 	SortInputsByRequired bool
+}
 
-	// AggregateTypeDefaults print default values of aggregate types (default: false)
-	// scope: Global
-	AggregateTypeDefaults bool
+//NewSettings returns new instance of Settings
+func NewSettings() *Settings {
+	return &Settings{
+		AggregateTypeDefaults: false,
+		EscapeMarkdown:        true,
+		ShowRequired:          true,
+		SortByName:            true,
+		SortInputsByRequired:  false,
+	}
 }


Test patch:
diff --git a/internal/pkg/print/json/json_test.go b/internal/pkg/print/json/json_test.go
index a16da21..72cf082 100644
--- a/internal/pkg/print/json/json_test.go
+++ b/internal/pkg/print/json/json_test.go
@@ -13,7 +13,7 @@ import (
 func TestPrint(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{}
+	var settings = &_settings.Settings{}
 
 	actual, err := json.Print(doc, settings)
 	if err != nil {
@@ -31,7 +31,7 @@ func TestPrint(t *testing.T) {
 func TestPrintWithSortByName(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName: true,
 	}
 
@@ -51,7 +51,7 @@ func TestPrintWithSortByName(t *testing.T) {
 func TestPrintWithSortInputsByRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName:           true,
 		SortInputsByRequired: true,
 	}
diff --git a/internal/pkg/print/markdown/document/document_test.go b/internal/pkg/print/markdown/document/document_test.go
index 411a441..8597881 100644
--- a/internal/pkg/print/markdown/document/document_test.go
+++ b/internal/pkg/print/markdown/document/document_test.go
@@ -13,7 +13,7 @@ import (
 func TestPrint(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{}
+	var settings = &_settings.Settings{}
 
 	actual, err := document.Print(doc, settings)
 	if err != nil {
@@ -31,7 +31,7 @@ func TestPrint(t *testing.T) {
 func TestWithAggregateTypeDefaults(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		AggregateTypeDefaults: true,
 	}
 
@@ -51,7 +51,7 @@ func TestWithAggregateTypeDefaults(t *testing.T) {
 func TestPrintWithRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		ShowRequired: true,
 	}
 
@@ -71,7 +71,7 @@ func TestPrintWithRequired(t *testing.T) {
 func TestPrintWithSortByName(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName: true,
 	}
 
@@ -91,7 +91,7 @@ func TestPrintWithSortByName(t *testing.T) {
 func TestPrintWithSortInputsByRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName:           true,
 		SortInputsByRequired: true,
 	}
@@ -112,7 +112,7 @@ func TestPrintWithSortInputsByRequired(t *testing.T) {
 func TestPrintWithEscapeName(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		EscapeMarkdown: true,
 	}
 
diff --git a/internal/pkg/print/markdown/table/table_test.go b/internal/pkg/print/markdown/table/table_test.go
index f2908c4..f7075de 100644
--- a/internal/pkg/print/markdown/table/table_test.go
+++ b/internal/pkg/print/markdown/table/table_test.go
@@ -13,7 +13,7 @@ import (
 func TestPrint(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{}
+	var settings = &_settings.Settings{}
 
 	actual, err := table.Print(doc, settings)
 	if err != nil {
@@ -31,7 +31,7 @@ func TestPrint(t *testing.T) {
 func TestWithAggregateTypeDefaults(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		AggregateTypeDefaults: true,
 	}
 
@@ -51,7 +51,7 @@ func TestWithAggregateTypeDefaults(t *testing.T) {
 func TestPrintWithRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		ShowRequired: true,
 	}
 
@@ -71,7 +71,7 @@ func TestPrintWithRequired(t *testing.T) {
 func TestPrintWithSortByName(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName: true,
 	}
 
@@ -91,7 +91,7 @@ func TestPrintWithSortByName(t *testing.T) {
 func TestPrintWithSortInputsByRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName:           true,
 		SortInputsByRequired: true,
 	}
@@ -112,7 +112,7 @@ func TestPrintWithSortInputsByRequired(t *testing.T) {
 func TestPrintWithEscapeName(t *testing.T) {
 	doc := doc.TestDoc(t, "../..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		EscapeMarkdown: true,
 	}
 
diff --git a/internal/pkg/print/pretty/pretty_test.go b/internal/pkg/print/pretty/pretty_test.go
index 2d66de9..50aa90d 100644
--- a/internal/pkg/print/pretty/pretty_test.go
+++ b/internal/pkg/print/pretty/pretty_test.go
@@ -12,7 +12,7 @@ import (
 func TestPretty(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{}
+	var settings = &_settings.Settings{}
 
 	actual, err := pretty.Print(doc, settings)
 	if err != nil {
@@ -97,7 +97,7 @@ func TestPretty(t *testing.T) {
 func TestPrettyWithWithAggregateTypeDefaults(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		AggregateTypeDefaults: true,
 	}
 
@@ -184,7 +184,7 @@ func TestPrettyWithWithAggregateTypeDefaults(t *testing.T) {
 func TestPrettyWithSortByName(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName: true,
 	}
 
@@ -271,7 +271,7 @@ func TestPrettyWithSortByName(t *testing.T) {
 func TestPrettyWithSortInputsByRequired(t *testing.T) {
 	doc := doc.TestDoc(t, "..")
 
-	var settings = _settings.Settings{
+	var settings = &_settings.Settings{
 		SortByName:           true,
 		SortInputsByRequired: true,
 	}