Skip to content

Commit

Permalink
support pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Jul 10, 2016
1 parent c57a876 commit 5875a7a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
14 changes: 10 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ func Unmarshal(data []byte, v interface{}) error {
ft := t.Field(i)
fv := rv.Field(i)

if !fv.CanSet() {
continue
}

tag := ft.Tag.Get("ltsv")
tags := strings.Split(tag, ",")
key := tags[0]
Expand All @@ -116,6 +112,16 @@ func Unmarshal(data []byte, v interface{}) error {
continue
}

if fv.Kind() == reflect.Ptr {
if fv.IsNil() {
fv.Set(reflect.New(fv.Type().Elem()))
}
fv = fv.Elem()
}
if !fv.CanSet() {
continue
}

switch fv.Kind() {
case reflect.String:
fv.SetString(s)
Expand Down
21 changes: 17 additions & 4 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,33 @@ func TestUnmarshal(t *testing.T) {
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height float64 `ltsv:"height"`
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
}
s := &ss{}
Unmarshal([]byte("user:songmu\tage:36\theight:169.1\tweight:66.6"), s)
hei := 169.1
expect2 := &ss{
User: "songmu",
Age: 36,
Height: 169.1,
Height: &hei,
Weight: 66.6,
}
if !reflect.DeepEqual(s, expect2) {
t.Errorf("result of data2map not expected: %#v", s)
}

s2 := &ss{}
Unmarshal([]byte("user:songmu\tage:36"), s2)
expect3 := &ss{
User: "songmu",
Age: 36,
// Height: nil,
Weight: 0.0,
}
if !reflect.DeepEqual(s2, expect3) {
t.Errorf("result of data2map not expected: %#v", s2)
}
}
6 changes: 6 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func Marshal(v interface{}) ([]byte, error) {
if key == "" {
key = strings.ToLower(ft.Name)
}
if fv.Kind() == reflect.Ptr {
if fv.IsNil() {
continue
}
fv = fv.Elem()
}

switch fv.Kind() {
case reflect.String:
Expand Down
20 changes: 16 additions & 4 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ func TestMarshal(t *testing.T) {
}

type ss struct {
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height float64 `ltsv:"height"`
User string `ltsv:"user"`
Age uint8 `ltsv:"age"`
Height *float64 `ltsv:"height"`
Weight float32
Memo string `ltsv:"-"`
}
he := 169.1
s := &ss{
User: "songmu",
Age: 36,
Height: 169.1,
Height: &he,
Weight: 66.6,
Memo: "songmu.jp",
}
Expand All @@ -33,4 +34,15 @@ func TestMarshal(t *testing.T) {
if string(buf) != string(expect) {
t.Errorf("result is not expected: %s", string(buf))
}

s = &ss{
User: "songmu",
Age: 36,
Memo: "songmu.jp",
}
expect = []byte("user:songmu\tage:36\tweight:0")
buf, _ = Marshal(s)
if string(buf) != string(expect) {
t.Errorf("result is not expected: %s", string(buf))
}
}

0 comments on commit 5875a7a

Please sign in to comment.