Skip to content

The usage of map looks like array , not map, why this way please? #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
videni opened this issue Apr 3, 2025 · 0 comments
Open

The usage of map looks like array , not map, why this way please? #51

videni opened this issue Apr 3, 2025 · 0 comments

Comments

@videni
Copy link

videni commented Apr 3, 2025

Swift

 var document: YDocument!
    var map: YMap<TestType>!

    override func setUp() {
        document = YDocument()
        map = document.getOrCreateMap(named: "test")
    }

    override func tearDown() {
        document = nil
        map = nil
    }

    func test_insert() {
        let initialInstance = TestType(name: "Aidar", age: 24)
        let secondInstance = TestType(name: "Joe", age: 55)

// the map looks like an array?
        XCTAssertEqual(map.length(), 0)
        map[initialInstance.name] = initialInstance
        map[secondInstance.name] = secondInstance
        XCTAssertEqual(map.length(), 2)

        let finalInstance = map.get(key: initialInstance.name)

        XCTAssertEqual(initialInstance, finalInstance)

        let contains = map.containsKey(secondInstance.name)
        XCTAssertTrue(contains)
    }

Rust

use yrs::{any, Array, ArrayPrelim, Doc, In, JsonPath, JsonPathEval, Map, MapPrelim, Out, Transact, WriteTxn};

let doc = Doc::new();
let mut txn = doc.transact_mut();
let users = txn.get_or_insert_array("users");

// here is  a  map in an array

users.insert(&mut txn, 0, MapPrelim::from([
    ("name".to_string(), In::Any(any!("Alice"))),
    ("surname".into(), In::Any(any!("Smith"))),
    ("age".into(), In::Any(any!(25))),
    (
        "friends".into(),
        In::from(ArrayPrelim::from([
            any!({ "name": "Bob", "nick": "boreas" }),
            any!({ "nick": "crocodile91" }),
        ])),
    ),
]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant