Skip to content

Commit

Permalink
Implement sorted flag.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flatheadmill committed Nov 14, 2021
1 parent 33d80df commit 7d4fa73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions addendum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -792,6 +800,7 @@ class Addendum {
} ]
}
}
const sorted = request.query.sorted == 'true' ? listing.sort(sort) : listing
return {
action: 'get',
node: {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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":
Expand Down
23 changes: 22 additions & 1 deletion test/keys.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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), {
Expand Down

0 comments on commit 7d4fa73

Please sign in to comment.