From 34ca1d991f50ef2705aeb2f2cf1a20b76d5c7e0f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 4 Oct 2018 17:05:10 +0200 Subject: [PATCH] add getters for response.Arguments (getUint(), getInt(), getFloat(), get(), getString()) --- err/interface.go | 2 +- response/arguments.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/err/interface.go b/err/interface.go index 6c37fbf..85a758c 100644 --- a/err/interface.go +++ b/err/interface.go @@ -35,7 +35,7 @@ func (e Error) Error() string { } -// MarshalJSON implements the json.Marshaler interface and is used +// MarshalJSON implements the 'json.Marshaler' interface and is used // to generate the JSON representation of the error data func (e Error) MarshalJSON() ([]byte, error) { diff --git a/response/arguments.go b/response/arguments.go index 3e9847c..7a640fe 100644 --- a/response/arguments.go +++ b/response/arguments.go @@ -1,7 +1,89 @@ package response +import ( + "errors" +) + +var ErrUnknownKey = errors.New("key does not exist") +var ErrInvalidType = errors.New("invalid type") + // Has checks whether a key exists in the arguments func (i Arguments) Has(key string) bool { _, exists := i[key] return exists } + +// Get extracts a parameter as an interface{} value +func (i Arguments) Get(key string) (interface{}, error) { + val, ok := i[key] + if !ok { + return 0, ErrUnknownKey + } + + return val, nil +} + +// GetFloat extracts a parameter as a float value +func (i Arguments) GetFloat(key string) (float64, error) { + val, err := i.Get(key) + if err != nil { + return 0, err + } + + floatval, ok := val.(float64) + if !ok { + return 0, ErrInvalidType + } + + return floatval, nil +} + +// GetInt extracts a parameter as an int value +func (i Arguments) GetInt(key string) (int, error) { + floatval, err := i.GetFloat(key) + if err != nil { + return 0, err + } + + return int(floatval), nil +} + +// GetUint extracts a parameter as an uint value +func (i Arguments) GetUint(key string) (uint, error) { + floatval, err := i.GetFloat(key) + if err != nil { + return 0, err + } + + return uint(floatval), nil +} + +// GetString extracts a parameter as a string value +func (i Arguments) GetString(key string) (string, error) { + val, ok := i[key] + if !ok { + return "", ErrUnknownKey + } + + stringval, ok := val.(string) + if !ok { + return "", ErrInvalidType + } + + return stringval, nil +} + +// GetBool extracts a parameter as a bool value +func (i Arguments) GetBool(key string) (bool, error) { + val, ok := i[key] + if !ok { + return false, ErrUnknownKey + } + + boolval, ok := val.(bool) + if !ok { + return false, ErrInvalidType + } + + return boolval, nil +}