Skip to content

Commit

Permalink
support proper encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Oct 18, 2018
1 parent ed7547d commit 66cc46b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ident.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flect

import (
"encoding"
"regexp"
"strings"
"unicode"
Expand Down Expand Up @@ -84,3 +85,15 @@ func toParts(s string) []string {

return parts
}

var _ encoding.TextUnmarshaler = &Ident{}
var _ encoding.TextMarshaler = &Ident{}

func (i *Ident) UnmarshalText(data []byte) error {
(*i) = New(string(data))
return nil
}

func (i Ident) MarshalText() ([]byte, error) {
return []byte(i.Original), nil
}
11 changes: 11 additions & 0 deletions ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ func Test_New(t *testing.T) {
})
}
}
func Test_MarshalText(t *testing.T) {
r := require.New(t)

n := New("mark")
b, err := n.MarshalText()
r.NoError(err)
r.Equal("mark", string(b))

r.NoError((&n).UnmarshalText([]byte("bates")))
r.Equal("bates", n.String())
}
13 changes: 13 additions & 0 deletions name/name.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package name

import (
"encoding"
"strings"

"github.com/gobuffalo/flect"
Expand Down Expand Up @@ -47,3 +48,15 @@ func (i Ident) Group() Ident {
parts = append(parts, last)
return New(strings.Join(parts, ""))
}

var _ encoding.TextUnmarshaler = &Ident{}
var _ encoding.TextMarshaler = &Ident{}

func (i *Ident) UnmarshalText(data []byte) error {
(*i) = New(string(data))
return nil
}

func (i Ident) MarshalText() ([]byte, error) {
return []byte(i.Original), nil
}
12 changes: 12 additions & 0 deletions name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ func Test_Group(t *testing.T) {
})
}
}

func Test_MarshalText(t *testing.T) {
r := require.New(t)

n := New("mark")
b, err := n.MarshalText()
r.NoError(err)
r.Equal("mark", string(b))

r.NoError((&n).UnmarshalText([]byte("bates")))
r.Equal("bates", n.String())
}

0 comments on commit 66cc46b

Please sign in to comment.