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

Code before:
        return diceerrors.NewErrWithFormattedMessage("-ERR could not perform this operation on a key that doesn't exist")
    }

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

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

    toggleResults := []interface{}{}
    modified := false

    _, err = expr.Modify(jsonData, func(value interface{}) (interface{}, bool) {
        if boolValue, ok := value.(bool); ok {
            newValue := !boolValue
            toggleResults = append(toggleResults, boolToInt(newValue))
            modified = true
            return newValue, true
        }
        toggleResults = append(toggleResults, nil)
        return value, false
    })

    if err != nil {
        return diceerrors.NewErrWithMessage("failed to toggle values")
    }

    if modified {
        newObj := &object.Obj{
            Value:        jsonData,
            TypeEncoding: object.ObjTypeJSON,
        }
        exp, ok := dstore.GetExpiry(obj, store)
        var exDurationMs int64 = -1
        if ok {
            exDurationMs = int64(exp - uint64(utils.GetCurrentTime().UnixMilli()))
        }
        if exDurationMs > 0 {
            store.SetExpiry(newObj, exDurationMs)
        }
        store.Put(key, newObj)
    }

    toggleResults = reverseResults(toggleResults)
    return clientio.Encode(toggleResults, false)
}

Review comment (suggestion):
nit: You can improve readability by making the smaller if-blocks.
```suggestion
        boolValue, ok := value.(bool)
        if !ok {
            toggleResults = append(toggleResults, nil)
            return value, false
        }
        newValue := !boolValue
        toggleResults = append(toggleResults, boolToInt(newValue))
        modified = true
        return newValue, true
```

Code after:
				return []interface{}{}, true
			}
		case utils.ObjectType:
			if element != struct{}{} {
				countClear++
				return struct{}{}, true
			}
		default:
			return element, false
		}
		return
	})
	if err != nil {
		return diceerrors.NewErrWithMessage(err.Error())
	}
	// Create a new object with the updated JSON data
	newObj := store.NewObj(jsonData, -1, object.ObjTypeJSON, object.ObjEncodingJSON)
	store.Put(key, newObj)
	return clientio.Encode(countClear, false)
}

// evalJSONTYPE retrieves a JSON value type stored at the specified key
// args must contain at least the key;  (path unused in this implementation)
// Returns response.RespNIL if key is expired or it does not exist
// Returns encoded error response if incorrect number of arguments
// The RESP value of the key's value type is encoded and then returned
func evalJSONTYPE(args []string, store *dstore.Store) []byte {
	if len(args) < 1 {
		return diceerrors.NewErrArity("JSON.TYPE")
	}
	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 clientio.RespNIL
	}

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

	jsonData := obj.Value

	if path == defaultRootPath {