diff --git a/internal/eval/store_eval.go b/internal/eval/store_eval.go index 5a1366926..379876ded 100644 --- a/internal/eval/store_eval.go +++ b/internal/eval/store_eval.go @@ -229,29 +229,40 @@ func evalGET(args []string, store *dstore.Store) *EvalResponse { switch _, oEnc := object.ExtractTypeEncoding(obj); oEnc { case object.ObjEncodingInt: // Value is stored as an int64, so use type assertion - if val, ok := obj.Value.(int64); ok { + if IsInt64(obj.Value) { return &EvalResponse{ - Result: val, + Result: obj.Value, Error: nil, } - } - - return &EvalResponse{ - Result: nil, - Error: diceerrors.ErrUnexpectedType("int64", obj.Value), + } else if IsString(obj.Value) { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("int64", "string"), + } + } else { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("int64", "unkown"), + } } case object.ObjEncodingEmbStr, object.ObjEncodingRaw: // Value is stored as a string, use type assertion - if val, ok := obj.Value.(string); ok { + if IsString(obj.Value) { return &EvalResponse{ - Result: val, + Result: obj.Value, Error: nil, } - } - return &EvalResponse{ - Result: nil, - Error: diceerrors.ErrUnexpectedType("string", obj.Value), + } else if IsInt64(obj.Value) { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("string", "int64"), + } + } else { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("string", "unkown"), + } } case object.ObjEncodingByteArray: @@ -3182,28 +3193,40 @@ func evalGETDEL(args []string, store *dstore.Store) *EvalResponse { switch _, oEnc := object.ExtractTypeEncoding(objVal); oEnc { case object.ObjEncodingInt: // Value is stored as an int64, so use type assertion - if val, ok := objVal.Value.(int64); ok { + if IsInt64(objVal.Value) { return &EvalResponse{ - Result: val, + Result: objVal.Value, Error: nil, } - } - return &EvalResponse{ - Result: nil, - Error: diceerrors.ErrUnexpectedType("int64", obj.Value), + } else if IsString(objVal.Value) { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("int64", "string"), + } + } else { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("int64", "unkown"), + } } case object.ObjEncodingEmbStr, object.ObjEncodingRaw: // Value is stored as a string, use type assertion - if val, ok := objVal.Value.(string); ok { + if IsString(objVal.Value) { return &EvalResponse{ - Result: val, + Result: objVal.Value, Error: nil, } - } - return &EvalResponse{ - Result: nil, - Error: diceerrors.ErrUnexpectedType("string", obj.Value), + } else if IsInt64(objVal.Value) { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("string", "int64"), + } + } else { + return &EvalResponse{ + Result: nil, + Error: diceerrors.ErrUnexpectedType("string", "unkown"), + } } case object.ObjEncodingByteArray: diff --git a/internal/eval/type_asserts.go b/internal/eval/type_asserts.go new file mode 100644 index 000000000..ef159ffba --- /dev/null +++ b/internal/eval/type_asserts.go @@ -0,0 +1,13 @@ +package eval + +// IsInt64 checks if the variable is of type int64. +func IsInt64(v interface{}) bool { + _, ok := v.(int64) + return ok +} + +// IsString checks if the variable is of type string. +func IsString(v interface{}) bool { + _, ok := v.(string) + return ok +}