-
Notifications
You must be signed in to change notification settings - Fork 608
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
osmocli: parse Use
field's arguments dynamically from a message
#6005
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
18bbd60
dynamically attach fields' values to use of cobra cmd
pysel 82997da
update changelog
devbot-wizard 9bd8cdf
add comment
pysel 85f3661
support both tx/q
pysel 0b1be44
fix use overwriting + owner to non-attachable fields
pysel 8755fb2
change QueryDescriptor
pysel 88feddb
go fmt
pysel cd3164e
remove params in SimpleQueryCmd
pysel a10060b
refactor TxCliDesc to not pass hardcoded args
pysel 2cf361e
field names to kebab
pysel a7d1818
add test for descs
pysel d47a324
Merge branch 'main' into pysel/dynamic-use-field
pysel ba08795
reset statik
pysel 318e397
Update x/concentrated-liquidity/tick_test.go
pysel e3e4c61
Update x/concentrated-liquidity/client/grpc/grpc_query.go
pysel b8bfcb1
Update x/concentrated-liquidity/client/grpc/grpc_query.go
pysel 7aa615e
reset grpc
pysel 35a1796
reset grpc dt-det
pysel fa8a8bc
reset all grpcs
pysel e112128
remove testing function
pysel 090d59d
self review
pysel c63b8e4
Merge branch 'main' into pysel/dynamic-use-field
pysel a85e510
Merge branch 'main' into pysel/dynamic-use-field
pysel 404874b
update osmoutils version
pysel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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/v17/x/concentrated-liquidity/client/queryproto" | ||
cltypes "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/types" | ||
lockuptypes "github.com/osmosis-labs/osmosis/v17/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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
to reviewers: are there some other fields which should not be passed as arguments of cli commands?