dasel icon indicating copy to clipboard operation
dasel copied to clipboard

`dasel put -t json -s 'array.[]' -v "{…}"` to a TOML file crashes dasel

Open jakubadamw opened this issue 2 years ago • 3 comments

Describe the bug dasel put fails to write more than one complex element (of type JSON, which is apparently how one denotes an object regardless of the target format) to an array in a TOML file.

To Reproduce Steps to reproduce the behavior:

$ touch test.toml
$ dasel put -f test.toml -t json -s "array.[]" -v "{ \"key\": \"value\" }"
$ cat test.toml
array = [{ key = "value" }]
$ dasel put -f test.toml -t json -s "array.[]" -v "{ \"key\": \"value\" }"
panic: reflect.Set: value of type []interface {} is not assignable to type []map[string]interface {}

goroutine 1 [running]:
reflect.Value.assignTo({0x104b89c80?, 0x1400012d5d8?, 0x104ba5ea0?}, {0x104a0a05d, 0xb}, 0x104b89ec0, 0x0)
        reflect/value.go:3145 +0x214
reflect.Value.Set({0x104b89ec0?, 0x1400012d170?, 0x94?}, {0x104b89c80?, 0x1400012d5d8?, 0x140003562d0?})
        reflect/value.go:2160 +0xc8
github.com/tomwright/dasel.Value.Append({{0x104b9eba0, 0x140002e9e50, 0x94}, 0x14000328540, 0x140003285b0, 0x140003562d0})
        github.com/tomwright/dasel/value.go:239 +0x17c
github.com/tomwright/dasel.glob..func3(0xf000000000000006?, 0x16?, {0x104faf9c8?, 0x14000356390?, 0x140002dae20?})
        github.com/tomwright/dasel/func_append.go:26 +0x25c
github.com/tomwright/dasel.BasicFunction.Run(...)
        github.com/tomwright/dasel/func.go:140
github.com/tomwright/dasel.(*Step).execute(0x1400014e960)
        github.com/tomwright/dasel/step.go:30 +0x5c
github.com/tomwright/dasel.(*Context).Next(0x14000275300)
        github.com/tomwright/dasel/context.go:225 +0x1cc
github.com/tomwright/dasel.(*Context).Run(0x104be5480?)
        github.com/tomwright/dasel/context.go:193 +0x34
github.com/tomwright/dasel.Put({0x104be5480, 0x14000356180}, {0x16b8cb9aa, 0x8}, {0x104be5480?, 0x14000356150?})
        github.com/tomwright/dasel/context.go:128 +0xa0
github.com/tomwright/dasel/internal/command.runPutCommand(0x14000331cc8, 0x1049e0ca7?)
        github.com/tomwright/dasel/internal/command/put.go:132 +0x4ac
github.com/tomwright/dasel/internal/command.putRunE(0x14000338300?, {0x14000275200, 0x0, 0x8?})
        github.com/tomwright/dasel/internal/command/put.go:83 +0x304
github.com/spf13/cobra.(*Command).execute(0x14000338300, {0x14000275180, 0x8, 0x8})
        github.com/spf13/[email protected]/command.go:916 +0x5c8
github.com/spf13/cobra.(*Command).ExecuteC(0x14000338000)
        github.com/spf13/[email protected]/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/[email protected]/command.go:968
main.main()
        github.com/tomwright/dasel/cmd/dasel/main.go:10 +0x24
$ dasel put -f test.toml -t json -s "array.[]" -v 123
panic: reflect.Set: value of type []interface {} is not assignable to type []map[string]interface {}

goroutine 1 [running]:
reflect.Value.assignTo({0x1055fdc80?, 0x1400012d578?, 0x105619ea0?}, {0x10547e05d, 0xb}, 0x1055fdec0, 0x0)
        reflect/value.go:3145 +0x214
