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

Code before:

	obj, err := rdbDeserialize(serializedData)
	if err != nil {
		return diceerrors.NewErrWithMessage("deserialization failed: " + err.Error())
	}

	store.Put(key, obj)
	return []byte("OK")
}

func rdbDeserialize(data []byte) (*object.Obj, error) {
	if len(data) < 2 {
		return nil, errors.New("insufficient data for deserialization")
	}

	objType := data[0] // Object type is the first byte in this format

	switch objType {
	case 0x09: // String type (changed from 0x00 to 0x09 to match the test case)
		return readString(data[1:])
	case 0xC0: // Integer type
		return readInt(data[1:])
	default:
		return nil, errors.New("unsupported object type")
	}
}

func readString(data []byte) (*object.Obj, error) {
	buf := bytes.NewReader(data)
	var strLen uint32
	if err := binary.Read(buf, binary.BigEndian, &strLen); err != nil {
		return nil, err
	}

	strBytes := make([]byte, strLen)
	if _, err := buf.Read(strBytes); err != nil {
		return nil, err
	}

	return &object.Obj{TypeEncoding: object.ObjTypeString, Value: string(strBytes)}, nil
}

func readInt(data []byte) (*object.Obj, error) {
	var intVal int64
	if err := binary.Read(bytes.NewReader(data), binary.BigEndian, &intVal); err != nil {
		return nil, err
	}

	return &object.Obj{TypeEncoding: object.ObjTypeInt, Value: intVal}, nil
}


Review comment (suggestion):
I changed back the string type as I was testing string. Also I was wondering if we can actually use the predefined values of encoding here instead of defining our own. We can use encoding from original key to actually restore in the same type and encoding

```suggestion
func rdbDeserialize(data []byte) (*object.Obj, error) {
	if len(data) < 3 {
		return nil, errors.New("insufficient data for deserialization")
	}

	objType := data[1] // Object type is the first byte in this format

	switch objType {
	case 0x00: // String type (changed from 0x00 to 0x09 to match the test case)
		return readString(data[2:])
	case 0xC0: // Integer type
		return readInt(data[2:])
	default:
		return nil, errors.New("unsupported object type")
	}
}
```

Code after:
	}

	return clientio.Encode(count, false)
}

func evalSCARD(args []string, store *dstore.Store) []byte {
	if len(args) != 1 {
		return diceerrors.NewErrArity("SCARD")
	}

	key := args[0]

	// Get the set object from the store.
	obj := store.Get(key)

	if obj == nil {
		return clientio.Encode(0, false)
	}

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

	if err := object.AssertEncoding(obj.TypeEncoding, object.ObjEncodingSetStr); err != nil {
		return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr)
	}

	// Get the set object.
	count := len(obj.Value.(map[string]struct{}))
	return clientio.Encode(count, false)
}

func evalSDIFF(args []string, store *dstore.Store) []byte {
	if len(args) < 1 {
		return diceerrors.NewErrArity("SDIFF")
	}

	srcKey := args[0]
	obj := store.Get(srcKey)

	// if the source key does not exist, return an empty response
	if obj == nil {
		return clientio.Encode([]string{}, false)
	}

	if err := object.AssertType(obj.TypeEncoding, object.ObjTypeSet); err != nil {
		return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr)
	}

	if err := object.AssertEncoding(obj.TypeEncoding, object.ObjEncodingSetStr); err != nil {