Skip to content

Commit

Permalink
fix: Fix marshaling of autobools
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 25, 2024
1 parent 5224e11 commit 523fd84
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/cmd/autobool.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

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

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

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)
Expand Down Expand Up @@ -71,6 +73,39 @@ func (b *autoBool) Type() string {
return "bool|auto"
}

// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON.
func (b *autoBool) UnmarshalJSON(data []byte) error {
if string(data) == `"auto"` {
b.auto = true
return nil
}
value, err := chezmoi.ParseBool(string(data))
if err != nil {
return err
}
b.auto = false
b.value = value
return nil
}

// 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" {
b.auto = true
return nil
}
boolValue, err := chezmoi.ParseBool(value.Value)
if err != nil {
return err
}
b.auto = false
b.value = boolValue
return nil
}

// Value returns b's value, calling b's autoFunc if needed.
func (b *autoBool) Value(autoFunc func() bool) bool {
if b.auto {
Expand Down

0 comments on commit 523fd84

Please sign in to comment.