reflect.Value.Set({0x1055fdec0?, 0x1400012d110?, 0x94?}, {0x1055fdc80?, 0x1400012d578?, 0x140002d62a0?})
        reflect/value.go:2160 +0xc8
github.com/tomwright/dasel.Value.Append({{0x105612ba0, 0x14000269e20, 0x94}, 0x140002aa540, 0x140002aa5b0, 0x140002d62a0})
        github.com/tomwright/dasel/value.go:239 +0x17c
github.com/tomwright/dasel.glob..func3(0xef00000000000006?, 0x16?, {0x105a239c8?, 0x140002d6360?, 0x1400025ade0?})
        github.com/tomwright/dasel/func_append.go:26 +0x25c
github.com/tomwright/dasel.BasicFunction.Run(...)
        github.com/tomwright/dasel/func.go:140
github.com/tomwright/dasel.(*Step).execute(0x1400010e9b0)
        github.com/tomwright/dasel/step.go:30 +0x5c
github.com/tomwright/dasel.(*Context).Next(0x140001f3300)
        github.com/tomwright/dasel/context.go:225 +0x1cc
github.com/tomwright/dasel.(*Context).Run(0x105659480?)
        github.com/tomwright/dasel/context.go:193 +0x34
github.com/tomwright/dasel.Put({0x105659480, 0x140002d6150}, {0x16ae579ba, 0x8}, {0x105659480?, 0x140002d6120?})
        github.com/tomwright/dasel/context.go:128 +0xa0
github.com/tomwright/dasel/internal/command.runPutCommand(0x140002b3cc8, 0x105454ca7?)
        github.com/tomwright/dasel/internal/command/put.go:132 +0x4ac
github.com/tomwright/dasel/internal/command.putRunE(0x140002ba300?, {0x140001f3200, 0x0, 0x8?})
        github.com/tomwright/dasel/internal/command/put.go:83 +0x304
github.com/spf13/cobra.(*Command).execute(0x140002ba300, {0x140001f3180, 0x8, 0x8})
        github.com/spf13/[email protected]/command.go:916 +0x5c8
github.com/spf13/cobra.(*Command).ExecuteC(0x140002ba000)
        github.com/spf13/[email protected]/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/[email protected]/command.go:968
main.main()
        github.com/tomwright/dasel/cmd/dasel/main.go:10 +0x24
$ dasel put -f test.toml -t json -s "array2.[]" -v 123
$ dasel put -f test.toml -t json -s "array2.[]" -v "{ \"key\": \"value\" }"
$ dasel put -f test.toml -t json -s "array2.[]" -v 1234
$ cat test.toml
array2 = [123.0, { key = "value" }, 1234.0]

[[array]]
  key = "value"

Expected behavior The above commands should succeed.

Desktop (please complete the following information):

$ dasel -v
dasel version 2.0.2

jakubadamw avatar Dec 30 '22 13:12 jakubadamw

Thanks for raising this issue. I am able to replicate it and will do some digging.

As an FYI, you are able to create a complex object using any of the parsers. Here's an example with TOML input:

dasel put -f test.toml -t toml -v 'key = "value"' 'array.[]'

The related docs are here: https://daseldocs.tomwright.me/commands/put Specifically for the -t,--type flag:

The type of value we are writing. Can be string, int or bool. If it is not contained in that list dasel attempts to find a read parser of --type and if found will read the input value as a document.

TomWright avatar Dec 30 '22 14:12 TomWright

@TomWright thank you for looking into this!

FWIW, I had tried using -t toml for the input value but couldn't get it to work with multiple pairs at once. A comma in between (key = "value", key2 = "value2") wouldn't parse.

jakubadamw avatar Dec 30 '22 19:12 jakubadamw

I am currently reworking the encode/decode logic for all formats as part of https://github.com/TomWright/dasel/pull/289.

I can reevaluate and hopefully fix this issue as part of that work.

TomWright avatar Jan 24 '23 19:01 TomWright