-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(keeper): implement json primitive return #2949
base: master
Are you sure you want to change the base?
Changes from all commits
7821510
11c0327
44de899
aca85fb
dd00a47
6347f25
e906187
cf7e6d0
a9d0af8
e5d26e1
424308a
9aa7139
d574bf7
4db0338
eb85c50
d215c18
35ed3d9
6a47f6a
ceb0f3b
a3a2ce6
ce91f7e
b251235
16d496d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package keyscli | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/amino" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys/client" | ||
) | ||
|
||
func NewJSONQueryCmd(rootCfg *client.BaseCfg, io commands.IO) *commands.Command { | ||
cfg := &client.QueryCfg{ | ||
RootCfg: rootCfg, | ||
} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "jquery", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name gives me shivers. Could we not do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What case do you see with broadcast/maketx and JSON? I personally like "jquery"; it sounds like "anti-web 2.0." :) |
||
ShortUsage: "jquery [flags] <path>", | ||
ShortHelp: "EXPERIMENTAL: makes an ABCI query and return a result in json", | ||
LongHelp: "EXPERIMENTAL: makes an ABCI query and return a result in json", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execJSONQuery(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
func execJSONQuery(cfg *client.QueryCfg, args []string, io commands.IO) error { | ||
if len(args) != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
cfg.Path = args[0] | ||
if cfg.Path == "vm/qeval" { | ||
// automatically add json suffix for qeval | ||
cfg.Path = cfg.Path + "/json" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks weird. what's the goal? maybe add a comment if there is a valid reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. The reason for that is this |
||
} | ||
|
||
qres, err := client.QueryHandler(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var output struct { | ||
Response json.RawMessage `json:"response"` | ||
Returns json.RawMessage `json:"returns,omitempty"` | ||
} | ||
|
||
if output.Response, err = amino.MarshalJSONIndent(qres.Response, "", " "); err != nil { | ||
io.ErrPrintfln("Unable to marshal response %+v\n", qres) | ||
return fmt.Errorf("amino marshal json error: %w", err) | ||
} | ||
|
||
// XXX: this is probably too specific | ||
if cfg.Path == "vm/qeval/json" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be a preliminary check raising an error at the beginning of the function? |
||
if len(qres.Response.Data) > 0 { | ||
output.Returns = qres.Response.Data | ||
} else { | ||
output.Returns = []byte("[]") | ||
} | ||
} | ||
|
||
var buff bytes.Buffer | ||
jqueryEnc := json.NewEncoder(&buff) | ||
jqueryEnc.SetEscapeHTML(false) // disable HTML escaping, as we want to correctly display `<`, `>` | ||
jqueryEnc.SetIndent("", " ") | ||
|
||
if err := jqueryEnc.Encode(output); err != nil { | ||
io.ErrPrintfln("Unable to marshal\n Response: %+v\n Returns: %+v\n", | ||
string(output.Response), | ||
string(output.Returns), | ||
) | ||
return fmt.Errorf("marshal json error: %w", err) | ||
} | ||
|
||
io.Println(buff.String()) | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zivkovicmilos, we don't need to port this module in this PR, but I'd like your opinion on what the diff will look like thanks to the new JSON built-in support.