...

Source file src/github.com/go-kit/kit/transport/http/jsonrpc/encode_decode.go

Documentation: github.com/go-kit/kit/transport/http/jsonrpc

     1  package jsonrpc
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/go-kit/kit/endpoint"
     7  
     8  	"context"
     9  )
    10  
    11  // Server-Side Codec
    12  
    13  // EndpointCodec defines a server Endpoint and its associated codecs
    14  type EndpointCodec struct {
    15  	Endpoint endpoint.Endpoint
    16  	Decode   DecodeRequestFunc
    17  	Encode   EncodeResponseFunc
    18  }
    19  
    20  // EndpointCodecMap maps the Request.Method to the proper EndpointCodec
    21  type EndpointCodecMap map[string]EndpointCodec
    22  
    23  // DecodeRequestFunc extracts a user-domain request object from raw JSON
    24  // It's designed to be used in JSON RPC servers, for server-side endpoints.
    25  // One straightforward DecodeRequestFunc could be something that unmarshals
    26  // JSON from the request body to the concrete request type.
    27  type DecodeRequestFunc func(context.Context, json.RawMessage) (request interface{}, err error)
    28  
    29  // EncodeResponseFunc encodes the passed response object to a JSON RPC result.
    30  // It's designed to be used in HTTP servers, for server-side endpoints.
    31  // One straightforward EncodeResponseFunc could be something that JSON encodes
    32  // the object directly.
    33  type EncodeResponseFunc func(context.Context, interface{}) (response json.RawMessage, err error)
    34  
    35  // Client-Side Codec
    36  
    37  // EncodeRequestFunc encodes the given request object to raw JSON.
    38  // It's designed to be used in JSON RPC clients, for client-side
    39  // endpoints. One straightforward EncodeResponseFunc could be something that
    40  // JSON encodes the object directly.
    41  type EncodeRequestFunc func(context.Context, interface{}) (request json.RawMessage, err error)
    42  
    43  // DecodeResponseFunc extracts a user-domain response object from an JSON RPC
    44  // response object. It's designed to be used in JSON RPC clients, for
    45  // client-side endpoints. It is the responsibility of this function to decide
    46  // whether any error present in the JSON RPC response should be surfaced to the
    47  // client endpoint.
    48  type DecodeResponseFunc func(context.Context, Response) (response interface{}, err error)
    49  

View as plain text