-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osmocli: parse
Use
field's arguments dynamically from a message (ba…
…ckport #6005) (#6240) * osmocli: parse Use field's arguments dynamically from a message (backport #6005) * go sum --------- Co-authored-by: Roman <[email protected]>
- Loading branch information
1 parent
cf69b87
commit 965a751
Showing
29 changed files
with
301 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package osmocli | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
|
||
"github.com/osmosis-labs/osmosis/osmoutils" | ||
) | ||
|
||
type Descriptor interface { | ||
GetCustomFlagOverrides() map[string]string | ||
AttachToUse(str string) | ||
} | ||
|
||
// fields that are not provided as arguments | ||
var nonAttachableFields []string = []string{"sender", "pagination", "owner", "admin"} | ||
|
||
// attachFieldsToUse extracts fields from reqP proto message and dynamically appends them into Use field | ||
func attachFieldsToUse[reqP proto.Message](desc Descriptor) { | ||
req := osmoutils.MakeNew[reqP]() | ||
v := reflect.ValueOf(req).Type().Elem() // get underlying non-pointer struct | ||
var useField string | ||
for i := 0; i < v.NumField(); i++ { | ||
fn := pascalToKebab(v.Field(i).Name) | ||
|
||
// if a field is parsed from a flag, skip it | ||
if desc.GetCustomFlagOverrides()[fn] != "" || osmoutils.Contains(nonAttachableFields, fn) { | ||
continue | ||
} | ||
|
||
useField += fmt.Sprintf(" [%s]", fn) | ||
} | ||
|
||
desc.AttachToUse(useField) | ||
} | ||
|
||
// pascalToKebab converts PascalCase string to kebab-case string | ||
func pascalToKebab(s string) string { | ||
reg := regexp.MustCompile(`([a-z0-9])([A-Z])`) | ||
s = reg.ReplaceAllString(s, `${1}-${2}`) | ||
|
||
// Convert everything to lowercase | ||
return strings.ToLower(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package osmocli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
clqueryproto "github.com/osmosis-labs/osmosis/v18/x/concentrated-liquidity/client/queryproto" | ||
cltypes "github.com/osmosis-labs/osmosis/v18/x/concentrated-liquidity/types" | ||
lockuptypes "github.com/osmosis-labs/osmosis/v18/x/lockup/types" | ||
) | ||
|
||
// test-specific helper descriptor | ||
type TestDescriptor interface { | ||
Descriptor | ||
GetUse() string | ||
} | ||
|
||
type TestQueryDescriptor struct { | ||
*QueryDescriptor | ||
} | ||
|
||
func (tqd *TestQueryDescriptor) GetUse() string { | ||
return tqd.Use | ||
} | ||
|
||
type TestTxCliDescriptor struct { | ||
*TxCliDesc | ||
} | ||
|
||
func (ttxcd *TestTxCliDescriptor) GetUse() string { | ||
return ttxcd.Use | ||
} | ||
|
||
func TestAttachFieldsToUse(t *testing.T) { | ||
tests := map[string]struct { | ||
desc TestDescriptor | ||
attachFunc func(Descriptor) | ||
expectedUse string | ||
}{ | ||
"basic/TxCliDesc/attaches_2_args": { | ||
desc: &TestTxCliDescriptor{ | ||
&TxCliDesc{ | ||
Use: "set-reward-receiver-address", | ||
Short: "sets reward receiver address for the designated lock id", | ||
Long: "sets reward receiver address for the designated lock id", | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*lockuptypes.MsgSetRewardReceiverAddress], | ||
expectedUse: "set-reward-receiver-address [lock-id] [reward-receiver]", | ||
}, | ||
"basic/QueryDescriptor/attaches_1_arg": { | ||
desc: &TestQueryDescriptor{ | ||
&QueryDescriptor{ | ||
Use: "pool-accumulator-rewards", | ||
Short: "Query pool accumulator rewards", | ||
Long: `{{.Short}}{{.ExampleHeader}} | ||
{{.CommandPrefix}} pool-accumulator-rewards 1`, | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*clqueryproto.PoolAccumulatorRewardsRequest], | ||
expectedUse: "pool-accumulator-rewards [pool-id]", | ||
}, | ||
"ignore_pagination/QueryDescriptor/no_args": { | ||
desc: &TestQueryDescriptor{ | ||
&QueryDescriptor{ | ||
Use: "pools", | ||
Short: "Query pools", | ||
Long: `{{.Short}}{{.ExampleHeader}} | ||
{{.CommandPrefix}} pools`, | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*clqueryproto.PoolsRequest], | ||
expectedUse: "pools", | ||
}, | ||
"ignore_owner/TxCliDesc/attach_2_args": { | ||
desc: &TestTxCliDescriptor{ | ||
&TxCliDesc{ | ||
Use: "lock-tokens", | ||
Short: "lock tokens into lockup pool from user account", | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*lockuptypes.MsgLockTokens], | ||
expectedUse: "lock-tokens [duration] [coins]", // in osmosis, this command takes duration from a flag, but here it is just for testing purposes | ||
}, | ||
"ignore_sender/TxCliDesc/attach_5_args": { // also tests that args are shown in kebab-case | ||
desc: &TestTxCliDescriptor{ | ||
&TxCliDesc{ | ||
Use: "add-to-position", | ||
Short: "add to an existing concentrated liquidity position", | ||
Example: "osmosisd tx concentratedliquidity add-to-position 10 1000000000uosmo 10000000uion --from val --chain-id localosmosis -b block --keyring-backend test --fees 1000000uosmo", | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*cltypes.MsgAddToPosition], | ||
expectedUse: "add-to-position [position-id] [amount0] [amount1] [token-min-amount0] [token-min-amount1]", | ||
}, | ||
"ignore_custom_flag_overrides/TxCliDesc/": { | ||
desc: &TestTxCliDescriptor{ | ||
&TxCliDesc{ | ||
Use: "lock-tokens", | ||
Short: "lock tokens into lockup pool from user account", | ||
CustomFlagOverrides: map[string]string{ | ||
"duration": "duration", | ||
}, | ||
}, | ||
}, | ||
attachFunc: attachFieldsToUse[*lockuptypes.MsgLockTokens], | ||
expectedUse: "lock-tokens [coins]", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt.attachFunc(tt.desc) | ||
require.Equal(t, tt.desc.GetUse(), tt.expectedUse) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.