Skip to content

Commit

Permalink
chore: Use github.com/goccy/go-yaml instead of gopkg.in/yaml.v3
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 28, 2025
1 parent 7ad5999 commit e25d60b
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 147 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/coreos/go-semver v0.3.1
github.com/fsnotify/fsnotify v1.8.0
github.com/go-git/go-git/v5 v5.13.2
github.com/goccy/go-yaml v1.15.15
github.com/google/go-github/v68 v68.0.0
github.com/google/renameio/v2 v2.0.0
github.com/gopasspw/gopass v1.15.15
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
golang.org/x/sys v0.29.0
golang.org/x/term v0.28.0
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v3 v3.0.1
howett.net/plist v1.0.1
mvdan.cc/sh/v3 v3.10.0
)
Expand Down Expand Up @@ -180,4 +180,5 @@ require (
golang.org/x/tools v0.29.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-yaml v1.15.15 h1:5turdzAlutS2Q7/QR/9R99Z1K0J00qDb4T0pHJcZ5ew=
github.com/goccy/go-yaml v1.15.15/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/entrytypeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (s *EntryTypeSet) MarshalJSON() ([]byte, error) {
}
}

// MarshalYAML implements gopkg.in/yaml.v3.Marshaler.
// MarshalYAML implements github.com/goccy/go-yaml.Marshaler.
func (s *EntryTypeSet) MarshalYAML() (any, error) {
if s.bits == EntryTypesAll {
return []string{"all"}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"slices"
"strings"

"github.com/goccy/go-yaml"
"github.com/pelletier/go-toml/v2"
"github.com/tailscale/hujson"
"gopkg.in/yaml.v3"
)

// Formats.
Expand Down
28 changes: 28 additions & 0 deletions internal/chezmoi/hexbytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chezmoi

import (
"encoding/hex"
"strconv"
)

// A HexBytes is a []byte which is marshaled as a hex string.
Expand All @@ -22,6 +23,15 @@ func (h HexBytes) MarshalText() ([]byte, error) {
return result, nil
}

// MarshalYAML implements github.com/goccy/go-yaml.BytesMarshaler.MarshalYAML.
func (h HexBytes) MarshalYAML() ([]byte, error) {
data := make([]byte, 2+2*len(h))
data[0] = '"'
hex.Encode(data[1:len(data)-1], []byte(h))
data[len(data)-1] = '"'
return data, nil
}

// UnmarshalText implements encoding.TextUnmarshaler.UnmarshalText.
func (h *HexBytes) UnmarshalText(text []byte) error {
if len(text) == 0 {
Expand All @@ -36,6 +46,24 @@ func (h *HexBytes) UnmarshalText(text []byte) error {
return nil
}

// UnmarshalYAML implements github.com/goccy/go-yaml.BytesUnmarshaler.UnmarshalYAML.
func (h *HexBytes) UnmarshalYAML(data []byte) error {
s, err := strconv.Unquote(string(data))
if err != nil {
return err
}
if s == "" {
*h = nil
return nil
}
hexBytes, err := hex.DecodeString(s)
if err != nil {
return err
}
*h = hexBytes
return nil
}

func (h HexBytes) String() string {
return hex.EncodeToString(h)
}
7 changes: 4 additions & 3 deletions internal/chezmoi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"strings"
"text/template"

"github.com/goccy/go-yaml"
"github.com/mattn/go-runewidth"
"github.com/mitchellh/copystructure"
"github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"
)

// A Template extends text/template.Template with support for directives.
Expand Down Expand Up @@ -61,8 +61,9 @@ func ParseTemplate(name string, data []byte, options TemplateOptions) (*Template
}
funcs["toYaml"] = func(data any) string {
var builder strings.Builder
encoder := yaml.NewEncoder(&builder)
encoder.SetIndent(runewidth.StringWidth(options.FormatIndent))
encoder := yaml.NewEncoder(&builder,
yaml.Indent(runewidth.StringWidth(options.FormatIndent)),
)
if err := encoder.Encode(data); err != nil {
panic(err)
}
Expand Down
14 changes: 5 additions & 9 deletions internal/cmd/autobool.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cmd

import (
"errors"
"bytes"
"fmt"
"reflect"
"strconv"
"strings"

"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)
Expand Down Expand Up @@ -38,7 +37,7 @@ func (b autoBool) MarshalJSON() ([]byte, error) {
}
}

// MarshalYAML implements gopkg.in/yaml.v3.Marshaler.
// MarshalYAML implements github.com/goccy/go-yaml.Marshaler.
func (b autoBool) MarshalYAML() (any, error) {
if b.auto {
return "auto", nil
Expand Down Expand Up @@ -89,15 +88,12 @@ func (b *autoBool) UnmarshalJSON(data []byte) error {
}

// UnmarshalYAML implements gopkg.in/yaml.Unmarshaler.UnmarshalYAML.
func (b *autoBool) UnmarshalYAML(value *yaml.Node) error {
if value.Kind != yaml.ScalarNode {
return errors.New("expected scalar node")
}
if value.Value == "auto" {
func (b *autoBool) UnmarshalYAML(data []byte) error {
if bytes.Equal(data, []byte("auto")) {
b.auto = true
return nil
}
boolValue, err := chezmoi.ParseBool(value.Value)
boolValue, err := chezmoi.ParseBool(string(data))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/testdata/scripts/configstate.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ exec chezmoi diff
email = "[email protected]"
-- golden/state-dump.yaml --
configState:
configState:
configTemplateContentsSHA256: af43121a524340707b84e390f510c949731177e6f2a25b3b6b11b2fc656cf8f2
configState:
configTemplateContentsSHA256: af43121a524340707b84e390f510c949731177e6f2a25b3b6b11b2fc656cf8f2
entryState: {}
gitHubKeysState: {}
gitHubLatestReleaseState: {}
Expand Down
186 changes: 86 additions & 100 deletions internal/cmd/testdata/scripts/dumpyaml.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,125 +10,111 @@ cmp stdout golden/dump-except-dirs.yaml

-- golden/dump-except-dirs.yaml --
.create:
type: file
name: .create
contents: |
# contents of .create
perm: 420
type: file
name: .create
contents: "# contents of .create\n"
perm: 420
.dir/file:
type: file
name: .dir/file
contents: |
# contents of .dir/file
perm: 420
type: file
name: .dir/file
contents: "# contents of .dir/file\n"
perm: 420
.dir/subdir/file:
type: file
name: .dir/subdir/file
contents: |
# contents of .dir/subdir/file
perm: 420
type: file
name: .dir/subdir/file
contents: "# contents of .dir/subdir/file\n"
perm: 420
.empty:
type: file
name: .empty
contents: ""
perm: 420
type: file
name: .empty
contents: ""
perm: 420
.executable:
type: file
name: .executable
contents: |
# contents of .executable
perm: 493
type: file
name: .executable
contents: "# contents of .executable\n"
perm: 493
.file:
type: file
name: .file
contents: |
# contents of .file
perm: 420
type: file
name: .file
contents: "# contents of .file\n"
perm: 420
.private:
type: file
name: .private
contents: |
# contents of .private
perm: 384
type: file
name: .private
contents: "# contents of .private\n"
perm: 384
.readonly:
type: file
name: .readonly
contents: |
# contents of .readonly
perm: 292
type: file
name: .readonly
contents: "# contents of .readonly\n"
perm: 292
.symlink:
type: symlink
name: .symlink
linkname: .dir/subdir/file
type: symlink
name: .symlink
linkname: .dir/subdir/file
.template:
type: file
name: .template
contents: |
key = value
perm: 420
type: file
name: .template
contents: |
key = value
perm: 420
-- golden/dump.yaml --
.create:
type: file
name: .create
contents: |
# contents of .create
perm: 420
type: file
name: .create
contents: "# contents of .create\n"
perm: 420
.dir:
type: dir
name: .dir
perm: 493
type: dir
name: .dir
perm: 493
.dir/file:
type: file
name: .dir/file
contents: |
# contents of .dir/file
perm: 420
type: file
name: .dir/file
contents: "# contents of .dir/file\n"
perm: 420
.dir/subdir:
type: dir
name: .dir/subdir
perm: 493
type: dir
name: .dir/subdir
perm: 493
.dir/subdir/file:
type: file
name: .dir/subdir/file
contents: |
# contents of .dir/subdir/file
perm: 420
type: file
name: .dir/subdir/file
contents: "# contents of .dir/subdir/file\n"
perm: 420
.empty:
type: file
name: .empty
contents: ""
perm: 420
type: file
name: .empty
contents: ""
perm: 420
.executable:
type: file
name: .executable
contents: |
# contents of .executable
perm: 493
type: file
name: .executable
contents: "# contents of .executable\n"
perm: 493
.file:
type: file
name: .file
contents: |
# contents of .file
perm: 420
type: file
name: .file
contents: "# contents of .file\n"
perm: 420
.private:
type: file
name: .private
contents: |
# contents of .private
perm: 384
type: file
name: .private
contents: "# contents of .private\n"
perm: 384
.readonly:
type: file
name: .readonly
contents: |
# contents of .readonly
perm: 292
type: file
name: .readonly
contents: "# contents of .readonly\n"
perm: 292
.symlink:
type: symlink
name: .symlink
linkname: .dir/subdir/file
type: symlink
name: .symlink
linkname: .dir/subdir/file
.template:
type: file
name: .template
contents: |
key = value
perm: 420
type: file
name: .template
contents: |
key = value
perm: 420
Loading

0 comments on commit e25d60b

Please sign in to comment.