Skip to content

Commit

Permalink
revert field changes for manual commands
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 19, 2023
1 parent 50fa2fe commit 74715b0
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ignite/templates/field/datatype/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ var DataBool = DataType{
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: %t,\n", name.UpperCamel, value%2 == 0)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := cast.ToBoolE(args[%d])
if err != nil {
return err
}`,
prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return fmt.Sprintf(`%[1]vBytes := []byte{0}
if %[1]v {
Expand Down
16 changes: 14 additions & 2 deletions ignite/templates/field/datatype/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ var (
return fmt.Sprintf("cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]",
name, index)
},
GenesisArgs: func(multiformatname.Name, int) string { return "" },
GenesisArgs: func(multiformatname.Name, int) string { return "" },
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := sdk.ParseCoinNormalized(args[%d])
if err != nil {
return err
}`, prefix, name.UpperCamel, argIndex)
},
GoCLIImports: []GoImport{{Name: "github.com/cosmos/cosmos-sdk/types", Alias: "sdk"}},
ProtoImports: []string{"gogoproto/gogo.proto", "cosmos/base/v1beta1/coin.proto"},
NonIndex: true,
Expand All @@ -38,7 +44,13 @@ var (
return fmt.Sprintf("repeated cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]",
name, index)
},
GenesisArgs: func(multiformatname.Name, int) string { return "" },
GenesisArgs: func(multiformatname.Name, int) string { return "" },
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := sdk.ParseCoinsNormalized(args[%d])
if err != nil {
return err
}`, prefix, name.UpperCamel, argIndex)
},
GoCLIImports: []GoImport{{Name: "github.com/cosmos/cosmos-sdk/types", Alias: "sdk"}},
ProtoImports: []string{"gogoproto/gogo.proto", "cosmos/base/v1beta1/coin.proto"},
NonIndex: true,
Expand Down
7 changes: 7 additions & 0 deletions ignite/templates/field/datatype/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ var DataCustom = DataType{
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: new(types.%s),\n", name.UpperCamel, name.UpperCamel)
},
CLIArgs: func(name multiformatname.Name, datatype, prefix string, argIndex int) string {
return fmt.Sprintf(`%[1]v%[2]v := new(types.%[3]v)
err = json.Unmarshal([]byte(args[%[4]v]), %[1]v%[2]v)
if err != nil {
return err
}`, prefix, name.UpperCamel, datatype, argIndex)
},
ToProtoField: func(datatype, name string, index int) *proto.NormalField {
return protoutil.NewField(name, datatype, index)
},
Expand Down
18 changes: 18 additions & 0 deletions ignite/templates/field/datatype/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: %d,\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := cast.ToInt32E(args[%d])
if err != nil {
return err
}`,
prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return fmt.Sprintf(`%[1]vBytes := make([]byte, 4)
binary.BigEndian.PutUint32(%[1]vBytes, uint32(%[1]v))`, name)
Expand All @@ -46,6 +53,17 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: []int32{%d},\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%[1]vCast%[2]v := strings.Split(args[%[3]v], listSeparator)
%[1]v%[2]v := make([]int32, len(%[1]vCast%[2]v))
for i, arg := range %[1]vCast%[2]v {
value, err := cast.ToInt32E(arg)
if err != nil {
return err
}
%[1]v%[2]v[i] = value
}`, prefix, name.UpperCamel, argIndex)
},
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "int32", index, protoutil.Repeated())
},
Expand Down
7 changes: 7 additions & 0 deletions ignite/templates/field/datatype/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: \"%d\",\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf("%s%s := args[%d]", prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return fmt.Sprintf("%[1]vBytes := []byte(%[1]v)", name)
},
Expand All @@ -44,6 +47,10 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: []string{\"%d\"},\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%[1]v%[2]v := strings.Split(args[%[3]v], listSeparator)`,
prefix, name.UpperCamel, argIndex)
},
GoCLIImports: []GoImport{{Name: "strings"}},
NonIndex: true,
ToProtoField: func(_, name string, index int) *proto.NormalField {
Expand Down
1 change: 1 addition & 0 deletions ignite/templates/field/datatype/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type DataType struct {
ToBytes func(name string) string
ToString func(name string) string
ToProtoField func(datatype, name string, index int) *proto.NormalField
CLIArgs func(name multiformatname.Name, datatype, prefix string, argIndex int) string
NonIndex bool
}

Expand Down
19 changes: 19 additions & 0 deletions ignite/templates/field/datatype/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: %d,\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%s%s, err := cast.ToUint64E(args[%d])
if err != nil {
return err
}`,
prefix, name.UpperCamel, argIndex)
},
ToBytes: func(name string) string {
return fmt.Sprintf(`%[1]vBytes := make([]byte, 8)
binary.BigEndian.PutUint64(%[1]vBytes, %[1]v)`, name)
Expand All @@ -46,6 +53,18 @@ var (
GenesisArgs: func(name multiformatname.Name, value int) string {
return fmt.Sprintf("%s: []uint64{%d},\n", name.UpperCamel, value)
},
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
return fmt.Sprintf(`%[1]vCast%[2]v := strings.Split(args[%[3]v], listSeparator)
%[1]v%[2]v := make([]uint64, len(%[1]vCast%[2]v))
for i, arg := range %[1]vCast%[2]v {
value, err := cast.ToUint64E(arg)
if err != nil {
return err
}
%[1]v%[2]v[i] = value
}`,
prefix, name.UpperCamel, argIndex)
},
ToProtoField: func(_, name string, index int) *proto.NormalField {
return protoutil.NewField(name, "uint64", index, protoutil.Repeated())
},
Expand Down
9 changes: 9 additions & 0 deletions ignite/templates/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func (f Field) GenesisArgs(value int) string {
return dt.GenesisArgs(f.Name, value)
}

// CLIArgs returns the Datatype CLI args.
func (f Field) CLIArgs(prefix string, argIndex int) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}
return dt.CLIArgs(f.Name, f.Datatype, prefix, argIndex)
}

// ToBytes returns the Datatype byte array cast.
func (f Field) ToBytes(name string) string {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
Expand Down

0 comments on commit 74715b0

Please sign in to comment.