nu_scripts icon indicating copy to clipboard operation
nu_scripts copied to clipboard

key-value module for std-rfc

Open NotTheDr01ds opened this issue 1 year ago • 5 comments

Simple getters and setters for key-value pairs in a pipeline.

From the help:

kv module

use std-rfc/kv *

Easily store and retrieve key-value pairs in a pipeline.

A common request is to be able to assign a pipeline result to a variable. While it's not currently possible to use a "let" statement within a pipeline, this module provides an alternative. Think of each key as a variable that can be set and retrieved.

kv set

Stores the pipeline value for later use

Usage:

<input> | kv set <key> <value?>

Examples:

ls ~ | kv set "home snapshot"
kv set foo 5
kv get

Retrieves a stored value by key

Counterpart of kv set.

Returns null if the key is not found.

Usage:

kv get <key> | <pipeline>
kv list

List the currently stored key-value pairs

Returns results as the Nushell value rather than the stored nuon.

kv drop

Returns and removes a key-value pair

NotTheDr01ds avatar Oct 02 '24 12:10 NotTheDr01ds

Before anyone asks, I wrote this as a set of space-separated-commands, rather than a <module> subcommand because get is a built in. Attempting to define:

export def get ...

... was clobbering/shadowing the internal get. It's possible workaround this in one of two ways:

  1. Avoid get, which is possible, since it is mostly syntactic sugar for | $in.<cell-path>
  2. Make the name kv get to avoid shadowing.

Option 1 resulted in some "less readable" code and was also more "dangerous", since if someone imported the module with use std-rfc/kv * instead of use std-rfc/kv, it would shadow the built-in and clobber most any other module in memory.

So I went with Option 2 ;-)

NotTheDr01ds avatar Oct 02 '24 12:10 NotTheDr01ds

I have no real problem with this but I wonder what our process should be for adding something to the stdlib. Seems like it should be more formal with voting or something. Not sure.

fdncred avatar Oct 02 '24 15:10 fdncred

Agreed - I was thinking the same thing while i was working on these. It's something we have to figure out if we hope to open up std once again.

Possibilities:

  • Telemetry ... Kidding. Mostly. Could be opt-in with an environment variable where it just pings (http get) some URL to update a counter. Longer-term, pie-in-the-sky.

  • Probably something more realistic like an "issue" thread here for commenting and upvotes. One thread per command (or potentially module). Reaches some level of upvotes (currently pretty low) and it gets moved forward?

  • Special-case core-team votes. I seem to recall you saying that right now it takes two core team to move a PR forward (or something like that; not sure if there's anything official).

NotTheDr01ds avatar Oct 02 '24 18:10 NotTheDr01ds

I'll add tests for this one as well before bringing out of draft.

Any thoughts on whether I should use msgpackz in place of nuon for the value pickling?

NotTheDr01ds avatar Oct 03 '24 15:10 NotTheDr01ds

You'd have to do some tests to see if it makes any difference because with msgpackz you have to pay for creating msgpack and brotli for compression/decompression.

fdncred avatar Oct 03 '24 15:10 fdncred