Last active
March 28, 2021 14:36
-
-
Save sneljo1/13b180ae490177b0f31309fec2bd7bda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string)([] byte, error) { | |
fmt.Printf("- getQueryResultForQueryString queryString:\n%s\n", queryString) | |
resultsIterator, err: = stub.GetQueryResult(queryString) | |
defer resultsIterator.Close() | |
if err != nil { | |
return nil, err | |
} | |
// buffer is a JSON array containing QueryRecords | |
var buffer bytes.Buffer | |
buffer.WriteString("[") | |
bArrayMemberAlreadyWritten: = false | |
for resultsIterator.HasNext() { | |
queryResponse, | |
err: = resultsIterator.Next() | |
if err != nil { | |
return nil, err | |
} | |
// Add a comma before array members, suppress it for the first array member | |
if bArrayMemberAlreadyWritten == true { | |
buffer.WriteString(",") | |
} | |
buffer.WriteString("{\"Key\":") | |
buffer.WriteString("\"") | |
buffer.WriteString(queryResponse.Key) | |
buffer.WriteString("\"") | |
buffer.WriteString(", \"Record\":") | |
// Record is a JSON object, so we write as-is | |
buffer.WriteString(string(queryResponse.Value)) | |
buffer.WriteString("}") | |
bArrayMemberAlreadyWritten = true | |
} | |
buffer.WriteString("]") | |
fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String()) | |
return buffer.Bytes(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment