Skip to content

Standardizing conversions to string #14090

Open

Description

@IanManske

Problem

There are many places in Nushell where values are turned into strings for one reason or another. Over time, this has led to many different implementations and behaviors. Most recently, this issue was brought up in #14086. Going foward, we should standardize the process(es) for turning values into strings and document it somewhere.

My current hypothesis (I need to go through the codebase and confirm) is that there are two or three main use cases for turning a value into a string:

  1. The value needs to be displayed in a human-readable format (e.g., in errors, for table, or for format). This should probably respect the current locale and formats specified in the config.
  2. The value needs to be converted to string data for some other programmatic purpose (e.g., into string). This should probably have the same output regardless of config settings or environment variables.
  3. The value needs to be sent to an external source. E.g., as an external command argument, converted to an OS environment variable, or as input to an external command. This is similar to 2) except that I think certain conversions should be disallowed as they are most likely an error, and having different behavior for others types makes more sense for this use case. To differentiate it from into string, to text can take up this role.

I think we can create a separate issue for 1) if necessary. I would rather start with 2) and 3), as I think 2) currently has the least uniform behavior and 3) is related to 2).

Rough Proposal

For 2), my current proposal is that we, for the most part, use to nuon. The difference being that all values are first converted to strings and then outputted as such (e.g., binary values are not formatted as binary literals). I.e., there is no guarantee that type information is preserved. In particular, types should be converted as follows:

Type Conversion
bool Use Rust's built in Display implementation
int Display
float Display
filesize Automatically detect unit precision. Leaning towards non-metric units so that decimal places can be used without losing precision. E.g., (1MB + 1B) | into string == '1.000001MB'
duration Use ISO 8601 durations but also add a negative sign at the start if necessary.
date Use .to_rfc3339() from chrono. This follows RFC 3339 but also allows out of range years as specified by ISO 8601 (otherwise some valid date values would not be representable as strings). To be precise, automatic subsecond precision is used and +00:00 is used instead of Z.
string Simply used as is.
glob Used as is.
binary Used as is if valid utf-8.
cellpath Prefixed with $. and with necessary quoting/escaping for each path member.
closure Error/disallowed
range [start]..[end] or [start]..<next>..[end]
nothing null
list [ val1, val2 ], adding quotes either where necessary or always (for strings, globs, and binary).
record { foo: bar, baz: val }, adding quotes where necessary for columns/keys.

Then, I propose 3) should be based off of 2) but with the following differences:

  • Non-utf-8 binary values are allowed.
  • Cellpaths are disallowed? These are nushell-specific data types, so probably does not make sense to convert it to a string for the purpose of sending to an external source.
  • Similarly, ranges should probably be disallowed? Our range format is most likely not going to be interpreted properly by an external source.
  • Records are disallowed too. Same as the above two, especially since records have escaping concerns and allow arbitrary nesting. Instead, users should use to json or some other to command.
  • Lists that contain none of the disallowed types above and also do not contain other lists are allowed. Each value is converted to a string based on 2) and a new line is added after each element. For conversion to OS environment variables, the OS specific path separator is used instead (I believe ; for windows and : for unix?).
  • Filesizes and durations I think can be converted the same as in 2)...
  • Nothing/null is a whole separate issue that might be worth discussing.

If you made it this far, thanks for reading lol. Would love to hear everybody's thoughts.

