Skip to content

Commit

Permalink
Allow empty keys in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
LiraNuna authored and alexjg committed May 9, 2024
1 parent e46cc45 commit fc9b4ab
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
11 changes: 0 additions & 11 deletions javascript/test/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,4 @@ describe("Automerge errors", () => {

assert(error instanceof Error)
})

it("Automerge.from throws an error, not a string", () => {
let error
try {
Automerge.from({ "": "bad key" })
} catch (err) {
error = err
}

assert(error instanceof Error)
})
})
17 changes: 3 additions & 14 deletions javascript/test/legacy_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,9 @@ describe("Automerge", () => {
assert.strictEqual(s1.prop, true)
})

it("should require property names to be valid", () => {
assert.throws(() => {
Automerge.change(s1, "foo", doc => (doc[""] = "x"))
}, /must not be an empty string/)
it("should not error on empty string keys", () => {
s2 = Automerge.change(s1, "set empty string key", doc => (doc[""] = "x"))
assert.strictEqual(s2[""], "x")
})

it("should not allow assignment of unsupported datatypes", () => {
Expand Down Expand Up @@ -681,16 +680,6 @@ describe("Automerge", () => {
assert.strictEqual(s1.textStyle, undefined)
assert.deepStrictEqual(s1, { title: "Hello" })
})

it("should validate field names", () => {
s1 = Automerge.change(s1, doc => (doc.nested = {}))
assert.throws(() => {
Automerge.change(s1, doc => (doc.nested[""] = "x"))
}, /must not be an empty string/)
assert.throws(() => {
Automerge.change(s1, doc => (doc.nested = { "": "x" }))
}, /must not be an empty string/)
})
})

describe("lists", () => {
Expand Down
2 changes: 0 additions & 2 deletions rust/automerge/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub enum AutomergeError {
Deflate(#[source] std::io::Error),
#[error("duplicate seq {0} found for actor {1}")]
DuplicateSeqNumber(u64, ActorId),
#[error("key must not be an empty string")]
EmptyStringKey,
#[error("general failure")]
Fail,
#[error("invalid actor ID `{0}`")]
Expand Down
4 changes: 0 additions & 4 deletions rust/automerge/src/transaction/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,6 @@ impl TransactionInner {
prop: String,
action: OpType,
) -> Result<Option<OpIdx>, AutomergeError> {
if prop.is_empty() {
return Err(AutomergeError::EmptyStringKey);
}

let id = self.next_id();
let prop_index = doc.ops_mut().osd.props.cache(prop.clone());
let key = Key::Map(prop_index);
Expand Down
12 changes: 12 additions & 0 deletions rust/automerge/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,3 +2216,15 @@ fn missing_actors_when_docs_are_forked() {
// error occurs here
doc2.save_and_verify().unwrap();
}

#[test]
fn allows_empty_keys_in_mappings() {
let mut doc = AutoCommit::new();
doc.put(&automerge::ROOT, "", 1).unwrap();
assert_doc!(
&doc,
map! {
"" => { 1 },
}
);
}

0 comments on commit fc9b4ab

Please sign in to comment.