go.uber.org/yarpc@v1.72.1/encoding/json/inbound.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package json
    22  
    23  import (
    24  	"context"
    25  	"encoding/json"
    26  	"reflect"
    27  
    28  	encodingapi "go.uber.org/yarpc/api/encoding"
    29  	"go.uber.org/yarpc/api/transport"
    30  	"go.uber.org/yarpc/pkg/errors"
    31  )
    32  
    33  // jsonHandler adapts a user-provided high-level handler into a transport-level
    34  // Handler.
    35  //
    36  // The wrapped function must already be in the correct format:
    37  //
    38  // 	f(ctx context.Context, body $reqBody) ($resBody, error)
    39  type jsonHandler struct {
    40  	reader  requestReader
    41  	handler reflect.Value
    42  }
    43  
    44  func (h jsonHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) error {
    45  	if err := errors.ExpectEncodings(treq, Encoding); err != nil {
    46  		return err
    47  	}
    48  
    49  	ctx, call := encodingapi.NewInboundCall(ctx)
    50  	if err := call.ReadFromRequest(treq); err != nil {
    51  		return err
    52  	}
    53  
    54  	reqBody, err := h.reader.Read(json.NewDecoder(treq.Body))
    55  	if err != nil {
    56  		return errors.RequestBodyDecodeError(treq, err)
    57  	}
    58  
    59  	results := h.handler.Call([]reflect.Value{reflect.ValueOf(ctx), reqBody})
    60  
    61  	if err := call.WriteToResponse(rw); err != nil {
    62  		return err
    63  	}
    64  
    65  	// we want to return the appErr if it exists as this is what
    66  	// the previous behavior was so we deprioritize this error
    67  	var encodeErr error
    68  	if result := results[0].Interface(); result != nil {
    69  		if err := json.NewEncoder(rw).Encode(result); err != nil {
    70  			encodeErr = errors.ResponseBodyEncodeError(treq, err)
    71  		}
    72  	}
    73  
    74  	if appErr, _ := results[1].Interface().(error); appErr != nil {
    75  		rw.SetApplicationError()
    76  		return appErr
    77  	}
    78  
    79  	return encodeErr
    80  }
    81  
    82  func (h jsonHandler) HandleOneway(ctx context.Context, treq *transport.Request) error {
    83  	if err := errors.ExpectEncodings(treq, Encoding); err != nil {
    84  		return err
    85  	}
    86  
    87  	ctx, call := encodingapi.NewInboundCall(ctx)
    88  	if err := call.ReadFromRequest(treq); err != nil {
    89  		return err
    90  	}
    91  
    92  	reqBody, err := h.reader.Read(json.NewDecoder(treq.Body))
    93  	if err != nil {
    94  		return errors.RequestBodyDecodeError(treq, err)
    95  	}
    96  
    97  	results := h.handler.Call([]reflect.Value{reflect.ValueOf(ctx), reqBody})
    98  
    99  	if err := results[0].Interface(); err != nil {
   100  		return err.(error)
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  // requestReader is used to parse a JSON request argument from a JSON decoder.
   107  type requestReader interface {
   108  	Read(*json.Decoder) (reflect.Value, error)
   109  }
   110  
   111  type structReader struct {
   112  	// Type of the struct (not a pointer to the struct)
   113  	Type reflect.Type
   114  }
   115  
   116  func (r structReader) Read(d *json.Decoder) (reflect.Value, error) {
   117  	value := reflect.New(r.Type)
   118  	err := d.Decode(value.Interface())
   119  	return value, err
   120  }
   121  
   122  type mapReader struct {
   123  	Type reflect.Type // Type of the map
   124  }
   125  
   126  func (r mapReader) Read(d *json.Decoder) (reflect.Value, error) {
   127  	value := reflect.New(r.Type)
   128  	err := d.Decode(value.Interface())
   129  	return value.Elem(), err
   130  }
   131  
   132  type ifaceEmptyReader struct{}
   133  
   134  func (ifaceEmptyReader) Read(d *json.Decoder) (reflect.Value, error) {
   135  	value := reflect.New(_interfaceEmptyType)
   136  	err := d.Decode(value.Interface())
   137  	return value.Elem(), err
   138  }