github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/payload.go (about)

     1  package protocol
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"net/http"
     7  
     8  	"github.com/aavshr/aws-sdk-go/aws"
     9  	"github.com/aavshr/aws-sdk-go/aws/client/metadata"
    10  	"github.com/aavshr/aws-sdk-go/aws/request"
    11  )
    12  
    13  // PayloadUnmarshaler provides the interface for unmarshaling a payload's
    14  // reader into a SDK shape.
    15  type PayloadUnmarshaler interface {
    16  	UnmarshalPayload(io.Reader, interface{}) error
    17  }
    18  
    19  // HandlerPayloadUnmarshal implements the PayloadUnmarshaler from a
    20  // HandlerList. This provides the support for unmarshaling a payload reader to
    21  // a shape without needing a SDK request first.
    22  type HandlerPayloadUnmarshal struct {
    23  	Unmarshalers request.HandlerList
    24  }
    25  
    26  // UnmarshalPayload unmarshals the io.Reader payload into the SDK shape using
    27  // the Unmarshalers HandlerList provided. Returns an error if unable
    28  // unmarshaling fails.
    29  func (h HandlerPayloadUnmarshal) UnmarshalPayload(r io.Reader, v interface{}) error {
    30  	req := &request.Request{
    31  		HTTPRequest: &http.Request{},
    32  		HTTPResponse: &http.Response{
    33  			StatusCode: 200,
    34  			Header:     http.Header{},
    35  			Body:       ioutil.NopCloser(r),
    36  		},
    37  		Data: v,
    38  	}
    39  
    40  	h.Unmarshalers.Run(req)
    41  
    42  	return req.Error
    43  }
    44  
    45  // PayloadMarshaler provides the interface for marshaling a SDK shape into and
    46  // io.Writer.
    47  type PayloadMarshaler interface {
    48  	MarshalPayload(io.Writer, interface{}) error
    49  }
    50  
    51  // HandlerPayloadMarshal implements the PayloadMarshaler from a HandlerList.
    52  // This provides support for marshaling a SDK shape into an io.Writer without
    53  // needing a SDK request first.
    54  type HandlerPayloadMarshal struct {
    55  	Marshalers request.HandlerList
    56  }
    57  
    58  // MarshalPayload marshals the SDK shape into the io.Writer using the
    59  // Marshalers HandlerList provided. Returns an error if unable if marshal
    60  // fails.
    61  func (h HandlerPayloadMarshal) MarshalPayload(w io.Writer, v interface{}) error {
    62  	req := request.New(
    63  		aws.Config{},
    64  		metadata.ClientInfo{},
    65  		request.Handlers{},
    66  		nil,
    67  		&request.Operation{HTTPMethod: "PUT"},
    68  		v,
    69  		nil,
    70  	)
    71  
    72  	h.Marshalers.Run(req)
    73  
    74  	if req.Error != nil {
    75  		return req.Error
    76  	}
    77  
    78  	io.Copy(w, req.GetBody())
    79  
    80  	return nil
    81  }