Skip to content

Commit dea6ab5

Browse files
Migrate to YAML encoder `gopkg.in/yaml.v3
Previously the `github.com/ghodss/yaml` (1) package was used as encoder for YAML (2) files. It was evaluated and choosen over the `gopkg.in/yaml.v2` package because it fixed some problems and misleading YAML specification implementations while also simplifying the usage of both JSON (3) and YAML (2) at the same time. Unfortunately the package is not actively maintained anymore and also has some problems and usage edge cases while the `gopkg.in/yaml` package is actively maintained and improved the YAML specification compatibility a lot. Therefore snowsaw migrated to the latest major version `gopkg.in/yaml.v3` (4) replacing the previously used package. References: (1) https://github.com/ghodss/yaml (2) https://yaml.org (3) https://www.json.org (4) https://github.com/go-yaml/yaml/tree/v3 Epic GH-33 Resolves GH-69
1 parent 503055c commit dea6ab5

File tree

6 files changed

+40
-50
lines changed

6 files changed

+40
-50
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ go 1.12
44

55
require (
66
github.com/fatih/color v1.7.0
7-
github.com/ghodss/yaml v1.0.0
87
github.com/imdario/mergo v0.3.7
98
github.com/magefile/mage v1.8.0
109
github.com/mattn/go-colorable v0.1.2 // indirect
1110
github.com/mitchellh/go-homedir v1.1.0
1211
github.com/spf13/cobra v0.0.5
12+
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22
1313
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
88
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
99
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
1010
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
11-
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
12-
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
1311
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1412
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
1513
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
@@ -48,3 +46,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
4846
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4947
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
5048
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
49+
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22 h1:0efs3hwEZhFKsCoP8l6dDB1AZWMgnEl3yWXWRZTOaEA=
50+
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/config/builder/builder.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
"fmt"
1717
"io/ioutil"
1818
"path/filepath"
19-
"reflect"
2019

2120
"github.com/fatih/color"
2221
"github.com/imdario/mergo"
2322

23+
"github.com/arcticicestudio/snowsaw/pkg/config"
2424
"github.com/arcticicestudio/snowsaw/pkg/config/encoder"
25+
"github.com/arcticicestudio/snowsaw/pkg/config/encoder/yaml"
2526
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
2627
"github.com/arcticicestudio/snowsaw/pkg/prt"
2728
"github.com/arcticicestudio/snowsaw/pkg/util/filesystem"
@@ -33,71 +34,63 @@ type builder struct {
3334
}
3435

3536
// Load tries to load all given configuration files.
36-
// It checks if the path is valid and exists, tries to assign a matching encoder.Encoder based on the file extension and
37-
// returns a pointer to a builder to chain and pass the loaded files to the Merge function.
37+
// It checks if the path is valid and exists, tries to assign a matching encoder based on the file extension and returns
38+
// a pointer to a builder to chain the merge function.
3839
func Load(files ...*file.File) *builder {
39-
s := &builder{Files: []*file.File{}}
40+
b := &builder{Files: []*file.File{}}
4041

4142
for _, f := range files {
42-
// Convert to absolute path and check if file exists, otherwise ignore and check next.
43-
f.Path, _ = filepath.Abs(f.Path)
44-
if exists, _ := filesystem.FileExists(f.Path); !exists {
43+
// Convert to an absolute path and check if the file exists, otherwise ignore and check next.
44+
absPath, absPathErr := filepath.Abs(f.Path)
45+
if absPathErr != nil {
46+
prt.Debugf("Could not convert to absolute configuration file path: %v", absPathErr)
47+
continue
48+
}
49+
if exists, _ := filesystem.FileExists(absPath); !exists {
4550
prt.Debugf("Ignoring non-existent configuration file: %s", color.CyanString(f.Path))
4651
continue
4752
}
53+
f.Path = absPath
4854

49-
// Find matching encoder by file extension if not already set.
50-
if f.Encoder == nil {
51-
fileExt := filepath.Ext(f.Path)
52-
if len(fileExt) <= 1 {
53-
prt.Debugf("Ignoring configuration file without supported extension: %s", color.CyanString(f.Path))
54-
continue
55-
}
56-
57-
// Strip dot character separating the file name and extension.
58-
fileExt = fileExt[1:]
59-
60-
// Only add files with supported encoders.
61-
for ext, enc := range encoder.ExtensionMapping {
62-
if ext == fileExt {
63-
f.Encoder = enc
64-
s.Files = append(s.Files, f)
65-
break
66-
}
67-
}
68-
} else {
69-
s.Files = append(s.Files, f)
55+
fileExt := filepath.Ext(f.Path)
56+
// Check if the file matches the supported YAML extension...
57+
if len(fileExt) <= 1 || fileExt[1:] != encoder.ExtensionsYaml {
58+
prt.Debugf("Ignoring configuration file without supported extension: %s", color.CyanString(f.Path))
59+
continue
60+
}
61+
// ...when trimming the dot character that separates the file name and extension.
62+
if fileExt[1:] == encoder.ExtensionsYaml {
63+
f.Encoder = yaml.NewYamlEncoder()
64+
b.Files = append(b.Files, f)
7065
}
7166
}
7267

73-
return s
68+
return b
7469
}
7570

7671
// Into accepts a configuration struct pointer and populates it with the current config state.
77-
// The order of the files array is maintained when merging the configuration states into the struct is enabled.
78-
func (s *builder) Into(c interface{}, merge bool) error {
79-
base := reflect.New(reflect.TypeOf(c).Elem()).Interface()
80-
81-
for _, f := range s.Files {
72+
// The order of the files are maintained when merging of the configuration states is enabled.
73+
func (b *builder) Into(c *config.Config, merge bool) error {
74+
for _, f := range b.Files {
8275
content, err := ioutil.ReadFile(f.Path)
8376
if err != nil {
8477
return err
8578
}
8679

8780
if !merge {
88-
// Decode the file content into the given base configuration state using the assigned encoder...
81+
// Decode the file content into the given configuration state using the assigned encoder...
8982
if encErr := f.Encoder.Decode(content, &c); encErr != nil {
9083
return fmt.Errorf("%s: %v", f.Path, encErr)
9184
}
9285
continue
9386
}
9487

95-
// ...or merge into the given base configuration state.
96-
raw := base
97-
if encErr := f.Encoder.Decode(content, &raw); encErr != nil {
88+
newState := &config.Config{}
89+
// ...or merge into the given encoded configuration state.
90+
if encErr := f.Encoder.Decode(content, &newState); encErr != nil {
9891
return fmt.Errorf("%s: %v", f.Path, encErr)
9992
}
100-
if encErr := mergo.Merge(c, raw, mergo.WithAppendSlice, mergo.WithOverride); encErr != nil {
93+
if encErr := mergo.Merge(c, newState, mergo.WithAppendSlice, mergo.WithOverride); encErr != nil {
10194
return fmt.Errorf("%s: %v", f.Path, encErr)
10295
}
10396
}

pkg/config/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ import (
2727
// Config represents the application-wide configurations.
2828
type Config struct {
2929
// LogLevel is the application-wide logging verbosity level.
30-
LogLevel string `json:"logLevel" yaml:"logLevel"`
30+
LogLevel string `yaml:"logLevel"`
3131

3232
// Snowblocks are general snowblocks configurations.
33-
Snowblocks Snowblocks `json:"snowblocks,flow" yaml:"snowblocks,flow"`
33+
Snowblocks Snowblocks `yaml:"snowblocks,flow"`
3434
}
3535

3636
// Snowblocks represents the general snowblocks configurations.
3737
type Snowblocks struct {
3838
// BaseDirs are the paths of the snowblock base directories.
39-
BaseDirs []string `json:"baseDirs,flow" yaml:"baseDirs,flow"`
39+
BaseDirs []string `yaml:"baseDirs,flow"`
4040
// Paths are the paths of the snowblocks directories.
41-
Paths []string `json:"paths,flow" yaml:"paths,flow"`
41+
Paths []string `yaml:"paths,flow"`
4242
}
4343

4444
func init() {

pkg/config/constants.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
package config
1313

1414
import (
15-
"github.com/arcticicestudio/snowsaw/pkg/api/snowblock"
1615
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
17-
"github.com/arcticicestudio/snowsaw/pkg/snowblock/task"
18-
"github.com/arcticicestudio/snowsaw/pkg/snowblock/task/link"
1916
)
2017

2118
const (

pkg/config/encoder/yaml/yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package yaml
1414

1515
import (
16-
"github.com/ghodss/yaml"
16+
"gopkg.in/yaml.v3"
1717
)
1818

1919
// Encoder represents a YAML configuration file encoder.

0 commit comments

Comments
 (0)