mukan-ignite/ignite/templates/field/fields.go
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

85 lines
2.2 KiB
Go

package field
import (
"fmt"
"strings"
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
)
// Fields represents a Field slice.
type Fields []Field
// GoCLIImports returns all go CLI imports.
func (f Fields) GoCLIImports() []datatype.GoImport {
allImports := make([]datatype.GoImport, 0)
exist := make(map[string]struct{})
for _, fields := range f {
for _, goImport := range fields.GoCLIImports() {
if _, ok := exist[goImport.Name]; ok {
continue
}
exist[goImport.Name] = struct{}{}
allImports = append(allImports, goImport)
}
}
return allImports
}
// ProtoImports returns all proto imports.
func (f Fields) ProtoImports() []string {
allImports := make([]string, 0)
exist := make(map[string]struct{})
for _, fields := range f {
for _, protoImport := range fields.ProtoImports() {
if _, ok := exist[protoImport]; ok {
continue
}
exist[protoImport] = struct{}{}
allImports = append(allImports, protoImport)
}
}
return allImports
}
// 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 i, field := range f {
// only the last field can be a variadic field
if i == len(f)-1 && field.IsSlice() {
args += fmt.Sprintf(`{ProtoField: "%s", Varargs: true}, `, field.ProtoFieldName())
continue
}
args += fmt.Sprintf(`{ProtoField: "%s"}, `, field.ProtoFieldName())
}
args = strings.TrimSpace(args)
return strings.Trim(args, ",")
}
// CLIUsage returns all inline fields args for CLI command usage.
func (f Fields) CLIUsage() string {
args := ""
for _, field := range f {
args += fmt.Sprintf(" [%s]", field.CLIUsage())
}
return strings.TrimSpace(args)
}
// Custom returns a list of custom fields.
func (f Fields) Custom() []string {
fields := make([]string, 0)
for _, field := range f {
if field.DatatypeName == datatype.Custom || field.DatatypeName == datatype.CustomSlice {
dataType, err := multiformatname.NewName(field.Datatype)
if err != nil {
panic(err)
}
fields = append(fields, dataType.Snake)
}
}
return fields
}