Skip to content

Commit

Permalink
fix(records): return flat map of records with name keys
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Nov 12, 2024
1 parent 30824b5 commit e1f8b75
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Refactored handlers to use a util that codifies responses on calls.
- Added documentation with luadoc types for improved linting.
- Records are now returned as an alphabetically sorted array of [undername, {transactionId, ttlSeconds}] with the '@' record being the first.
- Records are now returned as an alphabetically sorted array of [{name, transactionId, ttlSeconds}] with the '@' record being the first.

### Fixed

Expand Down
15 changes: 10 additions & 5 deletions src/common/records.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,21 @@ end
function records.getRecords()
local antRecords = utils.deepCopy(Records)
assert(antRecords, "Failed to copy Records")
local recordEntries = utils.entries(antRecords)
-- sort the records alphabetically, ensuring "@" record is first
local recordEntries = {}

for undername, record in pairs(antRecords) do
local entry = record
entry.name = undername
table.insert(recordEntries, entry)
end
table.sort(recordEntries, function(a, b)
if a[1] == "@" then
if a.name == "@" then
return true
end
if b[1] == "@" then
if b.name == "@" then
return false
end
return a[1] < b[1]
return a.name < b.name
end)

return recordEntries
Expand Down
19 changes: 7 additions & 12 deletions test/records.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ describe('aos Records', async () => {

const records = await getRecords(setRecordRes3.Memory);
assert(records);
const recordsMap = Object.fromEntries(records);
assert(recordsMap['@']);
// assert record order
const undernames = Object.keys(recordsMap);
assert(undernames[0] == '@');
assert.strictEqual(undernames.at(-1), 'test-3');

assert.strictEqual(records[0].name, '@');
assert.strictEqual(records.at(-1).name, 'test-3');
});

it('should get a singular record of the ant', async () => {
Expand Down Expand Up @@ -102,9 +99,8 @@ describe('aos Records', async () => {
setRecordResult.Memory,
);

const record = Object.fromEntries(
JSON.parse(recordsResult.Messages[0].Data),
)['@'];
const records = JSON.parse(recordsResult.Messages[0].Data);
const record = records[0];
assert(record.transactionId === ''.padEnd(43, '3'));
assert(record.ttlSeconds === 3600);
});
Expand Down Expand Up @@ -157,9 +153,8 @@ describe('aos Records', async () => {
setRecordResult.Memory,
);

const record = Object.fromEntries(
JSON.parse(recordsResult.Messages[0].Data),
)['timmy'];
const records = JSON.parse(recordsResult.Messages[0].Data);
const record = records.find((r) => r.name == 'timmy');
assert(record.transactionId === ''.padEnd(43, '3'));
assert(record.ttlSeconds === 3600);
});
Expand Down

0 comments on commit e1f8b75

Please sign in to comment.