Repository: DiceDB/dice
File: core/eval.go (Go)

Code before:

	store.PutAll(insertMap)
	return RespOK
}

// evalGET returns the value for the queried key in args
// The key should be the only param in args
// The RESP value of the key is encoded and then returned
// evalGET returns RespNIL if key is expired or it does not exist
func evalGET(args []string, store *Store) []byte {
	if len(args) != 1 {
		return diceerrors.NewErrArity("GET")
	}

	var key = args[0]

	obj := store.Get(key)

	// if key does not exist, return RESP encoded nil
	if obj == nil {
		return RespNIL
	}

	// If the object exists, check if it is a set object.
	if err := assertType(obj.TypeEncoding, ObjTypeSet); err == nil {
		return diceerrors.NewErrSetType()
	}

	// Decode and return the value based on its encoding
	switch _, oEnc := ExtractTypeEncoding(obj); oEnc {
	case ObjEncodingInt:
		// Value is stored as an int64, so use type assertion
		if val, ok := obj.Value.(int64); ok {
			return Encode(val, false)
		}
		return diceerrors.NewErrWithFormattedMessage("expected int64 but got another type: %s", obj.Value)

	case ObjEncodingEmbStr, ObjEncodingRaw:
		// Value is stored as a string, use type assertion
		if val, ok := obj.Value.(string); ok {
			return Encode(val, false)
		}
		return diceerrors.NewErrWithMessage("expected string but got another type")

	default:
		return diceerrors.NewErrWithMessage(diceerrors.WrongTypeErr)
	}
}

// evalDBSIZE returns the number of keys in the database.
func evalDBSIZE(args []string, store *Store) []byte {

Review comment (bug):
Please remove this function as encoding is to be done as part of `toEncodedMessage()`.
`WrongTypeErr` is already used at multiple places, we can follow the same pattern.
Ref: `return diceerrors.NewErrWithMessage(diceerrors.WrongTypeErr)`

Code after:

	store.PutAll(insertMap)
	return RespOK
}

// evalGET returns the value for the queried key in args
// The key should be the only param in args
// The RESP value of the key is encoded and then returned
// evalGET returns RespNIL if key is expired or it does not exist
func evalGET(args []string, store *Store) []byte {
	if len(args) != 1 {
		return diceerrors.NewErrArity("GET")
	}

	var key = args[0]

	obj := store.Get(key)

	// if key does not exist, return RESP encoded nil
	if obj == nil {
		return RespNIL
	}

	// Decode and return the value based on its encoding
	switch _, oEnc := ExtractTypeEncoding(obj); oEnc {
	case ObjEncodingInt:
		// Value is stored as an int64, so use type assertion
		if val, ok := obj.Value.(int64); ok {
			return Encode(val, false)
		}
		return diceerrors.NewErrWithFormattedMessage("expected int64 but got another type: %s", obj.Value)

	case ObjEncodingEmbStr, ObjEncodingRaw:
		// Value is stored as a string, use type assertion
		if val, ok := obj.Value.(string); ok {
			return Encode(val, false)
		}
		return diceerrors.NewErrWithMessage("expected string but got another type")

	default:
		return diceerrors.NewErrWithMessage(diceerrors.WrongTypeErr)
	}
}

// evalDBSIZE returns the number of keys in the database.
func evalDBSIZE(args []string, store *Store) []byte {
	if len(args) > 0 {
		return diceerrors.NewErrArity("DBSIZE")
	}

	// return the RESP encoded value