jaypipes / ghw

Go HardWare discovery/inspection library
Apache License 2.0
1.64k stars 180 forks source link

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

Closed Techno-Fox closed 3 years ago

Techno-Fox commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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) {