Skip to content

Commit 930bd55

Browse files
committed
Make CLI support parameter Args with or without Function
This commit makes it possible to pass in a JSON string containing Args and Function keys or just Args key as a chaincode input object for chaincode operations. Change-Id: I46fefc1eff0de169b0c5efbedbdea038e8cc9d3e Signed-off-by: Gabor Hosszu <[email protected]>
1 parent 0f959c0 commit 930bd55

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

peer/chaincode/common.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,17 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
187187
return fmt.Errorf("Chaincode argument error: %s", err)
188188
}
189189
m := f.(map[string]interface{})
190-
if len(m) != 2 {
191-
return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 2 keys: 'Function' and 'Args'")
192-
}
190+
sm := make(map[string]interface{})
193191
for k := range m {
194-
switch strings.ToLower(k) {
195-
case "args":
196-
case "function":
197-
default:
198-
return fmt.Errorf("Illegal chaincode key '%s' - must be 'Function' or 'Args'", k)
199-
}
192+
sm[strings.ToLower(k)] = m[k]
193+
}
194+
_, argsPresent := sm["args"]
195+
_, funcPresent := sm["function"]
196+
if !argsPresent || (len(m) == 2 && !funcPresent) || len(m) > 2 {
197+
return fmt.Errorf("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'")
200198
}
201199
} else {
202-
return errors.New("Empty JSON chaincode parameters must contain exactly 2 keys: 'Function' and 'Args'")
200+
return errors.New("Empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'")
203201
}
204202

205203
if chaincodeAttributesJSON != "[]" {

peer/chaincode/common_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package chaincode
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestCheckChaincodeCmdParamsWithNewCallingSchema(t *testing.T) {
26+
chaincodeAttributesJSON = "[]"
27+
chaincodeCtorJSON = `{ "Args":["func", "param"] }`
28+
chaincodePath = "some/path"
29+
require := require.New(t)
30+
result := checkChaincodeCmdParams(nil)
31+
32+
require.Nil(result)
33+
}
34+
35+
func TestCheckChaincodeCmdParamsWithOldCallingSchema(t *testing.T) {
36+
chaincodeAttributesJSON = "[]"
37+
chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }`
38+
chaincodePath = "some/path"
39+
require := require.New(t)
40+
result := checkChaincodeCmdParams(nil)
41+
42+
require.Nil(result)
43+
}
44+
45+
func TestCheckChaincodeCmdParamsWithFunctionOnly(t *testing.T) {
46+
chaincodeAttributesJSON = "[]"
47+
chaincodeCtorJSON = `{ "Function":"func" }`
48+
chaincodePath = "some/path"
49+
require := require.New(t)
50+
result := checkChaincodeCmdParams(nil)
51+
52+
require.Error(result)
53+
}
54+
55+
func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) {
56+
chaincodeAttributesJSON = "[]"
57+
chaincodeCtorJSON = `{}`
58+
chaincodePath = "some/path"
59+
require := require.New(t)
60+
result := checkChaincodeCmdParams(nil)
61+
62+
require.Error(result)
63+
}

0 commit comments

Comments
 (0)