Skip to content

Commit e7e4b92

Browse files
authored
fix: fix buf lint issues into the chain code (#4555)
* use snake case for proto variable names * use paramField.ProtoFieldName method * use default method for cli usage * use the fields method insead create the strings everytime * remove unused `tx.proto` import * fix tx packet cli usage * add some proto comments * add more comments to proto files * fix typo * disable some lint rules * fix some buf lint issues * fix some comments and typo * fix typo issue * add changelog * params proto field positons * add comment for NoData message * xstrcase pkg * always use xstrcase.UpperCamel * fix pascoal case for proto messages and rpc * fix pascalcase for methods * add pascal case to type_test.go
1 parent a056e1d commit e7e4b92

File tree

60 files changed

+828
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+828
-568
lines changed

buf.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ lint:
88
- FILE_LOWER_SNAKE_CASE
99
except:
1010
- COMMENT_FIELD
11-
- FIELD_NOT_REQUIRED
12-
- PACKAGE_NO_IMPORT_CYCLE
13-
- PACKAGE_VERSION_SUFFIX
1411
- RPC_REQUEST_STANDARD_NAME
12+
- RPC_RESPONSE_STANDARD_NAME
1513
- SERVICE_SUFFIX
16-
- UNARY_RPC
1714
disallow_comment_ignores: true
1815
breaking:
1916
use:

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
- [#4128](https://github.com/ignite/cli/pull/4128) Check for duplicate proto fields in config
6868
- [#4402](https://github.com/ignite/cli/pull/4402) Fix gentx parser into the cosmosutil package
6969
- [#4552](https://github.com/ignite/cli/pull/4552) Avoid direct access to proto field `perms.Account` and `perms.Permissions`
70+
- [#4555](https://github.com/ignite/cli/pull/4555) Fix buf lint issues into the chain code
7071

7172
## [`v28.8.1`](https://github.com/ignite/cli/releases/tag/v28.8.1)
7273

ignite/pkg/cosmosgen/template.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/iancoleman/strcase"
1111
"golang.org/x/text/cases"
1212
"golang.org/x/text/language"
13+
14+
"github.com/ignite/cli/v29/ignite/pkg/xstrcase"
1315
)
1416

1517
var (
@@ -52,7 +54,7 @@ func (t templateWriter) Write(destDir, protoPath string, data interface{}) error
5254
"camelCase": strcase.ToLowerCamel,
5355
"capitalCase": func(word string) string {
5456
replacer := strings.NewReplacer("-", "_", ".", "_")
55-
word = strcase.ToCamel(replacer.Replace(word))
57+
word = xstrcase.UpperCamel(replacer.Replace(word))
5658

5759
return cases.Title(language.English).String(word)
5860
},
@@ -64,7 +66,7 @@ func (t templateWriter) Write(destDir, protoPath string, data interface{}) error
6466
"camelCaseUpperSta": func(word string) string {
6567
replacer := strings.NewReplacer("-", "_", ".", "_")
6668

67-
return strcase.ToCamel(replacer.Replace(word))
69+
return xstrcase.UpperCamel(replacer.Replace(word))
6870
},
6971
"resolveFile": func(fullPath string) string { // TODO to check
7072
rel, _ := filepath.Rel(protoPath, fullPath)

ignite/pkg/multiformatname/multiformatname.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
package multiformatname
33

44
import (
5-
"strings"
6-
75
"github.com/iancoleman/strcase"
86

97
"github.com/ignite/cli/v29/ignite/pkg/errors"
10-
"github.com/ignite/cli/v29/ignite/pkg/xstrings"
8+
"github.com/ignite/cli/v29/ignite/pkg/xstrcase"
119
)
1210

1311
// Name represents a name with multiple naming convention representations.
@@ -16,6 +14,7 @@ type Name struct {
1614
Original string
1715
LowerCamel string
1816
UpperCamel string
17+
PascalCase string
1918
LowerCase string
2019
UpperCase string
2120
Kebab string
@@ -37,11 +36,12 @@ func NewName(name string, additionalChecks ...Checker) (Name, error) {
3736
return Name{
3837
Original: name,
3938
LowerCamel: strcase.ToLowerCamel(name),
40-
UpperCamel: strcase.ToCamel(name),
41-
UpperCase: strings.ToUpper(name),
39+
UpperCamel: xstrcase.UpperCamel(name),
40+
PascalCase: strcase.ToCamel(name),
41+
LowerCase: xstrcase.Lowercase(name),
42+
UpperCase: xstrcase.Uppercase(name),
4243
Kebab: strcase.ToKebab(name),
4344
Snake: strcase.ToSnake(name),
44-
LowerCase: lowercase(name),
4545
}, nil
4646
}
4747

@@ -79,14 +79,3 @@ func basicCheckName(name string) error {
7979

8080
return nil
8181
}
82-
83-
// lowercase returns the name with lower case and no special character.
84-
func lowercase(name string) string {
85-
return strings.ToLower(
86-
strings.ReplaceAll(
87-
xstrings.NoDash(name),
88-
"_",
89-
"",
90-
),
91-
)
92-
}

ignite/pkg/multiformatname/multiformatname_test.go

+171-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package multiformatname_test
22

33
import (
4-
"fmt"
54
"testing"
65

76
"github.com/stretchr/testify/require"
@@ -10,59 +9,178 @@ import (
109
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
1110
)
1211

13-
func TestNewMultiFormatName(t *testing.T) {
14-
// [valueToTest, lowerCamel, upperCamel, kebabCase]
15-
cases := [][6]string{
16-
{"foo", "foo", "Foo", "foo", "foo", "foo"},
17-
{"fooBar", "fooBar", "FooBar", "foo-bar", "foo_bar", "foobar"},
18-
{"foo-bar", "fooBar", "FooBar", "foo-bar", "foo_bar", "foobar"},
19-
{"foo_bar", "fooBar", "FooBar", "foo-bar", "foo_bar", "foobar"},
20-
{"foo_barFoobar", "fooBarFoobar", "FooBarFoobar", "foo-bar-foobar", "foo_bar_foobar", "foobarfoobar"},
21-
{"foo_-_bar", "fooBar", "FooBar", "foo---bar", "foo___bar", "foobar"},
22-
{"foo_-_Bar", "fooBar", "FooBar", "foo---bar", "foo___bar", "foobar"},
23-
{"fooBAR", "fooBar", "FooBar", "foo-bar", "foo_bar", "foobar"},
24-
{"fooBar123", "fooBar123", "FooBar123", "foo-bar-123", "foo_bar_123", "foobar123"},
12+
func TestNewName(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
arg string
16+
want multiformatname.Name
17+
err error
18+
}{
19+
{
20+
name: "simple lowercase name",
21+
arg: "foo",
22+
want: multiformatname.Name{
23+
Original: "foo",
24+
LowerCamel: "foo",
25+
UpperCamel: "Foo",
26+
PascalCase: "Foo",
27+
LowerCase: "foo",
28+
UpperCase: "FOO",
29+
Kebab: "foo",
30+
Snake: "foo",
31+
},
32+
},
33+
{
34+
name: "camelCase name",
35+
arg: "fooBar",
36+
want: multiformatname.Name{
37+
Original: "fooBar",
38+
LowerCamel: "fooBar",
39+
UpperCamel: "FooBar",
40+
PascalCase: "FooBar",
41+
LowerCase: "foobar",
42+
UpperCase: "FOOBAR",
43+
Kebab: "foo-bar",
44+
Snake: "foo_bar",
45+
},
46+
},
47+
{
48+
name: "kebab-case name",
49+
arg: "foo-bar",
50+
want: multiformatname.Name{
51+
Original: "foo-bar",
52+
LowerCamel: "fooBar",
53+
UpperCamel: "FooBar",
54+
PascalCase: "FooBar",
55+
LowerCase: "foobar",
56+
UpperCase: "FOOBAR",
57+
Kebab: "foo-bar",
58+
Snake: "foo_bar",
59+
},
60+
},
61+
{
62+
name: "snake_case name",
63+
arg: "foo_bar",
64+
want: multiformatname.Name{
65+
Original: "foo_bar",
66+
LowerCamel: "fooBar",
67+
UpperCamel: "FooBar",
68+
PascalCase: "FooBar",
69+
LowerCase: "foobar",
70+
UpperCase: "FOOBAR",
71+
Kebab: "foo-bar",
72+
Snake: "foo_bar",
73+
},
74+
},
75+
{
76+
name: "mixed snake_case and camelCase name",
77+
arg: "foo_barFoobar",
78+
want: multiformatname.Name{
79+
Original: "foo_barFoobar",
80+
LowerCamel: "fooBarFoobar",
81+
UpperCamel: "FooBarFoobar",
82+
PascalCase: "FooBarFoobar",
83+
LowerCase: "foobarfoobar",
84+
UpperCase: "FOOBARFOOBAR",
85+
Kebab: "foo-bar-foobar",
86+
Snake: "foo_bar_foobar",
87+
},
88+
},
89+
{
90+
name: "mixed underscores and dashes",
91+
arg: "foo_-_bar",
92+
want: multiformatname.Name{
93+
Original: "foo_-_bar",
94+
LowerCamel: "fooBar",
95+
UpperCamel: "Foo__Bar",
96+
PascalCase: "FooBar",
97+
LowerCase: "foobar",
98+
UpperCase: "FOOBAR",
99+
Kebab: "foo---bar",
100+
Snake: "foo___bar",
101+
},
102+
},
103+
{
104+
name: "mixed underscores, dashes, and numbers",
105+
arg: "foo_-_Bar1",
106+
want: multiformatname.Name{
107+
Original: "foo_-_Bar1",
108+
LowerCamel: "fooBar1",
109+
UpperCamel: "Foo__Bar_1",
110+
PascalCase: "FooBar1",
111+
LowerCase: "foobar1",
112+
UpperCase: "FOOBAR1",
113+
Kebab: "foo---bar-1",
114+
Snake: "foo___bar_1",
115+
},
116+
},
117+
{
118+
name: "uppercase variant in simple name",
119+
arg: "fooBAR",
120+
want: multiformatname.Name{
121+
Original: "fooBAR",
122+
LowerCamel: "fooBar",
123+
UpperCamel: "FooBar",
124+
PascalCase: "FooBar",
125+
LowerCase: "foobar",
126+
UpperCase: "FOOBAR",
127+
Kebab: "foo-bar",
128+
Snake: "foo_bar",
129+
},
130+
},
131+
{
132+
name: "uppercase variant with starting capital",
133+
arg: "FooBAR",
134+
want: multiformatname.Name{
135+
Original: "FooBAR",
136+
LowerCamel: "fooBar",
137+
UpperCamel: "FooBar",
138+
PascalCase: "FooBar",
139+
LowerCase: "foobar",
140+
UpperCase: "FOOBAR",
141+
Kebab: "foo-bar",
142+
Snake: "foo_bar",
143+
},
144+
},
145+
{
146+
name: "camelCase name with numbers",
147+
arg: "fooBar123",
148+
want: multiformatname.Name{
149+
Original: "fooBar123",
150+
LowerCamel: "fooBar123",
151+
UpperCamel: "FooBar_123",
152+
PascalCase: "FooBar123",
153+
LowerCase: "foobar123",
154+
UpperCase: "FOOBAR123",
155+
Kebab: "foo-bar-123",
156+
Snake: "foo_bar_123",
157+
},
158+
},
159+
{
160+
name: "multiple numbers in name",
161+
arg: "para_2_m_s_43_tr_1",
162+
want: multiformatname.Name{
163+
Original: "para_2_m_s_43_tr_1",
164+
LowerCamel: "para2MS43Tr1",
165+
UpperCamel: "Para_2MS_43Tr_1",
166+
PascalCase: "Para2MS43Tr1",
167+
LowerCase: "para2ms43tr1",
168+
UpperCase: "PARA2MS43TR1",
169+
Kebab: "para-2-m-s-43-tr-1",
170+
Snake: "para_2_m_s_43_tr_1",
171+
},
172+
},
25173
}
26-
27-
// test cases
28-
for _, testCase := range cases {
29-
name, err := multiformatname.NewName(testCase[0])
30-
require.NoError(t, err)
31-
require.Equal(
32-
t,
33-
testCase[0],
34-
name.Original,
35-
)
36-
require.Equal(
37-
t,
38-
testCase[1],
39-
name.LowerCamel,
40-
fmt.Sprintf("%s should be converted to the correct lower camel format", testCase[0]),
41-
)
42-
require.Equal(
43-
t,
44-
testCase[2],
45-
name.UpperCamel,
46-
fmt.Sprintf("%s should be converted the correct upper camel format", testCase[0]),
47-
)
48-
require.Equal(
49-
t,
50-
testCase[3],
51-
name.Kebab,
52-
fmt.Sprintf("%s should be converted the correct kebab format", testCase[0]),
53-
)
54-
require.Equal(
55-
t,
56-
testCase[4],
57-
name.Snake,
58-
fmt.Sprintf("%s should be converted the correct snake format", testCase[0]),
59-
)
60-
require.Equal(
61-
t,
62-
testCase[5],
63-
name.LowerCase,
64-
fmt.Sprintf("%s should be converted the correct lowercase format", testCase[0]),
65-
)
174+
for _, tt := range tests {
175+
t.Run(tt.name, func(t *testing.T) {
176+
got, err := multiformatname.NewName(tt.arg)
177+
if tt.err != nil {
178+
require.ErrorIs(t, err, tt.err)
179+
return
180+
}
181+
require.NoError(t, err)
182+
require.EqualValues(t, tt.want, got)
183+
})
66184
}
67185
}
68186

ignite/pkg/protoanalysis/protoutil/creator.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func NewRPC(name, inputType, outputType string, opts ...RPCSpecOptions) *proto.R
206206

207207
rpc := &proto.RPC{
208208
Name: r.name,
209+
Comment: defaultComment(r.name, "RPC"),
209210
RequestType: r.inputType,
210211
ReturnsType: r.outputType,
211212
StreamsRequest: r.streamsReq,
@@ -266,7 +267,8 @@ func NewService(name string, opts ...ServiceSpecOptions) *proto.Service {
266267
opt(&s)
267268
}
268269
service := &proto.Service{
269-
Name: s.name,
270+
Name: s.name,
271+
Comment: defaultComment(s.name, "service"),
270272
}
271273
for _, opt := range s.opts {
272274
service.Elements = append(service.Elements, opt)
@@ -415,6 +417,7 @@ func NewMessage(name string, opts ...MessageSpecOptions) *proto.Message {
415417
}
416418
message := &proto.Message{
417419
Name: m.name,
420+
Comment: defaultComment(name, "message"),
418421
IsExtend: m.isExtend,
419422
}
420423
for _, opt := range m.options {
@@ -521,7 +524,8 @@ func NewEnum(name string, opts ...EnumSpecOpts) *proto.Enum {
521524
opt(&e)
522525
}
523526
enum := &proto.Enum{
524-
Name: e.name,
527+
Name: e.name,
528+
Comment: defaultComment(name, "enum"),
525529
}
526530
for _, opt := range e.options {
527531
enum.Elements = append(enum.Elements, opt)
@@ -654,3 +658,13 @@ func isString(s string) bool {
654658
}
655659
return true
656660
}
661+
662+
// defaultComment creates a new default proto comment with name and type.
663+
func defaultComment(name, protoType string) *proto.Comment {
664+
return newComment(fmt.Sprintf(" %[1]v defines the %[1]v %[2]v.", name, protoType))
665+
}
666+
667+
// newComment creates a new proto comment.
668+
func newComment(text string) *proto.Comment {
669+
return &proto.Comment{Lines: []string{text}}
670+
}

ignite/pkg/protoanalysis/protoutil/proto_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestAddEmptyList_genesis(t *testing.T) {
176176
Apply(f, nil, func(c *Cursor) bool {
177177
if m, ok := c.Node().(*proto.Message); ok {
178178
if m.Name == "GenesisState" {
179-
lst := NewField(typename+"List", typename, 2,
179+
lst := NewField(typename+"_list", typename, 2,
180180
WithFieldOptions(NewOption("gogoproto.nullable", "false", Custom())),
181181
Repeated(),
182182
)

0 commit comments

Comments
 (0)