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

Code before:
	if hasExpired(obj) {
		return RESP_NIL
	}

	// return the RESP encoded value
	return Encode(obj.Value, false)
}

// evalJSONGET retrieves a JSON value stored at the specified key
// args must contain at least the key;  (path unused in this implementation)
// Returns RESP_NIL if key is expired or it does not exist
// Returns encoded error response if incorrect number of arguments
// The RESP value of the key is encoded and then returned
func evalJSONGET(args []string) []byte {
	// Check for correct number of arguments
	if len(args) < 1 {
		return Encode(errors.New("ERR wrong number of arguments for 'JSON.GET' command"), false)
	}

	key := args[0]

	// Retrieve the object from the database
	obj := Get(key)

	// Return nil if the key doesn't exist or has expired
	if obj == nil || hasExpired(obj) {
		return RESP_NIL
	}

	// Check if the object is of JSON type
	objType, _ := ExtractTypeEncoding(obj)
	if objType != OBJ_TYPE_JSON {
		return Encode(errors.New("WRONGTYPE Operation against a key holding the wrong kind of value"), false)
	}

	// Serialize the JSON value
	jsonBytes, err := json.Marshal(obj.Value)
	if err != nil {
		return Encode(errors.New("ERR could not serialize JSON value"), false)
	}

	// Return the JSON value as an encoded string
	return Encode(string(jsonBytes), false)
}

// evalJSONSET stores a JSON value at the specified key
// args must contain at least the key, path (unused in this implementation), and JSON string
// Returns encoded error response if incorrect number of arguments
// Returns encoded error if the JSON string is invalid
// Returns RESP_OK if the JSON value is successfully stored
func evalJSONSET(args []string) []byte {

Review comment (suggestion):
We don't need to check for key expiry here using `hasExpired()` since `Get()` already internally checks for expiration and returns nil.

Code after:
	}

	// return the RESP encoded value
	return Encode(obj.Value, false)
}

// evalJSONGET retrieves a JSON value stored at the specified key
// args must contain at least the key;  (path unused in this implementation)
// Returns RESP_NIL if key is expired or it does not exist
// Returns encoded error response if incorrect number of arguments
// The RESP value of the key is encoded and then returned
func evalJSONGET(args []string) []byte {
	if len(args) < 1 {
		return Encode(errors.New("ERR wrong number of arguments for 'JSON.GET' command"), false)
	}

	key := args[0]
	obj := Get(key)
	// Return nil if the key doesn't exist
	if obj == nil {
		return RESP_NIL
	}

	objType, _ := ExtractTypeEncoding(obj)
	if objType != OBJ_TYPE_JSON {
		return Encode(errors.New("WRONGTYPE Operation against a key holding the wrong kind of value"), false)
	}

	jsonValue, ok := obj.Value.(*fastjson.Value)
	if !ok {
		return Encode(errors.New("ERR internal error: stored value is not a valid JSON"), false)
	}

	return Encode(string(jsonValue.MarshalTo(nil)), false)
}

// evalJSONSET stores a JSON value at the specified key
// args must contain at least the key, path (unused in this implementation), and JSON string
// Returns encoded error response if incorrect number of arguments
// Returns encoded error if the JSON string is invalid
// Returns RESP_OK if the JSON value is successfully stored
func evalJSONSET(args []string) []byte {
	if len(args) < 3 {
		return Encode(errors.New("ERR wrong number of arguments for 'JSON.SET' command"), false)
	}

	key := args[0]
	// Note: args[1] (path) is ignored in this implementation
	jsonStr := args[2]

	// Parse the JSON string