Skip to content

Commit

Permalink
Merge pull request #5 from hottestseason/support_bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu authored May 21, 2022
2 parents a608c3f + 6081cd7 commit 3c87dbc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
10 changes: 10 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ func Unmarshal(data []byte, v interface{}) error {
continue
}
fv.SetFloat(n)
case reflect.Bool:
if potentiallyNull {
continue
}
b, err := strconv.ParseBool(s)
if err != nil {
errs[ft.Name] = &UnmarshalTypeError{Value: "bool " + s, Type: fv.Type()}
continue
}
fv.SetBool(b)
default:
u := indirect(fv)
if u == nil {
Expand Down
40 changes: 22 additions & 18 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func TestData2Map(t *testing.T) {
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
Memo string `ltsv:"-"`
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
EmailVerified bool `ltsv:"email_verified"`
Memo string `ltsv:"-"`
}

func pfloat64(f float64) *float64 {
Expand All @@ -41,32 +42,35 @@ var decodeTests = []struct {
}{
{
Name: "Simple",
Input: "user:songmu\tage:36\theight:169.1\tweight:66.6",
Input: "user:songmu\tage:36\theight:169.1\tweight:66.6\temail_verified:true",
Output: &ss{
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
EmailVerified: true,
},
},
{
Name: "Default values",
Input: "user:songmu\tage:36",
Output: &ss{
User: "songmu",
Age: 36,
Height: nil,
Weight: 0.0,
User: "songmu",
Age: 36,
Height: nil,
Weight: 0.0,
EmailVerified: false,
},
},
{
Name: "Hyphen and empty string as null number",
Input: "user:songmu\tage:\theight:-",
Output: &ss{
User: "songmu",
Age: 0,
Height: nil,
Weight: 0.0,
User: "songmu",
Age: 0,
Height: nil,
Weight: 0.0,
EmailVerified: false,
},
},
}
Expand Down
9 changes: 9 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func makeStructWriter(v reflect.Value) fieldWriter {
writer = makeUintWriter(key)
case reflect.Float32, reflect.Float64:
writer = makeFloatWriter(key)
case reflect.Bool:
writer = makeBoolWriter(key)
default:
dereference = false
writer = makeInterfaceWriter(key)
Expand Down Expand Up @@ -184,6 +186,13 @@ func makeFloatWriter(key string) fieldWriter {
})
}

func makeBoolWriter(key string) fieldWriter {
return fieldWriter(func(w io.Writer, v reflect.Value) error {
writeField(w, key, strconv.FormatBool(v.Bool()))
return nil
})
}

func makeInterfaceWriter(key string) fieldWriter {
return fieldWriter(func(w io.Writer, v reflect.Value) error {
if !v.CanInterface() {
Expand Down
22 changes: 12 additions & 10 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ var encodeTests = []struct {
{
Name: "Simple with nil pointer",
Input: &ss{
User: "songmu",
Age: 36,
Height: nil,
Weight: 66.6,
Memo: "songmu.jp",
User: "songmu",
Age: 36,
Height: nil,
Weight: 66.6,
EmailVerified: true,
Memo: "songmu.jp",
},
Output: "user:songmu\tage:36\tweight:66.6",
},
{
Name: "Simple without nil pointer",
Input: &ss{
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
Memo: "songmu.jp",
User: "songmu",
Age: 36,
Height: pfloat64(169.1),
Weight: 66.6,
EmailVerified: false,
Memo: "songmu.jp",
},
Output: "user:songmu\tage:36\theight:169.1\tweight:66.6",
},
Expand Down

0 comments on commit 3c87dbc

Please sign in to comment.