Skip to content

Commit

Permalink
fix unexpected type error message in GETDEL and GET to report correct…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
Ehijoe committed Oct 30, 2024
1 parent 6e65fc9 commit d55327a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
73 changes: 48 additions & 25 deletions internal/eval/store_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions internal/eval/type_asserts.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d55327a

Please sign in to comment.