I have written a go language to read value from a struct and display the value of required fields.
My go code is as below
//==================================================================================================
//================================== STRUCT FOR ALL ================================================
type purchaseOrder struct {
PONo string json:"PONo"
OrderDate string json:"OrderDate"
// ItemQty int json:"ItemQty"
ExpDelDate string json:"ExpDelDate"
ActualDelDate string json:"ActualDelDate"
DeliveryAdd string json:"DeliveryAdd"
Status string json:"Status"
Amount float64 json:"Amount"
Product_lists []Product_Details json:"Product_lists"
salesorder salesorder json:"Salesorder"
receiptdetails receiptdetails json:"Receiptdetails"
invoicedetails invoicedetails json:"Invoicedetails"
APAR_Details APAR_Details json:"apar_details"
}
type salesorder struct {
SOnum string json:"SOnum"
ShipTo string json:"ShipTo"
Companyname string json:"Companyname"
Address string json:"Address"
Quantity int json:"Quantity"
Deliverydate string json:"Deliverydate"
Unitprice string json:"Unitprice"
Value float64 json:"Value"
Purchasedate string json:"Purchasedate"
}
type receiptdetails struct {
ReceiptNo string json:"receiptNo"
Creationdate string json:"creationdate"
Status string json:"status"
Description string json:"description"
}
type invoicedetails struct {
InvoiceNo string json:"ReceiptNo"
Invoicedate string json:"Invoicedate"
BillTo string json:"BillTo"
Item string json:"Item"
Amount float64 json:"Amount"
}
type Product_Details struct {
ItemNo string json:"itemNo"
ItemName string json:"itemName"
Quantity int json:"quantity"
itemPrice float64 json:"itemPrice"
Value float64 json:"Value"
}
I have written function to do some validation,function code is as below
func (t *Actpay) Validate(stub shim.ChaincodeStubInterface, args []string) peer.Response {
fmt.Println("Validate using 3 way matching")
if len(args) != 4 {
return shim.Error("Incorrect Number of arguments.Expecting 4 for validation")
}
// Fetching PO details from ledger
var PoId = strings.ToLower(args[0])
bytes, err := stub.GetState(PoId)
var SoId = strings.ToLower(args[1])
bytes1, err := stub.GetState(SoId)
var RCId = strings.ToLower(args[2])
bytes2, err := stub.GetState(RCId)
var INId = strings.ToLower(args[3])
bytes3, err := stub.GetState(INId)
if err != nil {
return Error(http.StatusInternalServerError, err.Error())
}
var Purchase purchaseOrder
var Receipt receiptdetails
var Invoice invoicedetails
var Sales salesorder
err = json.Unmarshal(bytes, &Purchase)
err = json.Unmarshal(bytes1, &Sales)
err = json.Unmarshal(bytes2,&Receipt)
err = json.Unmarshal(bytes3, &Invoice)
if (Receipt.Status == "Delivered" && Invoice.Amount == Sales.Value && Sales.Value == Purchase.Amount){
return Success(http.StatusCreated, "validated", nil)
} else {
return Error(http.StatusInternalServerError, err.Error())
}
}
I am getting the below error:
json: cannot unmarshal array into Go value of type main.salesorder
Can some body suggest how can i get the fields from inner struct.