Skip to content

Unable to decode struct with Sumtype. "unknown sum type value" #15081

@ismyhc

Description

@ismyhc

V version: V 0.3.0 89a757a, timestamp: 2022-07-14 11:10:15 +0300
OS: macOS Monterey 12.4, Apple M1 Max

What did you do?

import json

pub type GetBlockResult = string | GetBlockResultB

pub struct GetBlockResponse {
	result GetBlockResult
}

pub struct GetBlockResultB {
	hash               string
	confirmations      u64
}

fn main() {
	a := '{ "result": "hello" }'
	res_a := run<GetBlockResponse>(a) or { panic(err) return }
	println(res_a)

	b := '{ "result": { "hash": "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", "confirmations": 743970 } }'
	res_b := run<GetBlockResponse>(b) or { panic(err) return }
	println(res_b)
}

fn run<T>(s string) ?T {
	res := json.decode(T, s) or { return err }
	return res
}

What did you expect to see?

GetBlockResponse{
    result: GetBlockResult('hello')
}
GetBlockResponse{
    result: GetBlockResult{
        hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09'
        confirmations: 743970
    }
}

What did you see instead?

GetBlockResponse{
    result: GetBlockResult('hello')
}
GetBlockResponse{
    result: unknown sum type value
}

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Modules: JSONBugs/feature requests, that are related to `json` and `x.json2` modules.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions