Skip to content

Commit

Permalink
Merge "Use string arguments in CLI"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Aug 16, 2016
2 parents 9fac6c4 + 35522bf commit e164856
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 @@ -173,9 +179,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 @@ -185,19 +191,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 e164856

Please sign in to comment.