2017-01-23 8 views
0

ストラクチャを使用して顧客の詳細を格納する単純なチェーンコードを作成しようとしています。私はうまく動作する1つのsetDetails関数を持っています。私は、UIDを引数としてとり、そのUIDを持つ顧客の詳細を表示する別のgetDetails funcを記述したいと思います。助けが必要!構造体の値を表示するためのチェーンコード関数

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

答えて

0

私は今までHyperledgerについて知らなかったが、githubのドキュメントを見た後、私はあなたが、後でそれを取り戻すためにstub.GetStateを使用して、あなたの顧客情報を保存するためにstub.PutStateを使用することになり得ます。

両方の方法は、バイトスライスを要求すると、私の推測では、これらの線に沿って何かのようになります。

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]) 
} 
+0

感謝!私はそれを試してみます – Aditi

関連する問題