2017-01-23 8 views
0

Ich versuche, einen einfachen Chaincode zu schreiben, der eine Struktur verwendet, um Kundendetails zu speichern. Ich habe eine SetDetails Funktion, die gut funktioniert. Ich möchte eine andere getDetails-Funktion schreiben, die UID als Argument nimmt und die Details des Kunden mit dieser UID ausgibt. Brauche Hilfe!Chaincode-Funktion zum Anzeigen von Strukturwerten

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

Antwort

0

ich wusste nicht, bis jetzt über Hyperledger, aber nach einem Blick auf die GitHub Dokumentation, erhalte ich Sie stub.PutState verwenden würde Ihre Kundendaten zu speichern, dann stub.GetState später verwenden Sie es zurück zu bekommen.

Da beide Methoden eine Byte Scheibe bitten, meine Vermutung etwas in diese Richtung wäre:

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

Dank! ich werde es versuchen – Aditi

Verwandte Themen