You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
829 B
Go

package wire
import (
"encoding/json"
"fmt"
)
type Response struct {
Re int
Body Value
}
func (r Response) MarshalJSON() ([]byte, error) {
return json.Marshal([3]interface{}{r.Re, r.Body.NetTag(), r.Body})
}
func (r *Response) UnmarshalJSON(b []byte) error {
var parts [3]json.RawMessage
if err := json.Unmarshal(b, &parts); err != nil {
return err
}
if err := json.Unmarshal(parts[0], &r.Re); err != nil {
return err
}
var tag string
if err := json.Unmarshal(parts[1], &tag); err != nil {
return err
}
f, ok := registry[tag]
if !ok {
return fmt.Errorf("unknown tag: %q", tag)
}
v := f()
if err := json.Unmarshal(parts[2], v); err != nil {
return err
}
r.Body = v
return nil
}
func ErrorResponse(re int, t string, args ...interface{}) Response {
return Response{re, Errorf(t, args...)}
}