Skip to content
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

feat: jsonpath experimental support #466

Merged
merged 5 commits into from
Sep 19, 2024
Merged

feat: jsonpath experimental support #466

merged 5 commits into from
Sep 19, 2024

Conversation

zxch3n
Copy link
Member

@zxch3n zxch3n commented Sep 17, 2024

Example

It's gated by a feature in Rust crate and cannot be used in WASM at the moment

fn setup_test_doc() -> LoroDoc {
    let doc = LoroDoc::new();
    let store = doc.get_map("store");

    let books = store.insert_container("book", LoroList::new()).unwrap();
    books
        .insert_container(
            0,
            create_map_from_json(json!({
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95,
                "isbn": "0-553-21311-3"
            })),
        )
        .unwrap();
    books
        .insert_container(
            1,
            create_map_from_json(json!({
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99,
                "isbn": "0-553-21312-1"
            })),
        )
        .unwrap();
    books
        .insert_container(
            2,
            create_map_from_json(json!({
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "price": 8.99,
                "isbn": "0-553-21313-X"
            })),
        )
        .unwrap();
    books
        .insert_container(
            3,
            create_map_from_json(json!({
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "price": 22.99,
                "isbn": "0-395-19395-8"
            })),
        )
        .unwrap();

    store
        .insert_container(
            "bicycle",
            create_map_from_json(json!({
                "color": "red",
                "price": 19.95
            })),
        )
        .unwrap();

    store.insert("expensive", 10).unwrap();

    doc
}

#[test]
fn test_all_authors() -> anyhow::Result<()> {
    let doc = setup_test_doc();
    let ans = doc.jsonpath("$.store.book[*].author")?;
    assert_eq!(
        to_json(ans),
        json!([
            "Nigel Rees",
            "Evelyn Waugh",
            "Herman Melville",
            "J. R. R. Tolkien"
        ])
    );
    Ok(())
}

@zxch3n zxch3n self-assigned this Sep 17, 2024
Copy link
Member

@Leeeon233 Leeeon233 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is all the syntax the same as jsonpath?

@zxch3n
Copy link
Member Author

zxch3n commented Sep 18, 2024

Is all the syntax the same as jsonpath?

In the current supported subset, yes

@zxch3n zxch3n merged commit d460346 into dev Sep 19, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants