Skip to content
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(autocli): add json file parsing support #15451

Merged
merged 15 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion client/v2/autocli/flag/message_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package flag
import (
"context"
"fmt"
"io"
"os"
"regexp"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
Expand All @@ -11,6 +14,8 @@ import (
"cosmossdk.io/client/v2/internal/util"
)

var isJSONFileRegex = regexp.MustCompile(`\.json$`)

type jsonMessageFlagType struct {
messageDesc protoreflect.MessageDescriptor
}
Expand Down Expand Up @@ -55,7 +60,20 @@ func (j *jsonMessageFlagValue) String() string {

func (j *jsonMessageFlagValue) Set(s string) error {
j.message = j.messageType.New().Interface()
return j.jsonUnmarshalOptions.Unmarshal([]byte(s), j.message)
var messageBytes []byte
if isJSONFileRegex.MatchString(s) {
jsonFile, err := os.Open(s)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
if err != nil {
return err
}
messageBytes, err = io.ReadAll(jsonFile)
if err != nil {
return err
}
} else {
messageBytes = []byte(s)
}
return j.jsonUnmarshalOptions.Unmarshal(messageBytes, j.message)
}

func (j *jsonMessageFlagValue) Type() string {
Expand Down
19 changes: 19 additions & 0 deletions client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ func TestEverything(t *testing.T) {
assert.DeepEqual(t, conn.lastRequest, conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())
}

func TestJSONParsing(t *testing.T) {
conn := testExecCommon(t, buildModuleQueryCommand,
"echo",
"1", "abc", `{"denom":"foo","amount":"1"}`,
"--some-messages", `{"bar":"baz"}`,
"-u", "27", // shorthand
)
assert.DeepEqual(t, conn.lastRequest, conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())

conn = testExecCommon(t, buildModuleQueryCommand,
"echo",
"1", "abc", `{"denom":"foo","amount":"1"}`,
"--some-messages", "testdata/some_message.json",
"-u", "27", // shorthand
)
assert.DeepEqual(t, conn.lastRequest, conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform())

}

func TestOptions(t *testing.T) {
conn := testExecCommon(t, buildModuleQueryCommand,
"echo",
Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/testdata/some_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"bar":"baz"}