Skip to content

Commit

Permalink
Support bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 5, 2024
1 parent 3860a73 commit bd28f1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/type_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pyo3::prelude::*;
use pyo3::types::{IntoPyDict, PyAny, PyBool, PyByteArray, PyDict, PyFloat, PyList, PyLong, PyString};
use pyo3::types::{IntoPyDict, PyAny, PyBool, PyByteArray, PyDict, PyFloat, PyList, PyLong, PyString, PyBytes};
use yrs::types::{Attrs, Change, EntryChange, Delta, Events, Path, PathSegment, Value};
use yrs::{Any, TransactionMut};
use std::ops::Deref;
Expand Down Expand Up @@ -201,6 +201,9 @@ impl ToPython for Any {
pub fn py_to_any(value: &PyAny) -> Any {
if value.is_none() {
Any::Null
} else if value.is_instance_of::<PyBytes>() {
let v: &[u8] = value.extract().unwrap();
Any::Buffer(v.into())
} else if value.is_instance_of::<PyString>() {
let v: &str = value.extract().unwrap();
Any::String(v.into())
Expand Down
5 changes: 5 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from pycrdt import Array, Doc, Map, Text


def test_binary_entry():
doc = Doc({"m": Map({"bytes": b"012"})})
assert doc["m"]["bytes"] == b"012"


def test_str():
doc = Doc()
map2 = Map({"key2": "val2"})
Expand Down

0 comments on commit bd28f1d

Please sign in to comment.