Skip to content

[encoding.binary] error decoding map[K]Struct #24190

@kravlad

Description

@kravlad

Describe the bug

Now V panics if I try to call binary.decode_binary with a map that has a struct as a value. (For example map[string]Struct)

Reproduction Steps

module main
import encoding.binary

struct St {
	i int
}

fn main() {
	b := binary.encode_binary({"s": St{1}})!
	b2 := binary.decode_binary[map[string]St](b)!
	println('Decoded binary file: ${b2}')
}

Expected Behavior

"Decoded binary file: {'s': St{
    i: 1
}}"

Current Behavior

/Users/user/dev/v/vlib/encoding/binary/serialize.v:402:8: error: cannot assign to `v`: expected `binary.Any`, not `St`
  400 |             v = decode_string(mut s)!
  401 |         } $else $if V is $struct {
  402 |             v = decode_struct(mut s, V{})!
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~
  403 |         } $else $if V is $map {
  404 |             v = decode_map(mut s, V{})!
/Users/user/dev/v/vlib/encoding/binary/serialize.v:408:17: error: cannot cast `encoding.binary.Any` to `St`
  406 |             v = decode_primitive(mut s, unsafe { V(0) })!
  407 |         }
  408 |         m[k as K] = v as V
      |                       ~~
  409 |     }
  410 |     return m

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 d54a823

Environment details (OS name and version, etc.)

|V full version      |V 0.4.10 b114254.d54a823
|:-------------------|:-------------------
|OS                  |macos, macOS, 15.3.2, 24D81
|Processor           |8 cpus, 64bit, little endian, Apple M1
|Memory              |0.18GB/16GB
|                    |
|V executable        |/Users/user/dev/v/v
|V last modified time|2025-04-11 03:43:02
|                    |
|V home dir          |OK, value: /Users/user/dev/v
|VMODULES            |OK, value: /Users/user/.vmodules
|VTMP                |OK, value: /tmp/v_501
|Current working dir |OK, value: /Users/user/projects/proj
|                    |
|Git version         |git version 2.49.0
|V git status        |weekly.2024.41-1243-gd54a8232 (2 commit(s) behind V master)
|.git/config present |true
|                    |
|cc version          |Apple clang version 16.0.0 (clang-1600.0.26.6)
|gcc version         |Apple clang version 16.0.0 (clang-1600.0.26.6)
|clang version       |Apple clang version 16.0.0 (clang-1600.0.26.6)
|tcc version         |tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
|tcc git status      |thirdparty-macos-arm64 713692d4
|emcc version        |N/A
|glibc version       |N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions