-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
57 lines (50 loc) · 1.59 KB
/
models.go
File metadata and controls
57 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package servercow
import "encoding/json"
// record mirrors the Servercow API DNS record structure.
type record struct {
Name string `json:"name"`
Type string `json:"type"`
TTL int `json:"ttl,omitempty"`
Content value `json:"content,omitempty"`
}
// value represents a DNS record's content. The Servercow API accepts either a
// plain JSON string (for single-value records) or a JSON array (for multi-value
// record types such as TXT, CAA, TLSA). This type handles both forms.
type value []string
func (v value) MarshalJSON() ([]byte, error) {
if len(v) == 0 {
// Return nil, nil (not []byte("null")) so that the encoding/json package
// treats this as an absent value when combined with the omitempty struct tag.
// This is intentional: record.Content uses omitempty and should be absent
// from JSON output when there are no values.
return nil, nil
}
if len(v) == 1 {
return json.Marshal(v[0])
}
return json.Marshal([]string(v))
}
func (v *value) UnmarshalJSON(b []byte) error {
// Handle JSON null explicitly.
if string(b) == "null" {
*v = nil
return nil
}
if len(b) > 0 && b[0] == '[' {
return json.Unmarshal(b, (*[]string)(v))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*v = value{s}
return nil
}
// message is the JSON body returned by the Servercow API for POST and DELETE
// operations. Note: the API returns HTTP 200 even for errors; callers must
// inspect ErrorMsg.
type message struct {
Message string `json:"message,omitempty"`
ErrorMsg string `json:"error,omitempty"`
}
func (m message) Error() string { return m.ErrorMsg }