Skip to content

Commit 9684184

Browse files
committed
move json.Unmarshal into UnmarshalJSON function
1 parent 7ba1131 commit 9684184

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

jsonapi/marshal_enum_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ func (s PublishStatus) MarshalText() (text []byte, err error) {
3434

3535
// UnmarshalText implements the TextUnmarshaler interface.
3636
func (s *PublishStatus) UnmarshalText(text []byte) error {
37-
var label string
38-
json.Unmarshal(text, &label)
37+
label := string(text)
3938

4039
for key, v := range publishStatusValues {
4140
if v == label {
@@ -48,7 +47,11 @@ func (s *PublishStatus) UnmarshalText(text []byte) error {
4847
}
4948

5049
func (s *PublishStatus) UnmarshalJSON(data []byte) error {
51-
return s.UnmarshalText(data)
50+
var text string
51+
if err := json.Unmarshal(data, &text); err != nil {
52+
return err
53+
}
54+
return s.UnmarshalText([]byte(text))
5255
}
5356

5457
type EnumPost struct {
@@ -69,7 +72,7 @@ func (e *EnumPost) SetID(ID string) error {
6972

7073
var _ = Describe("Custom enum types", func() {
7174
status := StatusPublished
72-
statusValue := "\"published\""
75+
statusValue := "published"
7376
singleJSON := []byte(`{"data":{"id": "1", "type": "enumPosts", "attributes": {"title":"First Post","status":"published"}}}`)
7477
firstPost := EnumPost{ID: "1", Title: "First Post", Status: StatusPublished}
7578
singlePostMap := map[string]interface{}{

0 commit comments

Comments
 (0)