Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [#4601](https://github.com/ignite/cli/pull/4601) Add `appregistry` as default plugin
- [#4613](https://github.com/ignite/cli/pull/4613) Improve and simplify prompting logic by bubbletea.
- [#4615](https://github.com/ignite/cli/pull/4615) Fetch Ignite announcements from API.
- [#4624](https://github.com/ignite/cli/pull/4624) Fix autocli templates for variadics.

### Fixes

Expand Down
11 changes: 11 additions & 0 deletions ignite/templates/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ func (f Field) DataType() string {
return dt.DataType(f.Datatype)
}

// IsSlice returns true if the field is a slice.
// It assumes that a non indexable type is a slice.
func (f Field) IsSlice() bool {
dt, ok := datatype.IsSupportedType(f.DatatypeName)
if !ok {
panic(fmt.Sprintf("unknown type %s", f.DatatypeName))
}

return dt.NonIndex
}

// ProtoFieldName returns the field name used in proto.
func (f Field) ProtoFieldName() string {
return f.Name.Snake
Expand Down
13 changes: 10 additions & 3 deletions ignite/templates/field/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ func (f Fields) ProtoImports() []string {
return allImports
}

// ProtoFieldName returns all inline fields args for name used in proto.
func (f Fields) ProtoFieldName() string {
// ProtoFieldNameAutoCLI returns all inline fields args for name used in proto.
// It should be used in AutoCLI to generate the field name.
func (f Fields) ProtoFieldNameAutoCLI() string {
args := ""
for _, field := range f {
for i, field := range f {
// only the last field can be a variadic field
if i == len(f)-1 && field.IsSlice() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to add the coins only as the last argument? Will the message not support a two-coin slice?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct, only one slice argument is supported. If you need multiple you should create a manual command

Copy link
Collaborator

Choose a reason for hiding this comment

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

may we should throw a warning or create a manual command in case the user tries to scaffold the slice of coins:

ignite s list employee bonus:coins salary:coins

Copy link
Member Author

Choose a reason for hiding this comment

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

Creating a manual command for cases AutoCLI falls short would be a great feature yeah. I do not think we should have a warming however.
I can log an issue for that.

args += fmt.Sprintf(`{ProtoField: "%s", Varargs: true}, `, field.ProtoFieldName())
continue
}

args += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName())
}
args = strings.TrimSpace(args)
Expand Down
8 changes: 1 addition & 7 deletions ignite/templates/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"embed"
"fmt"
"path/filepath"
"strings"

"github.com/emicklei/proto"
"github.com/gobuffalo/genny/v2"
Expand Down Expand Up @@ -214,11 +213,6 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *Options) genny.RunFn
return err
}

var positionalArgs string
for _, field := range opts.Fields {
positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName())
}

template := `{
RpcMethod: "%[2]v",
Use: "%[3]v",
Expand All @@ -233,7 +227,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *Options) genny.RunFn
opts.MsgName.PascalCase,
fmt.Sprintf("%s %s", opts.MsgName.Kebab, opts.Fields.CLIUsage()),
opts.MsgName.Original,
strings.TrimSpace(positionalArgs),
opts.Fields.ProtoFieldNameAutoCLI(),
)

content := replacer.Replace(f.String(), typed.PlaceholderAutoCLITx, replacement)
Expand Down
8 changes: 1 addition & 7 deletions ignite/templates/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"embed"
"fmt"
"path/filepath"
"strings"

"github.com/emicklei/proto"
"github.com/gobuffalo/genny/v2"
Expand Down Expand Up @@ -159,11 +158,6 @@ func cliQueryModify(replacer placeholder.Replacer, opts *Options) genny.RunFn {
return err
}

var positionalArgs string
for _, field := range opts.ReqFields {
positionalArgs += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName())
}

template := `{
RpcMethod: "%[2]v",
Use: "%[3]v",
Expand All @@ -178,7 +172,7 @@ func cliQueryModify(replacer placeholder.Replacer, opts *Options) genny.RunFn {
opts.QueryName.PascalCase,
fmt.Sprintf("%s %s", opts.QueryName.Kebab, opts.ReqFields.CLIUsage()),
opts.Description,
strings.TrimSpace(positionalArgs),
opts.ReqFields.ProtoFieldNameAutoCLI(),
)
content := replacer.Replace(f.String(), PlaceholderAutoCLIQuery, replacement)

Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny
opts.TypeName.PascalCase,
opts.TypeName.Kebab,
opts.TypeName.Original,
opts.Fields.ProtoFieldName(),
opts.Fields.ProtoFieldNameAutoCLI(),
opts.Fields.CLIUsage(),
)

Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny

index := fmt.Sprintf(`{ProtoField: "%s"}, `, opts.Index.ProtoFieldName())
indexStr := fmt.Sprintf("[%s] ", opts.Index.ProtoFieldName())
positionalArgs := index + opts.Fields.ProtoFieldName()
positionalArgs := index + opts.Fields.ProtoFieldNameAutoCLI()
positionalArgsStr := indexStr + opts.Fields.CLIUsage()

template := `{
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/typed/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny
opts.TypeName.PascalCase,
opts.TypeName.Kebab,
opts.TypeName.Original,
opts.Fields.ProtoFieldName(),
opts.Fields.ProtoFieldNameAutoCLI(),
opts.Fields.CLIUsage(),
)

Expand Down