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

Code before:
	return Encode(obj.Value, false)
}

// evalJSONDEL delete a value that the given json path include in.
// Returns RespZero if key is expired or it does not exist
// Returns encoded error response if incorrect number of arguments
// Returns an integer reply specified as the number of paths deleted (0 or more)
func evalJSONDEL(args []string, store *Store) []byte {
	if len(args) < 1 {
		return diceerrors.NewErrArity("JSON.DEL")
	}
	key := args[0]

	// Default path is root if not specified
	path := defaultRootPath
	if len(args) > 1 {
		path = args[1]
	}

	// Retrieve the object from the database
	obj := store.Get(key)
	if obj == nil {
		return RespZero
	}

	err := assertType(obj.TypeEncoding, ObjTypeJSON)
	if err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}
	err = assertEncoding(obj.TypeEncoding, ObjEncodingJSON)
	if err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}

	jsonData := obj.Value

	_, err = sonic.Marshal(jsonData)
	if err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}

	if len(args) == 1 || path == defaultRootPath {
		store.Del(key)
		return RespOne
	}

	expr, err := jp.ParseString(path)
	if err != nil {
		return diceerrors.NewErrWithMessage("invalid JSONPath")
	}
	results := expr.Get(jsonData)

Review comment (bug):
L346-L353 has similar sort of code and is used in another places as well example `JSON.CLEAR, JSON.GET` etc. May be we can extract out as function like below:
```
func assertTypeAndEncoding(typeEncoding, expectedType, expectedEncoding uint8) error {
	if err := assertType(typeEncoding, expectedType); err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}
	if err := assertEncoding(typeEncoding, expectedEncoding); err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}
	return nil
}
```

Code after:
	return Encode(obj.Value, false)
}

// evalJSONDEL delete a value that the given json path include in.
// Returns RespZero if key is expired or it does not exist
// Returns encoded error response if incorrect number of arguments
// Returns an integer reply specified as the number of paths deleted (0 or more)
func evalJSONDEL(args []string, store *Store) []byte {
	if len(args) < 1 {
		return diceerrors.NewErrArity("JSON.DEL")
	}
	key := args[0]

	// Default path is root if not specified
	path := defaultRootPath
	if len(args) > 1 {
		path = args[1]
	}

	// Retrieve the object from the database
	obj := store.Get(key)
	if obj == nil {
		return RespZero
	}

	errWithMessage := assertTypeAndEncoding(obj.TypeEncoding, ObjTypeJSON, ObjEncodingJSON)
	if errWithMessage != nil {
		return errWithMessage
	}

	jsonData := obj.Value

	_, err := sonic.Marshal(jsonData)
	if err != nil {
		return diceerrors.NewErrWithMessage("Existing key has wrong Dice type")
	}

	if len(args) == 1 || path == defaultRootPath {
		store.Del(key)
		return RespOne
	}

	expr, err := jp.ParseString(path)
	if err != nil {
		return diceerrors.NewErrWithMessage("invalid JSONPath")
	}
	results := expr.Get(jsonData)
	err = expr.Del(jsonData)
	if err != nil {
		return diceerrors.NewErrWithMessage(err.Error())
	}