链码函数以显示结构值

huangapple go评论84阅读模式
英文:

Chaincode function to display struct values

问题

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

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("初始化完成!")
	fmt.Printf("初始化完成!")

	return nil, nil
}

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

	if len(args) < 3 {
		return nil, errors.New("插入表失败。必须包括3个列值")
	}

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

	//希望打印与UID对应的特定客户的所有详细信息
	return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error) {
	function, args := stub.GetFunctionAndParameters()
	fmt.Printf("在Invoke中 %s", function)
	if function == "setDetails" {
		return t.setDetails(stub, args)

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

	return nil, errors.New("无效的调用函数名称。期望\"query\"")
}

func main() {
	err := shim.Start(new(SimpleChaincode))
	if err != nil {
		fmt.Printf("启动简单链码时出错:%s", err)
	}
}

以上是你要翻译的内容。

英文:

I'm trying to write a simple chaincode that uses a structure to store customer details. I have one setDetails func that works fine. I wish to write another getDetails func that takes UID as arguement and prints the details of the customer with that UID. Need Help!

package main
import (
&quot;errors&quot;
&quot;fmt&quot;
&quot;github.com/hyperledger/fabric/core/chaincode/shim&quot;
)
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(&quot;initialization done!!!&quot;)
fmt.Printf(&quot;initialization done!!!&quot;)
return nil, nil
}
func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) &lt; 3 {
return nil, errors.New(&quot;insert Into Table failed. Must include 3 column values&quot;)
}
customer := &amp;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(&quot;Inside Invoke %s&quot;, function)
if function == &quot;setDetails&quot; {
return t.setDetails(stub, args)
} else if function == &quot;getDetails&quot; {
return t.getDetails(stub, args)
}
return nil, errors.New(&quot;Invalid invoke function name. Expecting  \&quot;query\&quot;&quot;)
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf(&quot;Error starting Simple chaincode: %s&quot;, err)
}
}

答案1

得分: 1

我之前不知道Hyperledger,但是在查看了GitHub文档之后,我了解到你可以使用stub.PutState来存储客户信息,然后使用stub.GetState来获取它。

由于这两个方法都需要一个字节切片作为参数,我猜想代码可能是这样的:

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

    if len(args) < 3 {
        return nil, errors.New("插入表失败。必须包含3个列值")
    }

    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.PutState(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("参数数量不正确。期望查询的键名")
    }

    return stub.GetState(args[0])
}

以上是翻译好的内容,请确认是否满意。

英文:

I didn't know about Hyperledger until now, but after a look at the github documentation, I get you would use stub.PutState to store your customer information, then later use stub.GetState to get it back.

As both methods request a byte slice, my guess would be something along these lines:

func (t *SimpleChaincode) setDetails(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) &lt; 3 {
return nil, errors.New(&quot;insert Into Table failed. Must include 3 column values&quot;)
}
customer := &amp;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(&quot;Incorrect number of arguments. Expecting name of the key to query&quot;)
}
return stub.GetState(args[0])
}

huangapple
  • 本文由 发表于 2017年1月23日 18:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/41803562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定