Skip to content

Commit

Permalink
chore: use fmt.Sprintf instead concatenating strings (#3801)
Browse files Browse the repository at this point in the history
* use fmt.Sprintf instead string add

* add missing sprintf to proto query

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani authored Dec 4, 2023
1 parent 69db6cc commit d6595bd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 54 deletions.
3 changes: 2 additions & 1 deletion ignite/pkg/gomodule/gomodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"strings"
"testing"

"github.com/ignite/cli/ignite/pkg/gomodule"
"github.com/stretchr/testify/require"

"github.com/ignite/cli/ignite/pkg/gomodule"
)

func TestSplitPath(t *testing.T) {
Expand Down
26 changes: 13 additions & 13 deletions ignite/services/scaffolder/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ func checkComponentValidity(appPath, moduleName string, compName multiformatname
func checkComponentCreated(appPath, moduleName string, compName multiformatname.Name, noMessage bool) (err error) {
// associate the type to check with the component that scaffold this type
typesToCheck := map[string]string{
compName.UpperCamel: componentType,
"queryall" + compName.LowerCase + "request": componentType,
"queryall" + compName.LowerCase + "response": componentType,
"queryget" + compName.LowerCase + "request": componentType,
"queryget" + compName.LowerCase + "response": componentType,
"query" + compName.LowerCase + "request": componentQuery,
"query" + compName.LowerCase + "response": componentQuery,
compName.LowerCase + "packetdata": componentPacket,
compName.UpperCamel: componentType,
fmt.Sprintf("queryall%srequest", compName.LowerCase): componentType,
fmt.Sprintf("queryall%sresponse", compName.LowerCase): componentType,
fmt.Sprintf("queryget%srequest", compName.LowerCase): componentType,
fmt.Sprintf("queryget%sresponse", compName.LowerCase): componentType,
fmt.Sprintf("query%srequest", compName.LowerCase): componentQuery,
fmt.Sprintf("query%sresponse", compName.LowerCase): componentQuery,
fmt.Sprintf("%spacketdata", compName.LowerCase): componentPacket,
}

if !noMessage {
typesToCheck["msgcreate"+compName.LowerCase] = componentType
typesToCheck["msgupdate"+compName.LowerCase] = componentType
typesToCheck["msgdelete"+compName.LowerCase] = componentType
typesToCheck["msg"+compName.LowerCase] = componentMessage
typesToCheck["msgsend"+compName.LowerCase] = componentPacket
typesToCheck[fmt.Sprintf("msgcreate%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msgupdate%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msgdelete%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msg%s", compName.LowerCase)] = componentMessage
typesToCheck[fmt.Sprintf("msgsend%s", compName.LowerCase)] = componentPacket
}

absPath, err := filepath.Abs(filepath.Join(appPath, "x", moduleName, "types"))
Expand Down
8 changes: 6 additions & 2 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func protoModify(opts *PacketOptions) genny.RunFn {
if err != nil {
return err
}
name := xstrings.Title(opts.ModuleName) + "PacketData"
name := fmt.Sprintf("%sPacketData", xstrings.Title(opts.ModuleName))
message, err := protoutil.GetMessageByName(protoFile, name)
if err != nil {
return fmt.Errorf("failed while looking up '%s' message in %s: %w", name, path, err)
Expand Down Expand Up @@ -305,7 +305,11 @@ func protoTxModify(opts *PacketOptions) genny.RunFn {
return fmt.Errorf("failed while looking up service 'Msg' in %s: %w", path, err)
}
typenameUpper := opts.PacketName.UpperCamel
send := protoutil.NewRPC("Send"+typenameUpper, "MsgSend"+typenameUpper, "MsgSend"+typenameUpper+"Response")
send := protoutil.NewRPC(
fmt.Sprintf("Send%s", typenameUpper),
fmt.Sprintf("MsgSend%s", typenameUpper),
fmt.Sprintf("MsgSend%sResponse", typenameUpper),
)
protoutil.Append(serviceMsg, send)

// Create fields for MsgSend.
Expand Down
9 changes: 8 additions & 1 deletion ignite/templates/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ func protoTxRPCModify(opts *Options) genny.RunFn {
return fmt.Errorf("failed while looking up service 'Msg' in %s: %w", path, err)
}
typenameUpper := opts.MsgName.UpperCamel
protoutil.Append(serviceMsg, protoutil.NewRPC(typenameUpper, "Msg"+typenameUpper, "Msg"+typenameUpper+"Response"))
protoutil.Append(
serviceMsg,
protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("Msg%s", typenameUpper),
fmt.Sprintf("Msg%sResponse", typenameUpper),
),
)

newFile := genny.NewFileS(path, protoutil.Print(protoFile))
return r.File(newFile)
Expand Down
5 changes: 4 additions & 1 deletion ignite/templates/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func protoQueryModify(opts *Options) genny.RunFn {
}

typenameUpper, appModulePath := opts.QueryName.UpperCamel, gomodulepath.ExtractAppPath(opts.ModulePath)
rpcSingle := protoutil.NewRPC(typenameUpper, "Query"+typenameUpper+"Request", "Query"+typenameUpper+"Response",
rpcSingle := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("Query%sRequest", typenameUpper),
fmt.Sprintf("Query%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand Down
50 changes: 35 additions & 15 deletions ignite/templates/typed/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,21 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
// Create, update, delete rpcs. Better to append them altogether, single traversal.
typenameUpper := opts.TypeName.UpperCamel
protoutil.Append(serviceMsg,
protoutil.NewRPC("Create"+typenameUpper, "MsgCreate"+typenameUpper, "MsgCreate"+typenameUpper+"Response"),
protoutil.NewRPC("Update"+typenameUpper, "MsgUpdate"+typenameUpper, "MsgUpdate"+typenameUpper+"Response"),
protoutil.NewRPC("Delete"+typenameUpper, "MsgDelete"+typenameUpper, "MsgDelete"+typenameUpper+"Response"),
protoutil.NewRPC(
fmt.Sprintf("Create%s", typenameUpper),
fmt.Sprintf("MsgCreate%s", typenameUpper),
fmt.Sprintf("MsgCreate%sResponse", typenameUpper),
),
protoutil.NewRPC(
fmt.Sprintf("Update%s", typenameUpper),
fmt.Sprintf("MsgUpdate%s", typenameUpper),
fmt.Sprintf("MsgUpdate%sResponse", typenameUpper),
),
protoutil.NewRPC(
fmt.Sprintf("Delete%s", typenameUpper),
fmt.Sprintf("MsgDelete%s", typenameUpper),
fmt.Sprintf("MsgDelete%sResponse", typenameUpper),
),
)

// - Ensure custom types are imported
Expand Down Expand Up @@ -142,26 +154,26 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
}

msgCreate := protoutil.NewMessage(
"MsgCreate"+typenameUpper,
fmt.Sprintf("MsgCreate%s", typenameUpper),
protoutil.WithFields(createFields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgCreateResponse := protoutil.NewMessage(
"MsgCreate"+typenameUpper+"Response",
fmt.Sprintf("MsgCreate%sResponse", typenameUpper),
protoutil.WithFields(protoutil.NewField("id", "uint64", 1)),
)
msgUpdate := protoutil.NewMessage(
"MsgUpdate"+typenameUpper,
fmt.Sprintf("MsgUpdate%s", typenameUpper),
protoutil.WithFields(updateFields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgUpdateResponse := protoutil.NewMessage("MsgUpdate" + typenameUpper + "Response")
msgUpdateResponse := protoutil.NewMessage(fmt.Sprintf("MsgUpdate%sResponse", typenameUpper))
msgDelete := protoutil.NewMessage(
"MsgDelete"+typenameUpper,
fmt.Sprintf("MsgDelete%s", typenameUpper),
protoutil.WithFields(udfields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgDeleteResponse := protoutil.NewMessage("MsgDelete" + typenameUpper + "Response")
msgDeleteResponse := protoutil.NewMessage(fmt.Sprintf("MsgDelete%sResponse", typenameUpper))
protoutil.Append(
protoFile,
msgCreate,
Expand Down Expand Up @@ -205,7 +217,10 @@ func protoQueryModify(opts *typed.Options) genny.RunFn {
}
appModulePath := gomodulepath.ExtractAppPath(opts.ModulePath)
typenameUpper := opts.TypeName.UpperCamel
rpcQueryGet := protoutil.NewRPC(typenameUpper, "QueryGet"+typenameUpper+"Request", "QueryGet"+typenameUpper+"Response",
rpcQueryGet := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand All @@ -220,7 +235,10 @@ func protoQueryModify(opts *typed.Options) genny.RunFn {
)
protoutil.AttachComment(rpcQueryGet, fmt.Sprintf("Queries a %v by id.", typenameUpper))

rpcQueryAll := protoutil.NewRPC(typenameUpper+"All", "QueryAll"+typenameUpper+"Request", "QueryAll"+typenameUpper+"Response",
rpcQueryAll := protoutil.NewRPC(
fmt.Sprintf("%sAll", typenameUpper),
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand All @@ -241,19 +259,21 @@ func protoQueryModify(opts *typed.Options) genny.RunFn {
gogoOption := protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())

queryGetRequest := protoutil.NewMessage(
"QueryGet"+typenameUpper+"Request",
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
protoutil.WithFields(protoutil.NewField("id", "uint64", 1)),
)
field := protoutil.NewField(typenameUpper, typenameUpper, 1, protoutil.WithFieldOptions(gogoOption))
queryGetResponse := protoutil.NewMessage("QueryGet"+typenameUpper+"Response", protoutil.WithFields(field))
queryGetResponse := protoutil.NewMessage(
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithFields(field))

queryAllRequest := protoutil.NewMessage(
"QueryAll"+typenameUpper+"Request",
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
protoutil.WithFields(protoutil.NewField(paginationName, paginationType+"Request", 1)),
)
field = protoutil.NewField(typenameUpper, typenameUpper, 1, protoutil.Repeated(), protoutil.WithFieldOptions(gogoOption))
queryAllResponse := protoutil.NewMessage(
"QueryAll"+typenameUpper+"Response",
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithFields(field, protoutil.NewField(paginationName, paginationType+"Response", 2)),
)
protoutil.Append(protoFile, queryGetRequest, queryGetResponse, queryAllRequest, queryAllResponse)
Expand Down
44 changes: 31 additions & 13 deletions ignite/templates/typed/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
return fmt.Errorf("failed while looking up service 'Query' in %s: %w", path, err)
}
typenameUpper, typenameSnake, typenameLower := opts.TypeName.UpperCamel, opts.TypeName.Snake, opts.TypeName.LowerCamel
rpcQueryGet := protoutil.NewRPC(typenameUpper, "QueryGet"+typenameUpper+"Request", "QueryGet"+typenameUpper+"Response",
rpcQueryGet := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand All @@ -164,7 +167,10 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
)
protoutil.AttachComment(rpcQueryGet, fmt.Sprintf("Queries a %v by index.", typenameUpper))

rpcQueryAll := protoutil.NewRPC(typenameUpper+"All", "QueryAll"+typenameUpper+"Request", "QueryAll"+typenameUpper+"Response",
rpcQueryAll := protoutil.NewRPC(
fmt.Sprintf("%sAll", typenameUpper),
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand Down Expand Up @@ -202,20 +208,20 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
}
paginationType, paginationName := "cosmos.base.query.v1beta1.Page", "pagination"
queryGetRequest := protoutil.NewMessage(
"QueryGet"+typenameUpper+"Request",
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
protoutil.WithFields(queryIndexFields...),
)
gogoOption := protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())
queryGetResponse := protoutil.NewMessage(
"QueryGet"+typenameUpper+"Response",
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithFields(protoutil.NewField(typenameLower, typenameUpper, 1, protoutil.WithFieldOptions(gogoOption))),
)
queryAllRequest := protoutil.NewMessage(
"QueryAll"+typenameUpper+"Request",
fmt.Sprintf("QueryAll%sRequest", typenameUpper),
protoutil.WithFields(protoutil.NewField(paginationName, paginationType+"Request", 1)),
)
queryAllResponse := protoutil.NewMessage(
"QueryAll"+typenameUpper+"Response",
fmt.Sprintf("QueryAll%sResponse", typenameUpper),
protoutil.WithFields(
protoutil.NewField(
typenameLower,
Expand All @@ -224,7 +230,7 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
protoutil.Repeated(),
protoutil.WithFieldOptions(gogoOption),
),
protoutil.NewField(paginationName, paginationType+"Response", 2),
protoutil.NewField(paginationName, fmt.Sprintf("%sResponse", paginationType), 2),
),
)
protoutil.Append(protoFile, queryGetRequest, queryGetResponse, queryAllRequest, queryAllResponse)
Expand Down Expand Up @@ -537,9 +543,21 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
// better to append them altogether, single traversal.
typenameUpper := opts.TypeName.UpperCamel
protoutil.Append(serviceMsg,
protoutil.NewRPC("Create"+typenameUpper, "MsgCreate"+typenameUpper, "MsgCreate"+typenameUpper+"Response"),
protoutil.NewRPC("Update"+typenameUpper, "MsgUpdate"+typenameUpper, "MsgUpdate"+typenameUpper+"Response"),
protoutil.NewRPC("Delete"+typenameUpper, "MsgDelete"+typenameUpper, "MsgDelete"+typenameUpper+"Response"),
protoutil.NewRPC(
fmt.Sprintf("Create%s", typenameUpper),
fmt.Sprintf("MsgCreate%s", typenameUpper),
fmt.Sprintf("MsgCreate%sResponse", typenameUpper),
),
protoutil.NewRPC(
fmt.Sprintf("Update%s", typenameUpper),
fmt.Sprintf("MsgUpdate%s", typenameUpper),
fmt.Sprintf("MsgUpdate%sResponse", typenameUpper),
),
protoutil.NewRPC(
fmt.Sprintf("Delete%s", typenameUpper),
fmt.Sprintf("MsgDelete%s", typenameUpper),
fmt.Sprintf("MsgDelete%sResponse", typenameUpper),
),
)

// Messages
Expand Down Expand Up @@ -577,21 +595,21 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
protoutil.WithFields(append(commonFields, fields...)...),
protoutil.WithMessageOptions(creatorOpt),
)
msgCreateResponse := protoutil.NewMessage("MsgCreate" + typenameUpper + "Response")
msgCreateResponse := protoutil.NewMessage(fmt.Sprintf("MsgCreate%sResponse", typenameUpper))

msgUpdate := protoutil.NewMessage(
"MsgUpdate"+typenameUpper,
protoutil.WithFields(append(commonFields, fields...)...),
protoutil.WithMessageOptions(creatorOpt),
)
msgUpdateResponse := protoutil.NewMessage("MsgUpdate" + typenameUpper + "Response")
msgUpdateResponse := protoutil.NewMessage(fmt.Sprintf("MsgUpdate%sResponse", typenameUpper))

msgDelete := protoutil.NewMessage(
"MsgDelete"+typenameUpper,
protoutil.WithFields(commonFields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgDeleteResponse := protoutil.NewMessage("MsgDelete" + typenameUpper + "Response")
msgDeleteResponse := protoutil.NewMessage(fmt.Sprintf("MsgDelete%sResponse", typenameUpper))
protoutil.Append(protoFile,
msgCreate, msgCreateResponse, msgUpdate, msgUpdateResponse, msgDelete, msgDeleteResponse,
)
Expand Down
31 changes: 23 additions & 8 deletions ignite/templates/typed/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
}
appModulePath := gomodulepath.ExtractAppPath(opts.ModulePath)
typenameUpper := opts.TypeName.UpperCamel
rpcQueryGet := protoutil.NewRPC(typenameUpper, "QueryGet"+typenameUpper+"Request", "QueryGet"+typenameUpper+"Response",
rpcQueryGet := protoutil.NewRPC(
typenameUpper,
fmt.Sprintf("QueryGet%sRequest", typenameUpper),
fmt.Sprintf("QueryGet%sResponse", typenameUpper),
protoutil.WithRPCOptions(
protoutil.NewOption(
"google.api.http",
Expand All @@ -148,7 +151,7 @@ func protoRPCModify(opts *typed.Options) genny.RunFn {
field := protoutil.NewField(typenameUpper, typenameUpper, 1,
protoutil.WithFieldOptions(protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())),
)
queryGetResponse := protoutil.NewMessage("QueryGet"+typenameUpper+"Response", protoutil.WithFields(field))
queryGetResponse := protoutil.NewMessage(fmt.Sprintf("QueryGet%sResponse", typenameUpper), protoutil.WithFields(field))
protoutil.Append(protoFile, queryGetRequest, queryGetResponse)

newFile := genny.NewFileS(path, protoutil.Print(protoFile))
Expand Down Expand Up @@ -383,9 +386,21 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
// Append create, update, delete rpcs. Better to append them altogether, single traversal.
name := opts.TypeName.UpperCamel
protoutil.Append(serviceMsg,
protoutil.NewRPC("Create"+name, "MsgCreate"+name, "MsgCreate"+name+"Response"),
protoutil.NewRPC("Update"+name, "MsgUpdate"+name, "MsgUpdate"+name+"Response"),
protoutil.NewRPC("Delete"+name, "MsgDelete"+name, "MsgDelete"+name+"Response"),
protoutil.NewRPC(
fmt.Sprintf("Create%s", name),
fmt.Sprintf("MsgCreate%s", name),
fmt.Sprintf("MsgCreate%sResponse", name),
),
protoutil.NewRPC(
fmt.Sprintf("Update%s", name),
fmt.Sprintf("MsgUpdate%s", name),
fmt.Sprintf("MsgUpdate%sResponse", name),
),
protoutil.NewRPC(
fmt.Sprintf("Delete%s", name),
fmt.Sprintf("MsgDelete%s", name),
fmt.Sprintf("MsgDelete%sResponse", name),
),
)

// Ensure custom types are imported
Expand Down Expand Up @@ -415,19 +430,19 @@ func protoTxModify(opts *typed.Options) genny.RunFn {
protoutil.WithFields(fields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgCreateResponse := protoutil.NewMessage("MsgCreate" + name + "Response")
msgCreateResponse := protoutil.NewMessage(fmt.Sprintf("MsgCreate%sResponse", name))
msgUpdate := protoutil.NewMessage(
"MsgUpdate"+name,
protoutil.WithFields(fields...),
protoutil.WithMessageOptions(creatorOpt),
)
msgUpdateResponse := protoutil.NewMessage("MsgUpdate" + name + "Response")
msgUpdateResponse := protoutil.NewMessage(fmt.Sprintf("MsgUpdate%sResponse", name))
msgDelete := protoutil.NewMessage(
"MsgDelete"+name,
protoutil.WithFields(creator),
protoutil.WithMessageOptions(creatorOpt),
)
msgDeleteResponse := protoutil.NewMessage("MsgDelete" + name + "Response")
msgDeleteResponse := protoutil.NewMessage(fmt.Sprintf("MsgDelete%sResponse", name))
protoutil.Append(protoFile,
msgCreate, msgCreateResponse, msgUpdate, msgUpdateResponse, msgDelete, msgDeleteResponse,
)
Expand Down

0 comments on commit d6595bd

Please sign in to comment.