Skip to content

Commit

Permalink
Make AnyRequest and AnyResponse nil safe interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aviyam181199 committed Feb 4, 2025
1 parent 0f4ddc0 commit 1d46bd1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,35 @@ func NewRequest[T any](message *T) *Request[T] {
// Any returns the concrete request message as an empty interface, so that
// *Request implements the [AnyRequest] interface.
func (r *Request[_]) Any() any {
if r == nil {
return nil
}
return r.Msg
}

// Spec returns a description of this RPC.
func (r *Request[_]) Spec() Spec {
if r == nil {
return Spec{}
}
return r.spec
}

// Peer describes the other party for this RPC.
func (r *Request[_]) Peer() Peer {
if r == nil {
return Peer{}
}
return r.peer
}

// Header returns the HTTP headers for this request. Headers beginning with
// "Connect-" and "Grpc-" are reserved for use by the Connect and gRPC
// protocols: applications may read them but shouldn't write them.
func (r *Request[_]) Header() http.Header {
if r == nil {
return nil
}
if r.header == nil {
r.header = make(http.Header)
}
Expand All @@ -200,6 +212,9 @@ func (r *Request[_]) Header() http.Header {
// if the request was never actually sent to the server (and thus no
// determination ever made about the HTTP method).
func (r *Request[_]) HTTPMethod() string {
if r == nil {
return ""
}
return r.method
}

Expand Down Expand Up @@ -255,13 +270,19 @@ func NewResponse[T any](message *T) *Response[T] {
// Any returns the concrete response message as an empty interface, so that
// *Response implements the [AnyResponse] interface.
func (r *Response[_]) Any() any {
if r == nil {
return nil
}
return r.Msg
}

// Header returns the HTTP headers for this response. Headers beginning with
// "Connect-" and "Grpc-" are reserved for use by the Connect and gRPC
// protocols: applications may read them but shouldn't write them.
func (r *Response[_]) Header() http.Header {
if r == nil {
return nil
}
if r.header == nil {
r.header = make(http.Header)
}
Expand All @@ -276,6 +297,10 @@ func (r *Response[_]) Header() http.Header {
// Connect and gRPC protocols: applications may read them but shouldn't write
// them.
func (r *Response[_]) Trailer() http.Header {
if r == nil {
return nil
}

if r.trailer == nil {
r.trailer = make(http.Header)
}
Expand Down

0 comments on commit 1d46bd1

Please sign in to comment.