From 7d4fa7306866fccda1108c51b9269beea34dc60c Mon Sep 17 00:00:00 2001 From: Alan Gutierrez Date: Sun, 14 Nov 2021 04:21:45 -0600 Subject: [PATCH] Implement sorted flag. Necessary for Addendum since it is maintaining an unordered list for each tree node, but `etcd` seems to always produce a sorted listing. See #29. --- addendum.js | 9 +++++++++ package.json | 2 ++ test/keys.t.js | 23 ++++++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/addendum.js b/addendum.js index f7dd89b..32809da 100644 --- a/addendum.js +++ b/addendum.js @@ -14,6 +14,12 @@ const Reactor = require('reactor') const Conference = require('conference') +// Comparator function builder. +const ascension = require('ascension') + +// A comparator decorator that extracts values for comparison. +const whittle = require('whittle') + // An event scheduler to manage multiple timed events inside a calendar using a // single `setTimeout`. const { Calendar, Timer } = require('happenstance') @@ -32,6 +38,8 @@ const Log = require('./log') const AddendumError = require('./error') +const sort = whittle(ascension(String), node => node.key) + // Compassion applications are implemented as a class that implements a specific // interface of `async`/`await` functions. Addendum is an implementation of the // `etcd` v2 API using Compassion. With it you'll be able to see how you might @@ -792,6 +800,7 @@ class Addendum { } ] } } + const sorted = request.query.sorted == 'true' ? listing.sort(sort) : listing return { action: 'get', node: { diff --git a/package.json b/package.json index 1c12edf..99d8553 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "dependencies": { + "ascension": "3.0.0-alpha.12", "compassion": "2.0.0-alpha.21", "conference": "0.27.0-alpha.3", "cubbyhole": "4.0.8", @@ -27,6 +28,7 @@ "fastify-formbody": "5.1.0", "reactor": "17.0.0-alpha.17", "rescue": "7.0.0-alpha.18", + "whittle": "0.0.5", "wildmap": "2.0.0-alpha.1" }, "devDependencies": diff --git a/test/keys.t.js b/test/keys.t.js index 308e776..6eda0fa 100644 --- a/test/keys.t.js +++ b/test/keys.t.js @@ -2,7 +2,7 @@ // **TODO** Does the root key exist on a fresh boot of `etcd` and if so what is // its index and value? -const count = 32 +const count = 33 const harness = require('./harness') @@ -135,6 +135,27 @@ async function test (okay, { DELETE, GET, PUT, prune }) { }, 'list directory with vivified directory') } + { + const response = await GET('/v2/keys/addendum?sorted=true') + okay(prune(response), { + status: 200, + data: { + action: 'get', + node: { + dir: true, + key: '/addendum', + nodes: [{ + key: '/addendum/x', + value: 'y' + }, { + key: '/addendum/y', + dir: true + }] + } + } + }, 'list directory with vivified directory sorted') + } + { const response = await GET('/v2/keys/addendum?recursive=true') okay(prune(response), {