Skip to content

Commit

Permalink
remove tip when null
Browse files Browse the repository at this point in the history
  • Loading branch information
giunatale committed Apr 26, 2024
1 parent 9140f02 commit 138c7d7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go build -C cmd/cosmos-signer -o ../../build/cosmos-signer
16 changes: 15 additions & 1 deletion cmd/cosmos-signer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/atomone-hub/cosmos-signer/app"
signercli "github.com/atomone-hub/cosmos-signer/x/signer/client/cli"
)

const (
Expand All @@ -49,7 +50,7 @@ func NewRootCmd() *cobra.Command {
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// set the default command outputs
cmd.SetOut(cmd.OutOrStdout())
cmd.SetOut(signercli.NewFilterNullValJSON(cmd.OutOrStdout()))
cmd.SetErr(cmd.ErrOrStderr())

clientCtx = clientCtx.WithCmdContext(cmd.Context())
Expand Down Expand Up @@ -116,6 +117,19 @@ func NewRootCmd() *cobra.Command {
return err
}

return nil
},
PersistentPostRunE: func(cmd *cobra.Command, _ []string) error {
txCmd, _, err := cmd.Find([]string{"tx"})
if err != nil {
return err
}
outputDoc, err := txCmd.Flags().GetString(flags.FlagOutputDocument)
if err != nil {
return err
}
signercli.FilterNullJSONKeysFromFile(outputDoc)

return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion x/signer/client/cli/register_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
)

func RegisterSdkMsgsDynamic(ctx client.Context, pluginsDir string, unregisteredTypes map[string]struct{}) error {
func RegisterTypes(ctx client.Context, pluginsDir string, unregisteredTypes map[string]struct{}) error {
legacyAminoCodec := ctx.LegacyAmino
registry := ctx.Codec.InterfaceRegistry()
files, err := filepath.Glob(filepath.Join(pluginsDir, "*.so"))
Expand Down
5 changes: 1 addition & 4 deletions x/signer/client/cli/tx_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func preSignCmd(cmd *cobra.Command, _ []string) {
func makeSignCmd(origMakeSignCmd func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var clientCtx client.Context

clientCtx, err = client.GetClientTxContext(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -82,12 +81,10 @@ func makeSignCmd(origMakeSignCmd func(cmd *cobra.Command, args []string) error)
if err != nil {
return err
}
err = RegisterSdkMsgsDynamic(clientCtx, pluginsDir, unregisteredTypes)
err = RegisterTypes(clientCtx, pluginsDir, unregisteredTypes)
if err != nil {
return err
}

client.SetCmdClientContext(cmd, clientCtx) // not sure if this is needed
}

return origMakeSignCmd(cmd, args)
Expand Down
94 changes: 94 additions & 0 deletions x/signer/client/cli/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cli

import (
"encoding/json"
"io"
"os"
)

var (
nullKeys = []string{"tip"}
)

// FilterNullValJSON implements the io.Writer interface
type FilterNullValJSON struct {
Output io.Writer
NullKeys []string
}

func NewFilterNullValJSON(output io.Writer) *FilterNullValJSON {
return &FilterNullValJSON{
Output: output,
NullKeys: nullKeys,
}
}

func (w *FilterNullValJSON) Write(p []byte) (n int, err error) {
var data interface{}
if err := json.Unmarshal(p, &data); err != nil {
return 0, err
}
filteredData := FilterNullJSONKeys(data)
filteredBytes, err := json.Marshal(filteredData)
if err != nil {
return 0, err
}
return w.Output.Write(filteredBytes)
}

// FilterNullJSONKeys recursively filters out null values from JSON for specified keys
func FilterNullJSONKeys(data interface{}) interface{} {
switch v := data.(type) {
case map[string]interface{}:
for key, val := range v {
if shouldCheckKey(key) {
if val == nil {
delete(v, key)
} else {
v[key] = FilterNullJSONKeys(val)
}
} else {
v[key] = FilterNullJSONKeys(val) // Recursively process all keys
}
}
return v
case []interface{}:
for i := range v {
v[i] = FilterNullJSONKeys(v[i]) // Recursively process array elements
}
return v
default:
return v
}
}

// shouldCheckKey checks if the key is one of the specified keys to check
func shouldCheckKey(key string) bool {
for _, k := range nullKeys {
if key == k {
return true
}
}
return false
}

func FilterNullJSONKeysFromFile(outputDoc string) {
if outputDoc != "" {
content, err := os.ReadFile(outputDoc)
if err != nil {
panic(err)
}
var data interface{}
if err := json.Unmarshal(content, &data); err != nil {
panic(err)
}
filteredData := FilterNullJSONKeys(data)
filteredBytes, err := json.Marshal(filteredData)
if err != nil {
panic(err)
}
if err := os.WriteFile(outputDoc, filteredBytes, 0644); err != nil {
panic(err)
}
}
}

0 comments on commit 138c7d7

Please sign in to comment.