首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于显示结构值的Chaincode函数

用于显示结构值的Chaincode函数
EN

Stack Overflow用户
提问于 2017-01-23 18:00:40
回答 1查看 1K关注 0票数 0

我正在尝试编写一个简单的链代码,它使用一个结构来存储客户详细信息。我有一个运行良好的setDetails函数。我希望编写另一个getDetails函数,它将UID作为参数,并打印具有该UID的客户的详细信息。需要帮助!

代码语言:javascript
复制
package main

import (
    "errors"
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
)

type Customer struct {
    UID     string
    Name    string
    Address struct {
        StreetNo string
        Country  string
    }
}

type SimpleChaincode struct {
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
    fmt.Printf("initialization done!!!")
    fmt.Printf("initialization done!!!")

    return nil, nil
}

func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    if len(args) < 3 {
        return nil, errors.New("insert Into Table failed. Must include 3 column values")
    }

    customer := &Customer{}
    customer.UID = args[0]
    customer.Name = args[1]
    customer.Address.Country = args[2]

    return nil, nil
}

func (t *SimpleChaincode) getDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    //wish to print all details of an particular customer corresponding to the UID
    return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
    function, args := stub.GetFunctionAndParameters()
    fmt.Printf("Inside Invoke %s", function)
    if function == "setDetails" {
        return t.setDetails(stub, args)

    } else if function == "getDetails" {
        return t.getDetails(stub, args)
    }

    return nil, errors.New("Invalid invoke function name. Expecting  \"query\"")
}

func main() {
    err := shim.Start(new(SimpleChaincode))
    if err != nil {
        fmt.Printf("Error starting Simple chaincode: %s", err)
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-23 19:15:26

直到现在我才知道Hyperledger,但是在看了github文档之后,我知道您会使用stub.PutState来存储客户信息,然后使用stub.GetState来获取它。

由于这两种方法都请求一个字节片,我的猜测是这样的:

代码语言:javascript
复制
func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    if len(args) < 3 {
        return nil, errors.New("insert Into Table failed. Must include 3 column values")
    }

    customer := &Customer{}
    customer.UID = args[0]
    customer.Name = args[1]
    customer.Address.Country = args[2]

    raw, err := json.Marshal(customer)
    if err != nil {
        return nil, err
    }

    err := stub.PuState(customer.UID, raw)
    if err != nil {
        return nil, err
    }

    return nil, nil
}

func (t *SimpleChaincode) getDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {

    if len(args) != 1 {
        return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
    }

    return stub.GetState(args[0])
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41803562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档