• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

manyminds / api2go / 13825020247

13 Mar 2025 01:30AM UTC coverage: 83.995%. First build
13825020247

Pull #30

web-flow
Bump golang.org/x/net from 0.33.0 to 0.36.0

Bumps [golang.org/x/net](https://github.com/golang/net) from 0.33.0 to 0.36.0.
- [Commits](https://github.com/golang/net/compare/v0.33.0...v0.36.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #30: Null value unmarshal

1396 of 1662 relevant lines covered (84.0%)

33.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.92
/jsonapi/data_structs.go
1
package jsonapi
2

3
import (
4
        "bytes"
5
        "encoding/json"
6
        "errors"
7
)
8

9
var objectSuffix = []byte("{")
10
var arraySuffix = []byte("[")
11
var stringSuffix = []byte(`"`)
12

13
// A Document represents a JSON API document as specified here: http://jsonapi.org.
14
type Document struct {
15
        Links    Links                  `json:"links,omitempty"`
16
        Data     *DataContainer         `json:"data"`
17
        Included []Data                 `json:"included,omitempty"`
18
        Meta     map[string]interface{} `json:"meta,omitempty"`
19
}
20

21
// A DataContainer is used to marshal and unmarshal single objects and arrays
22
// of objects.
23
type DataContainer struct {
24
        DataObject *Data
25
        DataArray  []Data
26
}
27

28
// UnmarshalJSON unmarshals the JSON-encoded data to the DataObject field if the
29
// root element is an object or to the DataArray field for arrays.
30
func (c *DataContainer) UnmarshalJSON(payload []byte) error {
49✔
31
        if bytes.HasPrefix(payload, objectSuffix) {
83✔
32
                return json.Unmarshal(payload, &c.DataObject)
34✔
33
        }
34✔
34

35
        if bytes.HasPrefix(payload, arraySuffix) {
29✔
36
                return json.Unmarshal(payload, &c.DataArray)
14✔
37
        }
14✔
38

39
        return errors.New("expected a JSON encoded object or array")
1✔
40
}
41

42
// MarshalJSON returns the JSON encoding of the DataArray field or the DataObject
43
// field. It will return "null" if neither of them is set.
44
func (c *DataContainer) MarshalJSON() ([]byte, error) {
43✔
45
        if c.DataArray != nil {
51✔
46
                return json.Marshal(c.DataArray)
8✔
47
        }
8✔
48

49
        return json.Marshal(c.DataObject)
35✔
50
}
51

52
// Link represents a link for return in the document.
53
type Link struct {
54
        Href string `json:"href"`
55
        Meta Meta   `json:"meta,omitempty"`
56
}
57

58
// UnmarshalJSON marshals a string value into the Href field or marshals an
59
// object value into the whole struct.
60
func (l *Link) UnmarshalJSON(payload []byte) error {
6✔
61
        // Links may be null in certain cases, mainly noted in the JSONAPI spec
6✔
62
        // with pagination links (http://jsonapi.org/format/#fetching-pagination).
6✔
63
        if len(payload) == 4 && string(payload) == "null" {
7✔
64
                return nil
1✔
65
        }
1✔
66

67
        if bytes.HasPrefix(payload, stringSuffix) {
6✔
68
                return json.Unmarshal(payload, &l.Href)
1✔
69
        }
1✔
70

71
        if bytes.HasPrefix(payload, objectSuffix) {
6✔
72
                obj := make(map[string]interface{})
2✔
73
                err := json.Unmarshal(payload, &obj)
2✔
74
                if err != nil {
2✔
75
                        return err
×
76
                }
×
77
                var ok bool
2✔
78
                l.Href, ok = obj["href"].(string)
2✔
79
                if !ok {
3✔
80
                        return errors.New(`link object expects a "href" key`)
1✔
81
                }
1✔
82

83
                l.Meta, _ = obj["meta"].(map[string]interface{})
1✔
84
                return nil
1✔
85
        }
86

87
        return errors.New("expected a JSON encoded string or object")
2✔
88
}
89

90
// MarshalJSON returns the JSON encoding of only the Href field if the Meta
91
// field is empty, otherwise it marshals the whole struct.
92
func (l Link) MarshalJSON() ([]byte, error) {
63✔
93
        if l.Empty() {
64✔
94
                return json.Marshal(nil)
1✔
95
        }
1✔
96
        if len(l.Meta) == 0 {
122✔
97
                return json.Marshal(l.Href)
60✔
98
        }
60✔
99
        return json.Marshal(map[string]interface{}{
2✔
100
                "href": l.Href,
2✔
101
                "meta": l.Meta,
2✔
102
        })
2✔
103
}
104

105
// Empty returns true if the link has no href and no metadata.
106
func (l Link) Empty() bool {
63✔
107
        return len(l.Meta) == 0 && l.Href == ""
63✔
108
}
63✔
109

110
// Links contains a map of custom Link objects as given by an element.
111
type Links map[string]Link
112

113
// Meta contains unstructured metadata
114
type Meta map[string]interface{}
115

116
// Data is a general struct for document data and included data.
117
type Data struct {
118
        Type          string                  `json:"type"`
119
        ID            string                  `json:"id"`
120
        LID           string                  `json:"lid,omitempty"`
121
        Attributes    json.RawMessage         `json:"attributes"`
122
        Relationships map[string]Relationship `json:"relationships,omitempty"`
123
        Links         Links                   `json:"links,omitempty"`
124
        Meta          json.RawMessage         `json:"meta,omitempty"`
125
}
126

127
// Relationship contains reference IDs to the related structs
128
type Relationship struct {
129
        Links Links                      `json:"links,omitempty"`
130
        Data  *RelationshipDataContainer `json:"data,omitempty"`
131
        Meta  map[string]interface{}     `json:"meta,omitempty"`
132
}
133

134
// A RelationshipDataContainer is used to marshal and unmarshal single relationship
135
// objects and arrays of relationship objects.
136
type RelationshipDataContainer struct {
137
        DataObject *Identifier
138
        DataArray  []Identifier
139
}
140

141
// UnmarshalJSON unmarshals the JSON-encoded data to the DataObject field if the
142
// root element is an object or to the DataArray field for arrays.
143
func (c *RelationshipDataContainer) UnmarshalJSON(payload []byte) error {
19✔
144
        if bytes.HasPrefix(payload, objectSuffix) {
26✔
145
                // payload is an object
7✔
146
                return json.Unmarshal(payload, &c.DataObject)
7✔
147
        }
7✔
148

149
        if bytes.HasPrefix(payload, arraySuffix) {
23✔
150
                // payload is an array
11✔
151
                return json.Unmarshal(payload, &c.DataArray)
11✔
152
        }
11✔
153

154
        return errors.New("Invalid json for relationship data array/object")
1✔
155
}
156

157
// MarshalJSON returns the JSON encoding of the DataArray field or the DataObject
158
// field. It will return "null" if neither of them is set.
159
func (c *RelationshipDataContainer) MarshalJSON() ([]byte, error) {
41✔
160
        if c.DataArray != nil {
64✔
161
                return json.Marshal(c.DataArray)
23✔
162
        }
23✔
163
        return json.Marshal(c.DataObject)
18✔
164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc