Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json: cannot unmarshal string into Go struct field Info.architecture of type topology.Architecture #283

Closed
Techno-Fox opened this issue Oct 14, 2021 · 2 comments · Fixed by #284

Comments

@Techno-Fox
Copy link

This is a new one that came up. I noticed my structure wouldn't Unmarshal.

This was the error: json: cannot unmarshal string into Go struct field Info.architecture of type topology.Architecture

Example:

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/jaypipes/ghw"
)

func main() {

	data, err := ghw.Topology(ghw.WithDisableWarnings())
	if err != nil {
		log.Fatal(err)
	}

	jdata, err := json.Marshal(data)
	if err != nil {
		log.Fatal(err)
	}

	var t *ghw.TopologyInfo

	err = json.Unmarshal(jdata, &t)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(t)
}
@ffromani
Copy link
Collaborator

Hi @Techno-Fox thanks for the report! I'll have a look ASAP!
I wonder if we have a way to test more comprehensively to prevent these issues to resurface. I'll spend some time thinking about it.

ffromani added a commit to ffromani/ghw that referenced this issue Oct 21, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
@ffromani
Copy link
Collaborator

This time I reviewed all the types implementing MarshalJSON and added tests for all of them:

pkg/block/block.go:46:	// DriveType::MarshalJSON.
pkg/block/block.go:64:func (dt DriveType) MarshalJSON() ([]byte, error) {
pkg/block/block.go:111:	// StorageController::MarshalJSON.
pkg/block/block.go:144:func (sc StorageController) MarshalJSON() ([]byte, error) {
pkg/memory/memory_cache.go:36:	// CacheType:MarshalJSON.
pkg/memory/memory_cache.go:52:func (a CacheType) MarshalJSON() ([]byte, error) {
pkg/pci/pci.go:76:func (d *Device) MarshalJSON() ([]byte, error) {
pkg/topology/topology.go:43:	// Architecture:MarshalJSON.
pkg/topology/topology.go:58:func (a Architecture) MarshalJSON() ([]byte, error) {

ffromani added a commit to ffromani/ghw that referenced this issue Oct 21, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this issue Oct 21, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this issue Oct 21, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
jaypipes added a commit that referenced this issue Oct 26, 2021
topology: memory: allow encode/decode cycle (#283)
ffromani added a commit to ffromani/ghw that referenced this issue Nov 19, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/ghw that referenced this issue Nov 23, 2021
After we fixed jaypipes#275 (allow marshal/unmarshal cycle for block.Info),
we discovered few more occourrences which also prevented
full JSON encode/decode/encode cycles (aka roundtrips).

All of the new findings still see unnecessary limitations,
so this patch adds the missing functions.

Fixes: jaypipes#283
Signed-off-by: Francesco Romani <fromani@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants