1 package githubv4 2 3 import ( 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 22 type ( 23 // Base64String is a (potentially binary) string encoded using base64. 24 Base64String string 25 26 // Boolean represents true or false values. 27 Boolean graphql.Boolean 28 29 // Date is an ISO-8601 encoded date. 30 Date struct{ time.Time } 31 32 // DateTime is an ISO-8601 encoded UTC date. 33 DateTime struct{ time.Time } 34 35 // Float represents signed double-precision fractional values as 36 // specified by IEEE 754. 37 Float graphql.Float 38 39 // GitObjectID is a Git object ID. For example, 40 // "912ec1990bd09f8fc128c3fa6b59105085aabc03". 41 GitObjectID string 42 43 // GitRefname is a fully qualified reference name (e.g., refs/heads/main). 44 GitRefname string 45 46 // GitTimestamp is an ISO-8601 encoded date. 47 // Unlike the DateTime type, GitTimestamp is not converted in UTC. 48 GitTimestamp struct{ time.Time } 49 50 // HTML is a string containing HTML code. 51 HTML string 52 53 // ID represents a unique identifier that is Base64 obfuscated. It 54 // is often used to refetch an object or as key for a cache. The ID 55 // type appears in a JSON response as a String; however, it is not 56 // intended to be human-readable. When expected as an input type, 57 // any string (such as "VXNlci0xMA==") or integer (such as 4) input 58 // value will be accepted as an ID. 59 ID graphql.ID 60 61 // Int represents non-fractional signed whole numeric values. 62 // Int can represent values between -(2^31) and 2^31 - 1. 63 Int graphql.Int 64 65 // String represents textual data as UTF-8 character sequences. 66 // This type is most often used by GraphQL to represent free-form 67 // human-readable text. 68 String graphql.String 69 70 // URI is an RFC 3986, RFC 3987, and RFC 6570 (level 4) compliant URI. 71 URI struct{ *url.URL } 72 73 // X509Certificate is a valid x509 certificate. 74 X509Certificate struct{ *x509.Certificate } 75 ) 76 77 // MarshalJSON implements the json.Marshaler interface. 78 // The URI is a quoted string. 79 func (u URI) MarshalJSON() ([]byte, error) { 80 return json.Marshal(u.String()) 81 } 82 83 // UnmarshalJSON implements the json.Unmarshaler interface. 84 // The URI is expected to be a quoted string. 85 func (u *URI) UnmarshalJSON(data []byte) error { 86 // Ignore null, like in the main JSON package. 87 if string(data) == "null" { 88 return nil 89 } 90 var s string 91 err := json.Unmarshal(data, &s) 92 if err != nil { 93 return err 94 } 95 u.URL, err = url.Parse(s) 96 return err 97 } 98 99 // MarshalJSON implements the json.Marshaler interface. 100 func (x X509Certificate) MarshalJSON() ([]byte, error) { 101 // TODO: Implement. 102 return nil, fmt.Errorf("X509Certificate.MarshalJSON: not implemented") 103 } 104 105 // UnmarshalJSON implements the json.Unmarshaler interface. 106 func (x *X509Certificate) UnmarshalJSON(data []byte) error { 107 // TODO: Implement. 108 return fmt.Errorf("X509Certificate.UnmarshalJSON: not implemented") 109 } 110 111 // NewBase64String is a helper to make a new *Base64String. 112 func NewBase64String(v Base64String) *Base64String { return &v } 113 114 // NewBoolean is a helper to make a new *Boolean. 115 func NewBoolean(v Boolean) *Boolean { return &v } 116 117 // NewDate is a helper to make a new *Date. 118 func NewDate(v Date) *Date { return &v } 119 120 // NewDateTime is a helper to make a new *DateTime. 121 func NewDateTime(v DateTime) *DateTime { return &v } 122 123 // NewFloat is a helper to make a new *Float. 124 func NewFloat(v Float) *Float { return &v } 125 126 // NewGitObjectID is a helper to make a new *GitObjectID. 127 func NewGitObjectID(v GitObjectID) *GitObjectID { return &v } 128 129 // NewGitRefname is a helper to make a new *GitRefname. 130 func NewGitRefname(v GitRefname) *GitRefname { return &v } 131 132 // NewGitTimestamp is a helper to make a new *GitTimestamp. 133 func NewGitTimestamp(v GitTimestamp) *GitTimestamp { return &v } 134 135 // NewHTML is a helper to make a new *HTML. 136 func NewHTML(v HTML) *HTML { return &v } 137 138 // NewID is a helper to make a new *ID. 139 func NewID(v ID) *ID { return &v } 140 141 // NewInt is a helper to make a new *Int. 142 func NewInt(v Int) *Int { return &v } 143 144 // NewString is a helper to make a new *String. 145 func NewString(v String) *String { return &v } 146 147 // NewURI is a helper to make a new *URI. 148 func NewURI(v URI) *URI { return &v } 149 150 // NewX509Certificate is a helper to make a new *X509Certificate. 151 func NewX509Certificate(v X509Certificate) *X509Certificate { return &v } 152