// Code generated by go-varlink/varlinkgen. DO NOT EDIT.

package stringapi

import (
	"encoding/json"
	govarlink "github.com/emersion/go-varlink"
)

type RepeatIn struct {
	Input string `json:"input"`
}
type RepeatOut struct {
	Output string `json:"output"`
}

type ReverseIn struct {
	Input string `json:"input"`
}
type ReverseOut struct {
	Output string `json:"output"`
}

type Client struct {
	*govarlink.Client
}

func unmarshalError(err error) error {
	verr, ok := err.(*govarlink.ClientError)
	if !ok {
		return err
	}
	var v error
	switch verr.Name {
	default:
		return err
	}
	if err := json.Unmarshal(verr.Parameters, v); err != nil {
		return err
	}
	return v
}
func (c Client) Repeat(in *RepeatIn) (*RepeatOut, error) {
	if in == nil {
		in = new(RepeatIn)
	}
	out := new(RepeatOut)
	err := c.Client.Do("org.example.string.Repeat", in, out)
	return out, unmarshalError(err)
}
func (c Client) Reverse(in *ReverseIn) (*ReverseOut, error) {
	if in == nil {
		in = new(ReverseIn)
	}
	out := new(ReverseOut)
	err := c.Client.Do("org.example.string.Reverse", in, out)
	return out, unmarshalError(err)
}

type Backend interface {
	Repeat(*RepeatIn) (*RepeatOut, error)
	Reverse(*ReverseIn) (*ReverseOut, error)
}

type Handler struct {
	Backend Backend
}

func marshalError(err error) error {
	var name string
	switch err.(type) {
	default:
		return err
	}
	return &govarlink.ServerError{
		Name:       name,
		Parameters: err,
	}
}
func (h Handler) HandleVarlink(call *govarlink.ServerCall, req *govarlink.ServerRequest) error {
	var (
		out interface{}
		err error
	)
	switch req.Method {
	case "org.example.string.Repeat":
		in := new(RepeatIn)
		if err := json.Unmarshal(req.Parameters, in); err != nil {
			return err
		}
		out, err = h.Backend.Repeat(in)
	case "org.example.string.Reverse":
		in := new(ReverseIn)
		if err := json.Unmarshal(req.Parameters, in); err != nil {
			return err
		}
		out, err = h.Backend.Reverse(in)
	default:
		err = &govarlink.ServerError{
			Name:       "org.varlink.service.MethodNotFound",
			Parameters: map[string]string{"method": req.Method},
		}
	}
	if err != nil {
		return marshalError(err)
	}
	return call.CloseWithReply(out)
}

func (h Handler) Register(reg *govarlink.Registry) {
	reg.Add(&govarlink.RegistryInterface{
		Definition: "interface org.example.string\n\nmethod Repeat(\n    input: string\n) -> (output: string)\n\nmethod Reverse(\n    input: string\n) -> (output: string)\n",
		Name:       "org.example.string",
	}, h)
}
