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

Encode nil values into the document #190

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

marionauta
Copy link
Contributor

The encoder was omitting some nil values, making them undefined in the JavaScript library, instead of null.

Given the following code:

struct Item: Codable {
    var completedAt: Date?

    func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(completedAt, forKey: .completedAt)
    }
}

let item = Item(completedAt: nil)
let doc = Document()
let encoder = AutomergeEncoder(doc: doc)
try encoder.encode(item)
let completedAt = try doc.get(obj: .ROOT, key: "completedAt")
print(completedAt)

Results in completedAt being nil, instead of ScalarValue.Null. This means the encoder wasn't writing the null value into the Document.


This PR changes the encoder so it writes nils into the Document, with tests to check it.

As an implementation detail, now ScalarValue conforms to ScalarValueRepresentable.

The encoder was omitting some `nil` values, making them
`undefined` in the JavaScript library, instead of `null`.
@heckj heckj self-requested a review July 10, 2024 23:07
Copy link
Collaborator

@heckj heckj left a comment

Choose a reason for hiding this comment

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

Looks great, Mario! Thank you for the detailed tests hitting not just a nil parameter in a map, but an array of potentially nil items as well.

@heckj heckj merged commit 354f681 into automerge:main Jul 11, 2024
4 checks passed
@marionauta marionauta deleted the fix/encode-null-scalar branch July 12, 2024 10:17
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