Skip to content

Commit

Permalink
basic: respect formatter config defaults (#24)
Browse files Browse the repository at this point in the history
Under a certain configuration, the default value for `basic.Config.Indent`
would be ignored. Adjust the way the `NewWithConfig` function works to
decode the map overtop of default config, which will preserve default
values for fields not found in the map.
  • Loading branch information
braydonk committed Aug 24, 2022
1 parent d7d5422 commit 850e08b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ addlicense:
addlicense -c "Google LLC" -l apache .

test_diff:
go test -v -mod=mod github.com/google/yamlfmt/internal/diff
go test -v -mod=mod github.com/google/yamlfmt/internal/diff

test_basic_formatter:
go test -v -mod=mod github.com/google/yamlfmt/formatters/basic
6 changes: 3 additions & 3 deletions formatters/basic/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (f *BasicFormatterFactory) NewDefault() yamlfmt.Formatter {
}

func (f *BasicFormatterFactory) NewWithConfig(configData map[string]interface{}) (yamlfmt.Formatter, error) {
var c Config
err := mapstructure.Decode(configData, &c)
config := DefaultConfig()
err := mapstructure.Decode(configData, &config)
if err != nil {
return nil, err
}
return &BasicFormatter{Config: &c}, nil
return &BasicFormatter{Config: config}, nil
}
67 changes: 67 additions & 0 deletions formatters/basic/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package basic_test

import (
"testing"

"github.com/google/yamlfmt/formatters/basic"
)

func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
testCases := []struct {
name string
configMap map[string]interface{}
expectedConfig basic.Config
}{
{
name: "only indent specified",
configMap: map[string]interface{}{
"indent": 4,
},
expectedConfig: basic.Config{
Indent: 4,
IncludeDocumentStart: false,
},
},
{
name: "only include_document_start specified",
configMap: map[string]interface{}{
"include_document_start": true,
},
expectedConfig: basic.Config{
Indent: 2,
IncludeDocumentStart: true,
},
},
}

factory := basic.BasicFormatterFactory{}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
formatter, err := factory.NewWithConfig(tc.configMap)
if err != nil {
t.Fatalf("expected factory to create config, got error: %v", err)
}
basicFormatter, ok := formatter.(*basic.BasicFormatter)
if !ok {
t.Fatal("should have been able to cast to basic formatter")
}
if *basicFormatter.Config != tc.expectedConfig {
t.Fatalf("configs differed:\nexpected: %v\ngot: %v", *basicFormatter.Config, tc.expectedConfig)
}
})
}
}

0 comments on commit 850e08b

Please sign in to comment.