handler

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2021 License: BSD-3-Clause Imports: 9 Imported by: 37

Documentation

Overview

Package handler provides implementations of the jrpc2.Assigner interface, and support for adapting functions to the jrpc2.Handler interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Args

type Args []interface{}

Args is a wrapper that decodes an array of positional parameters into concrete locations.

Unmarshaling a JSON value into an Args value v succeeds if the JSON encodes an array with length len(v), and unmarshaling each subvalue i into the corresponding v[i] succeeds. As a special case, if v[i] == nil the corresponding value is discarded.

Marshaling an Args value v into JSON succeeds if each element of the slice is JSON marshalable, and yields a JSON array of length len(v) containing the JSON values corresponding to the elements of v.

Usage example:

func Handler(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
   var x, y int
   var s string

   if err := req.UnmarshalParams(&handler.Args{&x, &y, &s}); err != nil {
      return nil, err
   }
   // do useful work with x, y, and s
}
Example (Marshal)
bits, err := json.Marshal(Args{1, "foo", false, nil})
if err != nil {
	log.Fatalf("Encoding failed: %v", err)
}
fmt.Println(string(bits))
Output:

[1,"foo",false,null]
Example (Unmarshal)
const input = `[25, false, "apple"]`

var count int
var item string

if err := json.Unmarshal([]byte(input), &Args{&count, nil, &item}); err != nil {
	log.Fatalf("Decoding failed: %v", err)
}
fmt.Printf("count=%d, item=%q\n", count, item)
Output:

count=25, item="apple"

func (Args) MarshalJSON

func (a Args) MarshalJSON() ([]byte, error)

MarshalJSON supports JSON marshaling for a.

func (Args) UnmarshalJSON

func (a Args) UnmarshalJSON(data []byte) error

UnmarshalJSON supports JSON unmarshaling for a.

type Func

type Func func(context.Context, *jrpc2.Request) (interface{}, error)

A Func adapts a function having the correct signature to a jrpc2.Handler.

func New

func New(fn interface{}) Func

New adapts a function to a jrpc2.Handler. The concrete value of fn must be a function with one of the following type signatures:

func(context.Context) error
func(context.Context) Y
func(context.Context) (Y, error)
func(context.Context, X) error
func(context.Context, X) Y
func(context.Context, X) (Y, error)
func(context.Context, ...X) (Y, error)
func(context.Context, *jrpc2.Request) (Y, error)
func(context.Context, *jrpc2.Request) (interface{}, error)

for JSON-marshalable types X and Y. New will panic if the type of fn does not have one of these forms. The resulting method will handle encoding and decoding of JSON and report appropriate errors.

Functions adapted by in this way can obtain the *jrpc2.Request value using the jrpc2.InboundRequest helper on the context value supplied by the server.

func (Func) Handle

func (m Func) Handle(ctx context.Context, req *jrpc2.Request) (interface{}, error)

Handle implements the jrpc2.Handler interface by calling m.

type Map

type Map map[string]jrpc2.Handler

A Map is a trivial implementation of the jrpc2.Assigner interface that looks up method names in a map of static jrpc2.Handler values.

func NewService

func NewService(obj interface{}) Map

NewService adapts the methods of a value to a map from method names to Handler implementations as constructed by New. It will panic if obj has no exported methods with a suitable signature.

func (Map) Assign

func (m Map) Assign(_ context.Context, method string) jrpc2.Handler

Assign implements part of the jrpc2.Assigner interface.

func (Map) Names

func (m Map) Names() []string

Names implements part of the jrpc2.Assigner interface.

type Obj added in v0.6.2

type Obj map[string]interface{}

Obj is a wrapper that maps object fields into concrete locations.

Unmarshaling a JSON text into an Obj value v succeeds if the JSON encodes an object, and unmarshaling the value for each key k of the object into v[k] succeeds. If k does not exist in v, it is ignored.

Marshaling an Obj into JSON works as for an ordinary map.

Example (Unmarshal)
const input = `{"uid": 501, "name": "P. T. Barnum", "tags": [1, 3]}`

var uid int
var name string

if err := json.Unmarshal([]byte(input), &Obj{
	"uid":  &uid,
	"name": &name,
}); err != nil {
	log.Fatalf("Decoding failed: %v", err)
}
fmt.Printf("uid=%d, name=%q\n", uid, name)
Output:

uid=501, name="P. T. Barnum"

func (Obj) UnmarshalJSON added in v0.6.2

func (o Obj) UnmarshalJSON(data []byte) error

UnmarshalJSON supports JSON unmarshaling into o.

type ServiceMap

type ServiceMap map[string]jrpc2.Assigner

A ServiceMap combines multiple assigners into one, permitting a server to export multiple services under different names.

Example:

m := handler.ServiceMap{
  "Foo": handler.NewService(fooService),  // methods Foo.A, Foo.B, etc.
  "Bar": handler.NewService(barService),  // methods Bar.A, Bar.B, etc.
}

func (ServiceMap) Assign

func (m ServiceMap) Assign(ctx context.Context, method string) jrpc2.Handler

Assign splits the inbound method name as Service.Method, and passes the Method portion to the corresponding Service assigner. If method does not have the form Service.Method, or if Service is not set in m, the lookup fails and returns nil.

func (ServiceMap) Names

func (m ServiceMap) Names() []string

Names reports the composed names of all the methods in the service, each having the form Service.Method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL