Browse Source

Merge pull request #43 from mappum/develop

tmsp-cli arg string handling + Make dummy query response values more clear
pull/1780/head
Ethan Buchman 8 years ago
committed by GitHub
parent
commit
465c77e120
4 changed files with 40 additions and 23 deletions
  1. +24
    -8
      cmd/tmsp-cli/tmsp-cli.go
  2. +6
    -5
      example/dummy/dummy.go
  3. +4
    -4
      tests/test_cli/ex1.tmsp
  4. +6
    -6
      tests/test_cli/ex1.tmsp.out

+ 24
- 8
cmd/tmsp-cli/tmsp-cli.go View File

@ -213,7 +213,10 @@ func cmdAppendTx(c *cli.Context) error {
if len(args) != 1 {
return errors.New("Command append_tx takes 1 argument")
}
txBytes := stringOrHexToBytes(c.Args()[0])
txBytes, err := stringOrHexToBytes(c.Args()[0])
if err != nil {
return err
}
res := client.AppendTxSync(txBytes)
printResponse(c, res, string(res.Data), true)
return nil
@ -225,7 +228,10 @@ func cmdCheckTx(c *cli.Context) error {
if len(args) != 1 {
return errors.New("Command check_tx takes 1 argument")
}
txBytes := stringOrHexToBytes(c.Args()[0])
txBytes, err := stringOrHexToBytes(c.Args()[0])
if err != nil {
return err
}
res := client.CheckTxSync(txBytes)
printResponse(c, res, string(res.Data), true)
return nil
@ -244,7 +250,10 @@ func cmdQuery(c *cli.Context) error {
if len(args) != 1 {
return errors.New("Command query takes 1 argument")
}
queryBytes := stringOrHexToBytes(c.Args()[0])
queryBytes, err := stringOrHexToBytes(c.Args()[0])
if err != nil {
return err
}
res := client.QuerySync(queryBytes)
printResponse(c, res, string(res.Data), true)
return nil
@ -277,13 +286,20 @@ func printResponse(c *cli.Context, res types.Result, s string, printCode bool) {
}
// NOTE: s is interpreted as a string unless prefixed with 0x
func stringOrHexToBytes(s string) []byte {
if len(s) > 2 && s[:2] == "0x" {
func stringOrHexToBytes(s string) ([]byte, error) {
if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
b, err := hex.DecodeString(s[2:])
if err != nil {
fmt.Println("Error decoding hex argument:", err.Error())
err = fmt.Errorf("Error decoding hex argument: %s", err.Error())
return nil, err
}
return b
return b, nil
}
if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") {
err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s)
return nil, err
}
return []byte(s)
return []byte(s[1 : len(s)-1]), nil
}

+ 6
- 5
example/dummy/dummy.go View File

@ -1,6 +1,7 @@
package dummy
import (
"encoding/hex"
"strings"
. "github.com/tendermint/go-common"
@ -48,13 +49,13 @@ func (app *DummyApplication) Commit() types.Result {
func (app *DummyApplication) Query(query []byte) types.Result {
index, value, exists := app.state.Get(query)
queryResult := QueryResult{index, string(value), exists}
queryResult := QueryResult{index, string(value), hex.EncodeToString(value), exists}
return types.NewResultOK(wire.JSONBytes(queryResult), "")
}
type QueryResult struct {
Index int `json:"index"`
Value string `json:"value"`
Exists bool `json:"exists"`
Index int `json:"index"`
Value string `json:"value"`
ValueHex string `json:"valueHex"`
Exists bool `json:"exists"`
}

+ 4
- 4
tests/test_cli/ex1.tmsp View File

@ -1,10 +1,10 @@
echo hello
info
commit
append_tx abc
append_tx "abc"
info
commit
query abc
append_tx def=xyz
query "abc"
append_tx "def=xyz"
commit
query def
query "def"

+ 6
- 6
tests/test_cli/ex1.tmsp.out View File

@ -7,7 +7,7 @@
> commit
-> data: 0x
> append_tx abc
> append_tx "abc"
-> code: OK
> info
@ -16,17 +16,17 @@
> commit
-> data: 0x750502FC7E84BBD788ED589624F06CFA871845D1
> query abc
> query "abc"
-> code: OK
-> data: {"index":0,"value":"abc","exists":true}
-> data: {"index":0,"value":"abc","valueHex":"616263","exists":true}
> append_tx def=xyz
> append_tx "def=xyz"
-> code: OK
> commit
-> data: 0x76393B8A182E450286B0694C629ECB51B286EFD5
> query def
> query "def"
-> code: OK
-> data: {"index":1,"value":"xyz","exists":true}
-> data: {"index":1,"value":"xyz","valueHex":"78797a","exists":true}

Loading…
Cancel
Save