Go- Chaincode function to display struct values

0 votes

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. 

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

Sep 19, 2018 in Blockchain by slayer
• 29,370 points
1,597 views

1 answer to this question.

0 votes

Try this

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])
}
answered Sep 19, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

Updating data of struct to save in chaincode.

You can use a code similar to ...READ MORE

answered Jul 5, 2018 in Blockchain by slayer
• 29,370 points
1,199 views
0 votes
1 answer

Unable to unmarshal non-string into Go struct field SendTxArgs from of common.Address

Probably you should specify the default_account for client(sender address). In my ...READ MORE

answered Aug 28, 2018 in Blockchain by Johnathon
• 9,090 points
2,185 views
+1 vote
0 answers
+16 votes
4 answers
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,003 views
+1 vote
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP