Skip to main content

Module json_keep

Module json_keep 

Source
Expand description

Saving a JSON file this build reads without losing what it could not read.

A file under the person’s configuration is read into a typed value, changed, and written back whole. When the typed read gives up on a piece (an entry a newer AgentMux wrote with a kind this build has never heard of, a topic it does not know) and the caller then saves the rest, the piece is gone from disk: a downgrade, or one build that is a version behind, quietly deletes what the newer one kept. These helpers keep that piece: an entry that does not read is written back exactly as it was, and a file that does not read at all is copied aside before it is replaced, never simply overwritten.

Functions§

carry_unknown_keys
Copies into the object new every key of the object old that new lacks and that is not one of known (the keys this build owns: one of them missing from new was removed on purpose). What is left is what a newer AgentMux wrote.
carry_unknown_keys_in_list
carry_unknown_keys for each object of the list new, from the object of the list old that has the same id_key.
has_record
Whether the file holds a record under key, readable by this build or not.
read_entries
A file that is a JSON object of records, each read on its own: the records that read, and none of the ones that do not. A missing or unreadable file is empty.
read_old
The bytes of path as JSON, when it is JSON; the file as it was before this save, so the keys a typed value dropped can be carried over from it.
set_aside_if_unreadable
When path holds bytes that are not a T, copies them to <path>.unreadable so the next save of the file does not destroy them. Nothing when the file is absent or reads fine. Best effort: a copy that fails is logged, not an error of the save.
write_entries
Writes entries as the file’s records, plus every record already in the file that this build cannot read (they are not in entries because they never loaded, so the caller did not choose to drop them), exactly as they are. A file that is not an object of records is copied aside first. Written to a temp file and renamed.
write_entries_removing
write_entries for a save that deletes records on purpose: each key of removed is dropped from the file even when this build cannot read its record (an uninstall must revoke what it names, not leave it for a later build to find).