From 55b9689950dbb3f47e2d4720d04d2539243429bd Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 15:53:48 +0200 Subject: rename messages to types --- rpc/messages.go | 220 --------------------------------------------------- rpc/messages_test.go | 50 ------------ rpc/types.go | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++ rpc/types_test.go | 50 ++++++++++++ 4 files changed, 270 insertions(+), 270 deletions(-) delete mode 100644 rpc/messages.go delete mode 100644 rpc/messages_test.go create mode 100644 rpc/types.go create mode 100644 rpc/types_test.go diff --git a/rpc/messages.go b/rpc/messages.go deleted file mode 100644 index 549268cce..000000000 --- a/rpc/messages.go +++ /dev/null @@ -1,220 +0,0 @@ -/* - This file is part of go-ethereum - - go-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - go-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with go-ethereum. If not, see . -*/ -package rpc - -import ( - "encoding/json" - "fmt" - "math/big" - "strings" - - "github.com/ethereum/go-ethereum/common" -) - -type hexdata struct { - data []byte -} - -func (d *hexdata) String() string { - return "0x" + common.Bytes2Hex(d.data) -} - -func (d *hexdata) MarshalJSON() ([]byte, error) { - return json.Marshal(d.String()) -} - -func (d *hexdata) UnmarshalJSON(b []byte) (err error) { - d.data = common.FromHex(string(b)) - return nil -} - -func newHexData(input interface{}) *hexdata { - d := new(hexdata) - - if input == nil { - d.data = nil - } - switch input := input.(type) { - case []byte: - d.data = input - case common.Hash: - d.data = input.Bytes() - case *common.Hash: - d.data = input.Bytes() - case common.Address: - d.data = input.Bytes() - case *common.Address: - d.data = input.Bytes() - case *big.Int: - d.data = input.Bytes() - case int64: - d.data = big.NewInt(input).Bytes() - case uint64: - d.data = big.NewInt(int64(input)).Bytes() - case int: - d.data = big.NewInt(int64(input)).Bytes() - case uint: - d.data = big.NewInt(int64(input)).Bytes() - case string: // hexstring - d.data = common.Big(input).Bytes() - default: - d.data = nil - } - - return d -} - -type hexnum struct { - data []byte -} - -func (d *hexnum) String() string { - // Get hex string from bytes - out := common.Bytes2Hex(d.data) - // Trim leading 0s - out = strings.Trim(out, "0") - // Output "0x0" when value is 0 - if len(out) == 0 { - out = "0" - } - return "0x" + out -} - -func (d *hexnum) MarshalJSON() ([]byte, error) { - return json.Marshal(d.String()) -} - -func (d *hexnum) UnmarshalJSON(b []byte) (err error) { - d.data = common.FromHex(string(b)) - return nil -} - -func newHexNum(input interface{}) *hexnum { - d := new(hexnum) - - d.data = newHexData(input).data - - return d -} - -type RpcConfig struct { - ListenAddress string - ListenPort uint - CorsDomain string -} - -type InvalidTypeError struct { - method string - msg string -} - -func (e *InvalidTypeError) Error() string { - return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg) -} - -func NewInvalidTypeError(method, msg string) *InvalidTypeError { - return &InvalidTypeError{ - method: method, - msg: msg, - } -} - -type InsufficientParamsError struct { - have int - want int -} - -func (e *InsufficientParamsError) Error() string { - return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have) -} - -func NewInsufficientParamsError(have int, want int) *InsufficientParamsError { - return &InsufficientParamsError{ - have: have, - want: want, - } -} - -type NotImplementedError struct { - Method string -} - -func (e *NotImplementedError) Error() string { - return fmt.Sprintf("%s method not implemented", e.Method) -} - -func NewNotImplementedError(method string) *NotImplementedError { - return &NotImplementedError{ - Method: method, - } -} - -type DecodeParamError struct { - err string -} - -func (e *DecodeParamError) Error() string { - return fmt.Sprintf("could not decode, %s", e.err) - -} - -func NewDecodeParamError(errstr string) error { - return &DecodeParamError{ - err: errstr, - } -} - -type ValidationError struct { - ParamName string - msg string -} - -func (e *ValidationError) Error() string { - return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg) -} - -func NewValidationError(param string, msg string) error { - return &ValidationError{ - ParamName: param, - msg: msg, - } -} - -type RpcRequest struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params json.RawMessage `json:"params"` -} - -type RpcSuccessResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Result interface{} `json:"result"` -} - -type RpcErrorResponse struct { - Id interface{} `json:"id"` - Jsonrpc string `json:"jsonrpc"` - Error *RpcErrorObject `json:"error"` -} - -type RpcErrorObject struct { - Code int `json:"code"` - Message string `json:"message"` - // Data interface{} `json:"data"` -} diff --git a/rpc/messages_test.go b/rpc/messages_test.go deleted file mode 100644 index 91f0152dc..000000000 --- a/rpc/messages_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package rpc - -import ( - "testing" -) - -func TestInvalidTypeError(t *testing.T) { - err := NewInvalidTypeError("testField", "not string") - expected := "invalid type on field testField: not string" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestInsufficientParamsError(t *testing.T) { - err := NewInsufficientParamsError(0, 1) - expected := "insufficient params, want 1 have 0" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestNotImplementedError(t *testing.T) { - err := NewNotImplementedError("foo") - expected := "foo method not implemented" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestDecodeParamError(t *testing.T) { - err := NewDecodeParamError("foo") - expected := "could not decode, foo" - - if err.Error() != expected { - t.Error(err.Error()) - } -} - -func TestValidationError(t *testing.T) { - err := NewValidationError("foo", "should be `bar`") - expected := "foo not valid, should be `bar`" - - if err.Error() != expected { - t.Error(err.Error()) - } -} diff --git a/rpc/types.go b/rpc/types.go new file mode 100644 index 000000000..549268cce --- /dev/null +++ b/rpc/types.go @@ -0,0 +1,220 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with go-ethereum. If not, see . +*/ +package rpc + +import ( + "encoding/json" + "fmt" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/common" +) + +type hexdata struct { + data []byte +} + +func (d *hexdata) String() string { + return "0x" + common.Bytes2Hex(d.data) +} + +func (d *hexdata) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +func (d *hexdata) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexData(input interface{}) *hexdata { + d := new(hexdata) + + if input == nil { + d.data = nil + } + switch input := input.(type) { + case []byte: + d.data = input + case common.Hash: + d.data = input.Bytes() + case *common.Hash: + d.data = input.Bytes() + case common.Address: + d.data = input.Bytes() + case *common.Address: + d.data = input.Bytes() + case *big.Int: + d.data = input.Bytes() + case int64: + d.data = big.NewInt(input).Bytes() + case uint64: + d.data = big.NewInt(int64(input)).Bytes() + case int: + d.data = big.NewInt(int64(input)).Bytes() + case uint: + d.data = big.NewInt(int64(input)).Bytes() + case string: // hexstring + d.data = common.Big(input).Bytes() + default: + d.data = nil + } + + return d +} + +type hexnum struct { + data []byte +} + +func (d *hexnum) String() string { + // Get hex string from bytes + out := common.Bytes2Hex(d.data) + // Trim leading 0s + out = strings.Trim(out, "0") + // Output "0x0" when value is 0 + if len(out) == 0 { + out = "0" + } + return "0x" + out +} + +func (d *hexnum) MarshalJSON() ([]byte, error) { + return json.Marshal(d.String()) +} + +func (d *hexnum) UnmarshalJSON(b []byte) (err error) { + d.data = common.FromHex(string(b)) + return nil +} + +func newHexNum(input interface{}) *hexnum { + d := new(hexnum) + + d.data = newHexData(input).data + + return d +} + +type RpcConfig struct { + ListenAddress string + ListenPort uint + CorsDomain string +} + +type InvalidTypeError struct { + method string + msg string +} + +func (e *InvalidTypeError) Error() string { + return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg) +} + +func NewInvalidTypeError(method, msg string) *InvalidTypeError { + return &InvalidTypeError{ + method: method, + msg: msg, + } +} + +type InsufficientParamsError struct { + have int + want int +} + +func (e *InsufficientParamsError) Error() string { + return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have) +} + +func NewInsufficientParamsError(have int, want int) *InsufficientParamsError { + return &InsufficientParamsError{ + have: have, + want: want, + } +} + +type NotImplementedError struct { + Method string +} + +func (e *NotImplementedError) Error() string { + return fmt.Sprintf("%s method not implemented", e.Method) +} + +func NewNotImplementedError(method string) *NotImplementedError { + return &NotImplementedError{ + Method: method, + } +} + +type DecodeParamError struct { + err string +} + +func (e *DecodeParamError) Error() string { + return fmt.Sprintf("could not decode, %s", e.err) + +} + +func NewDecodeParamError(errstr string) error { + return &DecodeParamError{ + err: errstr, + } +} + +type ValidationError struct { + ParamName string + msg string +} + +func (e *ValidationError) Error() string { + return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg) +} + +func NewValidationError(param string, msg string) error { + return &ValidationError{ + ParamName: param, + msg: msg, + } +} + +type RpcRequest struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` +} + +type RpcSuccessResponse struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Result interface{} `json:"result"` +} + +type RpcErrorResponse struct { + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` + Error *RpcErrorObject `json:"error"` +} + +type RpcErrorObject struct { + Code int `json:"code"` + Message string `json:"message"` + // Data interface{} `json:"data"` +} diff --git a/rpc/types_test.go b/rpc/types_test.go new file mode 100644 index 000000000..91f0152dc --- /dev/null +++ b/rpc/types_test.go @@ -0,0 +1,50 @@ +package rpc + +import ( + "testing" +) + +func TestInvalidTypeError(t *testing.T) { + err := NewInvalidTypeError("testField", "not string") + expected := "invalid type on field testField: not string" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestInsufficientParamsError(t *testing.T) { + err := NewInsufficientParamsError(0, 1) + expected := "insufficient params, want 1 have 0" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestNotImplementedError(t *testing.T) { + err := NewNotImplementedError("foo") + expected := "foo method not implemented" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestDecodeParamError(t *testing.T) { + err := NewDecodeParamError("foo") + expected := "could not decode, foo" + + if err.Error() != expected { + t.Error(err.Error()) + } +} + +func TestValidationError(t *testing.T) { + err := NewValidationError("foo", "should be `bar`") + expected := "foo not valid, should be `bar`" + + if err.Error() != expected { + t.Error(err.Error()) + } +} -- cgit