Activity

  1. added
    status:needs-design
    semantics
    on Oct 15, 2024
  2. IanManske commented on Oct 15, 2024

    @IanManske
    MemberAuthor

    Another related but probably separate issue: the to {format} commands should probably error on data types not supported by the data format (e.g., JSON has no concept of dates). Instead, there should be a --lossy flag that falls back to using 2) to convert the unsupported types to string. The --lossy flag means that round-trip conversions are not value preserving. E.g., ($some_date | to json --lossy | from json) != $some_date.

  3. fdncred commented on Oct 15, 2024

    @fdncred
    Contributor

    I think we need to be sure that we're not losing functionality. It sounds like 3) could be losing some but I can't really tell without testing things.

    I definitely want a better "to string" api where we don't have to decide which of ~5 functions to use.

    I figure something like to_abbreviated is still needed since we represent nested tables, records, and lists as strings but maybe that can just be inner workings and not be public?

    I also wonder if closures should work in a way similar to how view source can display them.

    Thanks for writing this up. I agree that something is definitely needed. I look forward to seeing how this progresses.

  4. 132ikl commented on Oct 15, 2024

    @132ikl
    Member

    For 2), my current proposal is that we, for the most part, use to nuon. The difference being that all values are first converted to strings and then outputted as such (e.g., binary values are not formatted as binary literals).

    I'm a bit confused on the distinction between to nuon and what you're proposing here, could you explain a bit more? The rules from your table seem to be similar to what to nuon does.

  5. IanManske commented on Oct 16, 2024

    @IanManske
    MemberAuthor

    I'm a bit confused on the distinction between to nuon and what you're proposing here, could you explain a bit more?

    No problem, I should have given an example.

    > "hello" | into binary | to nuon
    0x[68656C6C6F]
    
    > "hello" | into binary | into string
    hello
    
    > { foo: ('bar' | into binary) } | to nuon
    {foo: 0x[626172]}
    
    > { foo: ('bar' | into binary) } | into string
    {foo: bar}

    So, I guess besides binary values, my proposal is the same as to nuon. However, we now have glob types without a corresponding syntax. If we add a syntax for glob literals and add it to the nuon format, then globs would also be different under this proposal.

  6. added a commit that references this issue on Nov 2, 2024
  7. added
    category:inconsistent-behavior
    on May 15, 2025
  8. 132ikl commented on May 15, 2025

    @132ikl
    Member

    We talked a little about this at the meeting yesterday, and I've been thinking about it a bit since. I like Ian's rough proposal, and I think the three noted use cases are accurate (although I think we should be a bit stricter on 3 than Ian suggests, but that's a topic for #14452). Also, there is a TLDR at the bottom of this comment

    String conversion methods

    I'm not as concerned with the specifics of each value representation right now so much as mapping Nushell commands/behaviors to the use-cases described. I think as a first step we can replace our current string conversion functions (to_expanded_string, to_abbreviated_string, coerce_into_string) with:

    • A human-readable string conversion function (which respects config, locale)
      • For clarity, I'll refer to this as to_human_string (but can bikeshed later)
    • A programmatic string conversion function (which is the same regardless of config, locale)
      • Possibly with a "coerce" parameter to convert values which don't have a clear programmatic string representation. specifically, closures and custom values)
      • This should only work for scalar/primitive values, there should be no programmatic version of composite values. I have a feeling that we only ever need human-readable or format-specific versions of composite values, and that no "general" programmatic representation is actually needed for composite values
      • For clarity, let's refer to this as to_programmatic_string

    How we would actually implement these methods

    Here's a list of the primary areas in Nushell where we use string conversions, and how I think they should behave using the above approach:

    • to text
      • This is the command you should choose if you need a human-readable string which won't necessarily be displayed in the terminal.
      • I think this should should simply call the to_human_string function. This would mean to text maintains its function of being a simple way to convert any Nushell value to a human-readable string.
      • We might want the human-readable version of lists and records to better represent the value by adding in brackets or curly braces respectively to the human-readable representation, although I'm not exactly sure what the most "human-readable" non-table version of a list or record is. If we do want to add brackets/braces however, this would mean the way to text is currently commonly used as an alternative to str join (char nl) would no longer work. If we decide to do this, I think we should bite the bullet on this one and add a to lines or something, since it seems like an anti-pattern to be using to text when you actually just want to join lines. If we really don't want to change this, we could add another command to print the human-readable version of a string.
    • render/table
      • This is the command you should use if you need a human-readable string which will be displayed in the terminal. We should stress that this is not a general purpose command for creating human-readable strings, and to text should instead be used for that.
      • For scalar values, we should just use to_human_string (with the exception of binary values since we have the fancy binary value display)
      • For composite values, we use the tabled tables, and inner values use to_human_string. This way, scalar values are displayed the same within a table and outside of a table, which is not the current behavior.
    • into string
      • This simply calls to_programmatic_string for scalars.
      • It maps over lists and tables, and errors if you try to convert a record (same as current behavior)
    • to and format subcommands
      • These commands can use to_programmatic_string as needed, but should not use to_human_string
      • These commands might apply their own formatting choices and rules on top of, or in place of, to_programmatic_string. For example, to nuon would probably use to_programmatic_string for most values, but would need special handling for binary values (as noted by Ian)
      • These commands should error if a value cannot be represented in the format. For example, if a format cannot represent tables-within-tables, then a table within a table should error rather than be converted to a human readable string, and you can use to text to convert inner tables to human readable strings if needed.
      • Currently, the format date command when given no arguments will output a human readable string. This is the only format command which works like this, and I think we should remove this functionality. Then, all format commands will be programmatic conversions only. to text can instead used to produce a (config respecting) human readable string from a date.
    • Implicit conversions
      • We have a couple places where we do implicit conversions, such as str join (probably others, but I don't initially see anything?). Whether we want to change this is a different topic, so for the sake of not derailing us, I will assume the current behavior. I think we should use to_programmatic_string for these cases, as they are programmatic operations. You can always use to text if you want a human-readable string.

    Miscellanea:

    • I think we can consider external command conversions (case 3 in Ian's description, related to non-strings are not piped to external commands #14452) to be a kind of "format", and apply extra strictness on top of to_programmatic_string as needed.
    • We can keep the abbreviated strings used by render/table and explore, but move that to a specific helper function for use by those commands specifically. This should make it clear that it's not a general-purpose conversion.
    • For the REPL output when not using table (so display_output hook set to {||}), I think we should use to_human_string

    TLDR

    I think string conversions should fit into two main categories, with a corresponding conversion method on Value:

    • Human readable strings, which respect the config and locale settings. These are only used by to text, render/table, and explore
    • Programmatic strings, which are used in other cases where string representations of values are needed. These do not depend on config or locale. These are only for primitive values, and not for composite values (lists, records, tables).

    Commands which convert to a specific format, like the to and format subcommands can use programmatic strings, but apply further strictness or additional formatting as needed by that format. They should not use human readable strings.

    To implement this, we would need to make following changes:

    • to text would become the primary way to do general-purpose conversions to human readable strings. Therefore, we might want lists and records to look a little more like their literals to make it clear that the value is a list or record (as opposed to the current behavior of joining list items with newlines and printing key-value pairs for records)
      • We could add a command which better represents the intent of the str join (char nl) use-case of to text, such as to lines
    • The no-argument version of format date would be removed. The format specifier argument would be made mandatory. You should use to text or into string instead.
    • render/table would render values inside of a list/table and outside of a list/table in the same way (with the exception of binary values, since they have a special display outside of a list/table)
    • Formats which cannot fully represent a value should error rather than using a string version (for example, lists-within-lists and tables-within-tables would error when using to html and to md instead being converted to a [list 1 item] string)
    • Implicit conversions (such as str join) would use programmatic strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:inconsistent-behaviorsemanticsstatus:needs-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions