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 map support #15770

Merged
merged 34 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
608b657
feat(autocli): add map support
JeancarloBarrios Apr 10, 2023
c772f7e
added bin simple map
JeancarloBarrios Apr 10, 2023
b1f39e1
add generic map type
JeancarloBarrios Apr 16, 2023
62b594a
add generic map type
JeancarloBarrios Apr 16, 2023
909d497
added missing functions from pflags
JeancarloBarrios Apr 16, 2023
19bc662
reorganize de folders
JeancarloBarrios Apr 16, 2023
194f1ec
remove composite type
JeancarloBarrios Apr 17, 2023
4af8805
add composite type
JeancarloBarrios Apr 17, 2023
5a2a220
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios Apr 17, 2023
2ecdd0e
linter change
JeancarloBarrios Apr 17, 2023
f63e841
Merge branch 'JeancarloBarrios/cli-map-support' of github.com:cosmos/…
JeancarloBarrios Apr 17, 2023
8bd224e
linter change
JeancarloBarrios Apr 17, 2023
3f537c6
linter change
JeancarloBarrios Apr 17, 2023
4602c67
linter change
JeancarloBarrios Apr 17, 2023
063e6f0
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios Apr 17, 2023
e3bfa29
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios Apr 19, 2023
3c17f9b
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios Apr 21, 2023
d16f02c
fix dead code
JeancarloBarrios May 4, 2023
0a3037d
fix unesesary declaration
JeancarloBarrios May 4, 2023
b47085e
fix dangling print
JeancarloBarrios May 4, 2023
4390236
fix imports
JeancarloBarrios May 4, 2023
ca99ce3
nit
JeancarloBarrios May 4, 2023
eebaa3f
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios May 16, 2023
314b2b9
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios May 22, 2023
af82b18
Merge branch 'JeancarloBarrios/cli-map-support' of github.com:cosmos/…
JeancarloBarrios May 24, 2023
4b3f018
aded test edge cases and remove string-string
JeancarloBarrios May 24, 2023
773c092
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios May 24, 2023
1d72109
fix: add coma separeted args
JeancarloBarrios May 26, 2023
2bd4af4
fix: add coma separeted args
JeancarloBarrios May 26, 2023
8f3c45c
Merge branch 'main' into JeancarloBarrios/cli-map-support
JeancarloBarrios May 29, 2023
de46fd9
change name to mor readable
JeancarloBarrios May 29, 2023
ff5114f
Merge branch 'JeancarloBarrios/cli-map-support' of github.com:cosmos/…
JeancarloBarrios May 29, 2023
f35af11
nit
JeancarloBarrios May 29, 2023
f3cf0d2
nit
JeancarloBarrios May 30, 2023
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
137 changes: 137 additions & 0 deletions client/v2/autocli/flag/bool_to_value.go
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package flag

import (
"github.com/spf13/pflag"
"strconv"
)

func newBoolToString[K bool, V string](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolStringMap := newGenericMapValue(val, p)
newBoolStringMap.Options = genericMapValueOptions[K, V]{
genericType: "boolToString",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary, please just return return strconv.ParseBool(s) or even better

keyParser: strconv.ParseBool,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @odeke-em could you provide me with some guidance on how to handle this error?
Cannot use 'strconv.ParseBool' (type func(str string) (bool, error)) as the type func(string) (K, error)
In order to make this change

},
valueParser: func(s string) (V, error) {
return V(s), nil
},
}
return newBoolStringMap
}

func BoolToStringP(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]string, usage string) *map[bool]string {
p := make(map[bool]string)
flagSet.VarP(newBoolToString(value, &p), name, shorthand, usage)
return &p
}

func newBoolToInt32[K bool, V int32](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolInt32Map := newGenericMapValue(val, p)
newBoolInt32Map.Options = genericMapValueOptions[K, V]{
genericType: "boolToInt32",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyParser: strconv.ParseBool,

valueParser: func(s string) (V, error) {
value, err := strconv.ParseInt(s, 10, 32)
return V(value), err
},
}
return newBoolInt32Map
}

func BoolToInt32P(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]int32, usage string) *map[bool]int32 {
p := make(map[bool]int32)
flagSet.VarP(newBoolToInt32(value, &p), name, shorthand, usage)
return &p
}

func newBoolToInt64[K bool, V int64](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolInt64Map := newGenericMapValue(val, p)
newBoolInt64Map.Options = genericMapValueOptions[K, V]{
genericType: "boolToInt64",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyParser: strconv.ParseBool

valueParser: func(s string) (V, error) {
value, err := strconv.ParseInt(s, 10, 64)
return V(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary casts and redundancy, please simply return

valueParser: func(s string) (int64, error) {
     return strconv.ParseInt(s, 10, 64)
},

}
return newBoolInt64Map
}

func BoolToInt64P(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]int64, usage string) *map[bool]int64 {
p := make(map[bool]int64)
flagSet.VarP(newBoolToInt64(value, &p), name, shorthand, usage)
return &p
}

func newBoolToUint32[K bool, V uint32](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolUint32Map := newGenericMapValue(val, p)
newBoolUint32Map.Options = genericMapValueOptions[K, V]{
genericType: "boolToUint32",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyParser: strconv.ParseBool

valueParser: func(s string) (V, error) {
value, err := strconv.ParseUint(s, 10, 32)
return V(value), err
},
}
return newBoolUint32Map
}

func BoolToUint32P(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]uint32, usage string) *map[bool]uint32 {
p := make(map[bool]uint32)
flagSet.VarP(newBoolToUint32(value, &p), name, shorthand, usage)
return &p
}

func newBoolToUint64[K bool, V uint64](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolUint64Map := newGenericMapValue(val, p)
newBoolUint64Map.Options = genericMapValueOptions[K, V]{
genericType: "boolToUint64",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyParser: strconv.ParseBool

valueParser: func(s string) (V, error) {
value, err := strconv.ParseUint(s, 10, 64)
return V(value), err
},
}
return newBoolUint64Map
}

func BoolToUint64P(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]uint64, usage string) *map[bool]uint64 {
p := make(map[bool]uint64)
flagSet.VarP(newBoolToUint64(value, &p), name, shorthand, usage)
return &p
}

func newBoolToBool[K bool, V bool](val map[K]V, p *map[K]V) *genericMapValue[K, V] {
newBoolBoolMap := newGenericMapValue(val, p)
newBoolBoolMap.Options = genericMapValueOptions[K, V]{
genericType: "boolToBool",
keyParser: func(s string) (K, error) {
value, err := strconv.ParseBool(s)
return K(value), err
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyParser: strconv.ParseBool

valueParser: func(s string) (V, error) {
value, err := strconv.ParseBool(s)
return V(value), err
},
}
return newBoolBoolMap
}

func BoolToBoolP(flagSet *pflag.FlagSet, name, shorthand string, value map[bool]bool, usage string) *map[bool]bool {
p := make(map[bool]bool)
flagSet.VarP(newBoolToBool(value, &p), name, shorthand, usage)
return &p
}
70 changes: 67 additions & 3 deletions client/v2/autocli/flag/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package flag

import (
"context"
"strconv"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
cosmos_proto "github.com/cosmos/cosmos-proto"
"github.com/spf13/pflag"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/internal/util"
cosmos_proto "github.com/cosmos/cosmos-proto"
)

// namingOptions specifies internal naming options for flags.
Expand Down Expand Up @@ -60,8 +61,14 @@ func (b *Builder) addFieldFlag(ctx context.Context, flagSet *pflag.FlagSet, fiel

// use the built-in pflag StringP, Int32P, etc. functions
var val HasValue

if field.IsList() {
val = bindSimpleListFlag(flagSet, field.Kind(), name, shorthand, usage)
}
if field.IsMap() {
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
keyKind := field.MapKey().Kind()
valKind := field.MapValue().Kind()
val = bindSimpleMapFlag(flagSet, keyKind, valKind, name, shorthand, usage)
} else {
val = bindSimpleFlag(flagSet, field.Kind(), name, shorthand, usage)
}
Expand All @@ -81,7 +88,65 @@ func (b *Builder) resolveFlagType(field protoreflect.FieldDescriptor) Type {
if typ != nil {
return compositeListType{simpleType: typ}
}
return nil
}
if field.IsMap() {
keyKind := field.MapKey().Kind()
valType := b.resolveFlagType(field.MapValue())
if valType != nil {
switch keyKind {
case protoreflect.StringKind:
ct := new(compositeMapType[string])
ct.keyValueResolver = func(s string) (string, error) { return s, nil }
ct.valueType = valType
ct.keyType = "string"
return ct
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
ct := new(compositeMapType[int32])
JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
ct.keyValueResolver = func(s string) (int32, error) {
i, err := strconv.ParseInt(s, 10, 32)
return int32(i), err
}
ct.valueType = valType
ct.keyType = "int32"
return ct
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
ct := new(compositeMapType[int64])
ct.keyValueResolver = func(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 64)
return i, err
}
ct.valueType = valType
ct.keyType = "int64"
return ct
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
ct := new(compositeMapType[uint32])
ct.keyValueResolver = func(s string) (uint32, error) {
i, err := strconv.ParseUint(s, 10, 32)
return uint32(i), err
}
ct.valueType = valType
ct.keyType = "uint32"
return ct
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
ct := new(compositeMapType[uint64])
ct.keyValueResolver = func(s string) (uint64, error) {
i, err := strconv.ParseUint(s, 10, 64)
return i, err
}
ct.valueType = valType
ct.keyType = "uint64"
return ct
case protoreflect.BoolKind:
ct := new(compositeMapType[bool])
ct.keyValueResolver = strconv.ParseBool
ct.valueType = valType
ct.keyType = "bool"
return ct
}
return nil

}
return nil
}

Expand All @@ -107,7 +172,6 @@ func (b *Builder) resolveFlagTypeBasic(field protoreflect.FieldDescriptor) Type
if flagType, ok := b.messageFlagTypes[field.Message().FullName()]; ok {
return flagType
}

JeancarloBarrios marked this conversation as resolved.
Show resolved Hide resolved
return jsonMessageFlagType{
messageDesc: field.Message(),
}
Expand Down
Loading