Skip to content

Commit

Permalink
Use string arguments in CLI
Browse files Browse the repository at this point in the history
Change-Id: I41c2147090a538a91f8ebe6b0f78b44ce5bee45c
Signed-off-by: Gabor Hosszu <gabor@digitalasset.com>
  • Loading branch information
gaborh-da committed Aug 12, 2016
1 parent 457635a commit 35522bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
25 changes: 15 additions & 10 deletions peer/chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/hyperledger/fabric/core"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/peer/common"
"github.com/hyperledger/fabric/peer/util"
pb "github.com/hyperledger/fabric/protos"
Expand All @@ -33,6 +34,10 @@ import (
"golang.org/x/net/context"
)

type container struct {
Args []string
}

// chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
// INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
// the query result on STDOUT. A command-line flag (-r, --raw) determines
Expand All @@ -57,11 +62,12 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
}
// Build the spec
input := &pb.ChaincodeInput{}
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
inputc := container{}
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
return
}

input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}
var attributes []string
if err = json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
Expand Down Expand Up @@ -172,9 +178,9 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
}
}

// Check that non-empty chaincode parameters contain only Function and
// Args keys. Type checking is done later when the JSON is actually
// unmarshaled into a pb.ChaincodeInput. To better understand what's going
// Check that non-empty chaincode parameters contain only Args as a key.
// Type checking is done later when the JSON is actually unmarshaled
// into a pb.ChaincodeInput. To better understand what's going
// on here with JSON parsing see http://blog.golang.org/json-and-go -
// Generic JSON with interface{}
if chaincodeCtorJSON != "{}" {
Expand All @@ -184,19 +190,18 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
return fmt.Errorf("Chaincode argument error: %s", err)
}
m := f.(map[string]interface{})
if len(m) != 2 {
return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 2 keys - 'Function' and 'Args'")
if len(m) != 1 {
return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 1 key: 'Args'")
}
for k := range m {
switch strings.ToLower(k) {
case "function":
case "args":
default:
return fmt.Errorf("Illegal chaincode key '%s' - must be either 'Function' or 'Args'", k)
return fmt.Errorf("Illegal chaincode key '%s' - must be only 'Args'", k)
}
}
} else {
return errors.New("Empty JSON chaincode parameters must contain exactly 2 keys - 'Function' and 'Args'")
return errors.New("Empty JSON chaincode parameters must contain exactly 1 key: 'Args'")
}

if chaincodeAttributesJSON != "[]" {
Expand Down
5 changes: 4 additions & 1 deletion peer/chaincode/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"golang.org/x/net/context"

"github.com/hyperledger/fabric/core"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/peer/common"
"github.com/hyperledger/fabric/peer/util"
pb "github.com/hyperledger/fabric/protos"
Expand Down Expand Up @@ -62,9 +63,11 @@ func chaincodeDeploy(cmd *cobra.Command, args []string) error {
}
// Build the spec
input := &pb.ChaincodeInput{}
if err := json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
inputc := container{}
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &inputc); err != nil {
return fmt.Errorf("Chaincode argument error: %s", err)
}
input = &pb.ChaincodeInput{Args: shim.ToChaincodeArgs(inputc.Args...)}

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

0 comments on commit 35522bf

Please sign in to comment.