Skip to content

"can't convert list<string> to string" but I don't want to #7033

Closed

Description

@theAkito

Related problem

# Use `rhash` to get SHA-512 hash.
export def "hash sha512" [
  --path (-p): bool
  --size (-s): bool
  --all (-a): bool
] {
  let input = $in
  let span = (metadata $input).span
  if ($input | any ($it | path exists) == false) {
    error make {
      msg: "At least one of the files provided does not exist."
      label: {
        text: "Input contains file, that does not exist."
        start: $span.start
        end: $span.end
      }
    }
  }
  let options = if $all {
    [[fmt columns]; ['%p,%f,%s B,%{Sha-512}' ["path" "name" "size" "hash"]]]
    } else if $path {
    [[fmt columns]; ['%p,%{Sha-512}' ["path" "hash"]]]
    } else if $size {
    [[fmt columns]; ['%f,%s B,%{Sha-512}' ["name" "size" "hash"]]]
    } else {
    [[fmt columns]; ['%f,%{Sha-512}' ["name" "hash"]]]
  }
  rhash $"--printf=($options | get fmt.0)" $input | split column ',' ($options | get columns.0 )
}

I don't want to convert ["name" "hash"] to a single string, but Nu seems to try to do it and fails at it.

Describe the solution you'd like

I want it to be two separate strings, so split column can accept them as its rest parameter.

Describe alternatives you've considered

Played around with all kinds of column, split and str commands, but none of them worked in this case.

Additional context and details

In Bash, that would be the following.

test-arg-count.sh

function show_args {
  ## Reliable way of showing the
  ## number of arguments given.
  ## Useful for debugging Bash behaviour.
  printf "%d args:" "$#"
  printf " <%s>" "$@"
  echo
}


show_args "$@"

Run

bash test-sep-cmd.sh my nu style is nyce
5 args: <my> <nu> <style> <is> <nyce>

We provide 5 separate strings, so this is what we get. However, Nu cannot seem to make that happen with the list provided in the example above.

Activity

  1. added
    category:enhancement
    on Nov 6, 2022
  2. rgwood commented on Mar 1, 2023

    @rgwood
    Contributor

    Please provide a minimal reproduction.

  3. theAkito commented on Mar 3, 2023

    @theAkito
    Author

    Works

    'test.nu,test.nu,67 B,55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703' | split column ',' "path" "name" "size" "hash"

    Does not work

    let o = ["path" "name" "size" "hash"]
    'test.nu,test.nu,67 B,55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703' | split column ',' $o
    Error: nu::parser::type_mismatch (link)
    
      × Type mismatch.
     1 │ 'test.nu,test.nu,67 B,55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703' | split column ',' $o
       ·                                                                                                                                                                            ─┬
       ·                                                                                                                                                                             ╰── expected String, found List(String)
       ╰────
    
    let o = [[fmt columns]; ['%p,%f,%s B,%{Sha-512}' ["path" "name" "size" "hash"]]]
    'test.nu,test.nu,67 B,55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703' | split column ',' ($o | get columns.0)
    Error: nu::shell::cant_convert (link)
    
      × Can't convert to string.
       ╭─[entry #31:1:1]
     1 │ let o = [[fmt columns]; ['%p,%f,%s B,%{Sha-512}' ["path" "name" "size" "hash"]]]
       ·                                                  ──────────────┬──────────────
       ·                                                                ╰── can't convert list<string> to string
       ╰────
    
  4. Zoybean commented on Feb 12, 2024

    @Zoybean
    Contributor

    You should be able to use the spread operator (...), replacing $o with ...$o, like so:

    > let o = ["path" "name" "size" "hash"]
    > 'test.nu,test.nu,67 B,55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703' | split column ',' ...$o
    ────#────┬─────path──────┬─────name─────┬───size────┬─────────────────────────────────────────────────────────────────hash──────────────────────────────────────────────────────────────────
     0       │ test.nu       │ test.nu      │ 67 B      │ 55E7A8C5CCFC2016D70A4E6696573806053C41ACF497E31CD70B478D19F5F999EF150B6AA352969C167E0DB6D45E4FB431E2C52B29EBA8E82993111B26A31703
    ────#────┴─────path──────┴─────name─────┴───size────┴─────────────────────────────────────────────────────────────────hash──────────────────────────────────────────────────────────────────
    

    The spread operator unpacks a list into a sequence of parameters to a function, and appears to do what you wanted.

  5. kubouch commented on Apr 20, 2024

    @kubouch
    Contributor
    let o = ["path" "name" "size" "hash"]
    'test.nu,test.nu,67 B,55E7' | split column ',' ...$o

    seems to work now.

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:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions