Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在用Go编写的Hyperledger结构链码中使用UUID(作为字节数组)?_Go_Hyperledger Fabric_Uuid_Hyperledger Chaincode - Fatal编程技术网

如何在用Go编写的Hyperledger结构链码中使用UUID(作为字节数组)?

如何在用Go编写的Hyperledger结构链码中使用UUID(作为字节数组)?,go,hyperledger-fabric,uuid,hyperledger-chaincode,Go,Hyperledger Fabric,Uuid,Hyperledger Chaincode,我正在用Go for Hyperledger结构编写链码。我有下一个密码 package chaincode import ( "encoding/json" "fmt" "log" "time" "github.com/google/uuid" "github.com/hyperledger/fabric-chaincode-go/

我正在用Go for Hyperledger结构编写链码。我有下一个密码

package chaincode

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

    "github.com/google/uuid"

    "github.com/hyperledger/fabric-chaincode-go/shim"
    "github.com/hyperledger/fabric-contract-api-go/contractapi"
)

type SmartContract struct {
    contractapi.Contract
}

type MyStruct struct {
    ID         string 
    Description string `json:"description"`
    UUID   uuid.UUID `json:"uuid"`
}

func (s *SmartContract) Create(ctx contractapi.TransactionContextInterface, ID string, description string) error {
    asset := MyStruct{
        ID:         ID,
        Description: description,
        UUID:     uuid.Nil,
    }
    js, err := json.Marshal(asset)
    if err != nil {
        return err
    }

    return ctx.GetStub().PutState(asset.ID,js)
}

func (s *SmartContract) Get(ctx contractapi.TransactionContextInterface, ID string) (*MyStruct, error) {
    assetAsBytes, err := ctx.GetStub().GetState(ID)
    if err != nil {
        return nil, fmt.Errorf("failed to read from world state: %w", err)
    }

    if assetAsBytes == nil {
        return nil, fmt.Errorf("%s does not exist", ID)
    }

    asset := new(MyStruct)
    err = json.Unmarshal(assetAsBytes, asset)
    if err != nil {
        return nil, fmt.Errorf("can not unmarshal: %v", err)
    }

    return asset, nil
}
之前,我有相同的代码,但没有UUID字段,它工作得很好。但现在,在添加此字段后,我得到了下一个错误
值与架构不匹配:\n1。return.uuid:无效类型。应为:数组,在调用
Get
方法时为:字符串“

但正如我所看到的,写入和读取分类账工作得很好,但在对等和链码通信中存在一些错误。我不明白如何修复这个错误,除了用字符串类型替换UUID字段


有没有办法在不替换类型的情况下修复它?我想将UUID用作UUID.UUID(字节数组),而不是字符串,因为我不想进行转换和UUID字符串验证。

在这个问题中遇到了类似的问题:


目前的建议是转换为字符串,因为契约API似乎错误地处理了字节数组。这是将来应该解决的问题,但具体时间线未知。

我检查了代码,并注意到,问题不完全是UUID,而是字节数组。