Skip to content

Commit 35522bf

Browse files
committed
Use string arguments in CLI
Change-Id: I41c2147090a538a91f8ebe6b0f78b44ce5bee45c Signed-off-by: Gabor Hosszu <[email protected]>
1 parent 457635a commit 35522bf

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

peer/chaincode/common.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"github.com/hyperledger/fabric/core"
28+
"github.com/hyperledger/fabric/core/chaincode/shim"
2829
"github.com/hyperledger/fabric/peer/common"
2930
"github.com/hyperledger/fabric/peer/util"
3031
pb "github.com/hyperledger/fabric/protos"
@@ -33,6 +34,10 @@ import (
3334
"golang.org/x/net/context"
3435
)
3536

37+
type container struct {
38+
Args []string
39+
}
40+
3641
// chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
3742
// INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
3843
// the query result on STDOUT. A command-line flag (-r, --raw) determines
@@ -57,11 +62,12 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
5762
}
5863
// Build the spec
5964
input := &pb.ChaincodeInput{}
60-
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
65+
inputc := container{}
66+
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
6167
err = fmt.Errorf("Chaincode argument error: %s", err)
6268
return
6369
}
64-
70+
input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
6571
var attributes []string
6672
if err = json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
6773
err = fmt.Errorf("Chaincode argument error: %s", err)
@@ -172,9 +178,9 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
172178
}
173179
}
174180

175-
// Check that non-empty chaincode parameters contain only Function and
176-
// Args keys. Type checking is done later when the JSON is actually
177-
// unmarshaled into a pb.ChaincodeInput. To better understand what's going
181+
// Check that non-empty chaincode parameters contain only Args as a key.
182+
// Type checking is done later when the JSON is actually unmarshaled
183+
// into a pb.ChaincodeInput. To better understand what's going
178184
// on here with JSON parsing see http://blog.golang.org/json-and-go -
179185
// Generic JSON with interface{}
180186
if chaincodeCtorJSON != "{}" {
@@ -184,19 +190,18 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
184190
return fmt.Errorf("Chaincode argument error: %s", err)
185191
}
186192
m := f.(map[string]interface{})
187-
if len(m) != 2 {
188-
return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 2 keys - 'Function' and 'Args'")
193+
if len(m) != 1 {
194+
return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 1 key: 'Args'")
189195
}
190196
for k := range m {
191197
switch strings.ToLower(k) {
192-
case "function":
193198
case "args":
194199
default:
195-
return fmt.Errorf("Illegal chaincode key '%s' - must be either 'Function' or 'Args'", k)
200+
return fmt.Errorf("Illegal chaincode key '%s' - must be only 'Args'", k)
196201
}
197202
}
198203
} else {
199-
return errors.New("Empty JSON chaincode parameters must contain exactly 2 keys - 'Function' and 'Args'")
204+
return errors.New("Empty JSON chaincode parameters must contain exactly 1 key: 'Args'")
200205
}
201206

202207
if chaincodeAttributesJSON != "[]" {

peer/chaincode/deploy.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"golang.org/x/net/context"
2828

2929
"github.com/hyperledger/fabric/core"
30+
"github.com/hyperledger/fabric/core/chaincode/shim"
3031
"github.com/hyperledger/fabric/peer/common"
3132
"github.com/hyperledger/fabric/peer/util"
3233
pb "github.com/hyperledger/fabric/protos"
@@ -62,9 +63,11 @@ func chaincodeDeploy(cmd *cobra.Command, args []string) error {
6263
}
6364
// Build the spec
6465
input := &pb.ChaincodeInput{}
65-
if err := json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
66+
inputc := container{}
67+
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
6668
return fmt.Errorf("Chaincode argument error: %s", err)
6769
}
70+
input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
6871

6972
var attributes []string
7073
if err := json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {

0 commit comments

Comments
 (0)