1package githubv4
2
3import (
4 "crypto/x509"
5 "encoding/json"
6 "fmt"
7 "net/url"
8 "time"
9
10 "github.com/shurcooL/graphql"
11)
12
13// Note: These custom types are meant to be used in queries for now.
14// But the plan is to switch to using native Go types (string, int, bool, time.Time, etc.).
15// See https://github.com/shurcooL/githubv4/issues/9 for details.
16//
17// These custom types currently provide documentation, and their use
18// is required for sending outbound queries. However, native Go types
19// can be used for unmarshaling. Once https://github.com/shurcooL/githubv4/issues/9
20// is resolved, native Go types can completely replace these.
21
22type (
23 // Boolean represents true or false values.
24 Boolean graphql.Boolean
25
26 // Date is an ISO-8601 encoded date.
27 Date struct{ time.Time }
28
29 // DateTime is an ISO-8601 encoded UTC date.
30 DateTime struct{ time.Time }
31
32 // Float represents signed double-precision fractional values as
33 // specified by IEEE 754.
34 Float graphql.Float
35
36 // GitObjectID is a Git object ID. For example,
37 // "912ec1990bd09f8fc128c3fa6b59105085aabc03".
38 GitObjectID string
39
40 // GitTimestamp is an ISO-8601 encoded date.
41 // Unlike the DateTime type, GitTimestamp is not converted in UTC.
42 GitTimestamp struct{ time.Time }
43
44 // HTML is a string containing HTML code.
45 HTML string
46
47 // ID represents a unique identifier that is Base64 obfuscated. It
48 // is often used to refetch an object or as key for a cache. The ID
49 // type appears in a JSON response as a String; however, it is not
50 // intended to be human-readable. When expected as an input type,
51 // any string (such as "VXNlci0xMA==") or integer (such as 4) input
52 // value will be accepted as an ID.
53 ID graphql.ID
54
55 // Int represents non-fractional signed whole numeric values.
56 // Int can represent values between -(2^31) and 2^31 - 1.
57 Int graphql.Int
58
59 // String represents textual data as UTF-8 character sequences.
60 // This type is most often used by GraphQL to represent free-form
61 // human-readable text.
62 String graphql.String
63
64 // URI is an RFC 3986, RFC 3987, and RFC 6570 (level 4) compliant URI.
65 URI struct{ *url.URL }
66
67 // X509Certificate is a valid x509 certificate.
68 X509Certificate struct{ *x509.Certificate }
69)
70
71// MarshalJSON implements the json.Marshaler interface.
72// The URI is a quoted string.
73func (u URI) MarshalJSON() ([]byte, error) {
74 return json.Marshal(u.String())
75}
76
77// UnmarshalJSON implements the json.Unmarshaler interface.
78// The URI is expected to be a quoted string.
79func (u *URI) UnmarshalJSON(data []byte) error {
80 // Ignore null, like in the main JSON package.
81 if string(data) == "null" {
82 return nil
83 }
84 var s string
85 err := json.Unmarshal(data, &s)
86 if err != nil {
87 return err
88 }
89 u.URL, err = url.Parse(s)
90 return err
91}
92
93// MarshalJSON implements the json.Marshaler interface.
94func (x X509Certificate) MarshalJSON() ([]byte, error) {
95 // TODO: Implement.
96 return nil, fmt.Errorf("X509Certificate.MarshalJSON: not implemented")
97}
98
99// UnmarshalJSON implements the json.Unmarshaler interface.
100func (x *X509Certificate) UnmarshalJSON(data []byte) error {
101 // TODO: Implement.
102 return fmt.Errorf("X509Certificate.UnmarshalJSON: not implemented")
103}
104
105// NewBoolean is a helper to make a new *Boolean.
106func NewBoolean(v Boolean) *Boolean { return &v }
107
108// NewDate is a helper to make a new *Date.
109func NewDate(v Date) *Date { return &v }
110
111// NewDateTime is a helper to make a new *DateTime.
112func NewDateTime(v DateTime) *DateTime { return &v }
113
114// NewFloat is a helper to make a new *Float.
115func NewFloat(v Float) *Float { return &v }
116
117// NewGitObjectID is a helper to make a new *GitObjectID.
118func NewGitObjectID(v GitObjectID) *GitObjectID { return &v }
119
120// NewGitTimestamp is a helper to make a new *GitTimestamp.
121func NewGitTimestamp(v GitTimestamp) *GitTimestamp { return &v }
122
123// NewHTML is a helper to make a new *HTML.
124func NewHTML(v HTML) *HTML { return &v }
125
126// NewID is a helper to make a new *ID.
127func NewID(v ID) *ID { return &v }
128
129// NewInt is a helper to make a new *Int.
130func NewInt(v Int) *Int { return &v }
131
132// NewString is a helper to make a new *String.
133func NewString(v String) *String { return &v }
134
135// NewURI is a helper to make a new *URI.
136func NewURI(v URI) *URI { return &v }
137
138// NewX509Certificate is a helper to make a new *X509Certificate.
139func NewX509Certificate(v X509Certificate) *X509Certificate { return &v }