|
| 1 | +package jsonapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | +) |
| 10 | + |
| 11 | +type PublishStatus int |
| 12 | + |
| 13 | +const ( |
| 14 | + StatusUnpublished PublishStatus = iota |
| 15 | + StatusPublished |
| 16 | +) |
| 17 | + |
| 18 | +var publishStatusValues = []string{ |
| 19 | + StatusUnpublished: "unpublished", |
| 20 | + StatusPublished: "published", |
| 21 | +} |
| 22 | + |
| 23 | +func (s PublishStatus) String() string { |
| 24 | + if s < 0 || int(s) >= len(publishStatusValues) { |
| 25 | + panic("value out of range") |
| 26 | + } |
| 27 | + return publishStatusValues[s] |
| 28 | +} |
| 29 | + |
| 30 | +// MarshalText implements the TextMarshaler interface. |
| 31 | +func (s PublishStatus) MarshalText() (text []byte, err error) { |
| 32 | + return []byte(s.String()), nil |
| 33 | +} |
| 34 | + |
| 35 | +// UnmarshalText implements the TextUnmarshaler interface. |
| 36 | +func (s *PublishStatus) UnmarshalText(text []byte) error { |
| 37 | + label := string(text) |
| 38 | + |
| 39 | + for key, v := range publishStatusValues { |
| 40 | + if v == label { |
| 41 | + *s = PublishStatus(key) |
| 42 | + return nil |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return fmt.Errorf("invalid value `%s`", label) |
| 47 | +} |
| 48 | + |
| 49 | +func (s *PublishStatus) UnmarshalJSON(data []byte) error { |
| 50 | + var text string |
| 51 | + if err := json.Unmarshal(data, &text); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + return s.UnmarshalText([]byte(text)) |
| 55 | +} |
| 56 | + |
| 57 | +type EnumPost struct { |
| 58 | + ID string `json:"-"` |
| 59 | + Title string |
| 60 | + Status PublishStatus |
| 61 | +} |
| 62 | + |
| 63 | +func (e EnumPost) GetID() string { |
| 64 | + return e.ID |
| 65 | +} |
| 66 | + |
| 67 | +func (e *EnumPost) SetID(ID string) error { |
| 68 | + e.ID = ID |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +var _ = Describe("Custom enum types", func() { |
| 74 | + status := StatusPublished |
| 75 | + statusValue := "published" |
| 76 | + singleJSON := []byte(`{"data":{"id": "1", "type": "enumPosts", "attributes": {"title":"First Post","status":"published"}}}`) |
| 77 | + firstPost := EnumPost{ID: "1", Title: "First Post", Status: StatusPublished} |
| 78 | + singlePostMap := map[string]interface{}{ |
| 79 | + "data": map[string]interface{}{ |
| 80 | + "id": "1", |
| 81 | + "type": "enumPosts", |
| 82 | + "attributes": map[string]interface{}{ |
| 83 | + "title": firstPost.Title, |
| 84 | + "status": StatusPublished, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + Context("When marshaling objects including enumes", func() { |
| 90 | + singlePost := EnumPost{ |
| 91 | + ID: "1", |
| 92 | + Title: "First Post", |
| 93 | + Status: StatusPublished, |
| 94 | + } |
| 95 | + |
| 96 | + It("marshals JSON", func() { |
| 97 | + result, err := MarshalToJSON(singlePost) |
| 98 | + Expect(err).ToNot(HaveOccurred()) |
| 99 | + Expect(result).To(MatchJSON(singleJSON)) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + Context("When unmarshaling objects including enums", func() { |
| 104 | + |
| 105 | + It("unmarshals status string values to int enum type", func() { |
| 106 | + var result PublishStatus |
| 107 | + result.UnmarshalText([]byte(statusValue)) |
| 108 | + Expect(result).To(Equal(status)) |
| 109 | + }) |
| 110 | + |
| 111 | + It("unmarshals single objects into a struct", func() { |
| 112 | + var post EnumPost |
| 113 | + err := Unmarshal(singlePostMap, &post) |
| 114 | + Expect(err).ToNot(HaveOccurred()) |
| 115 | + Expect(post).To(Equal(firstPost)) |
| 116 | + }) |
| 117 | + |
| 118 | + It("unmarshals JSON", func() { |
| 119 | + var posts []EnumPost |
| 120 | + err := UnmarshalFromJSON(singleJSON, &posts) |
| 121 | + Expect(err).ToNot(HaveOccurred()) |
| 122 | + Expect(posts).To(Equal([]EnumPost{firstPost})) |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments