Skip to content

Commit ac6d278

Browse files
authored
refactor(ecode): add codegen and improve testability (#312)
* refactor: add codegen for ecode * refactor: deprecate GeneratedBuf * refactor: use error.Is to check ecode equality * fix: caller stack levels * fix: imports of maps and slices packages * fix: lint errors * fix: coverage * refactor: i18n APIs * fix: duplicate verb +v print * test: add more cases * feat: check extracted fields names
1 parent 720a4cc commit ac6d278

40 files changed

+1439
-820
lines changed

codecov.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ coverage:
55
target: 63%
66
threshold: 1%
77
ignore:
8-
- "**/*.pb.go" # ignore all *.pb.go files
9-
- "test" # ignore folders and all its contents
8+
- "**/*.pb.go" # ignore all *.pb.go files
9+
- "**/*_generated.go"
10+
- "test" # ignore folders and all its contents
11+
- "internal/tools"

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require (
1212
github.com/stretchr/testify v1.9.0
1313
github.com/subchen/go-xmldom v1.1.2
1414
github.com/xuri/excelize/v2 v2.9.0
15-
go.uber.org/multierr v1.11.0
1615
go.uber.org/zap v1.24.0
16+
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
1717
golang.org/x/sync v0.8.0
1818
golang.org/x/text v0.19.0
1919
google.golang.org/protobuf v1.34.2
@@ -40,6 +40,7 @@ require (
4040
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
4141
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
4242
go.uber.org/atomic v1.7.0 // indirect
43+
go.uber.org/multierr v1.11.0 // indirect
4344
golang.org/x/arch v0.14.0 // indirect
4445
golang.org/x/crypto v0.28.0 // indirect
4546
golang.org/x/net v0.30.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
104104
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
105105
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
106106
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
107+
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
108+
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
107109
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
108110
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
109111
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=

internal/confgen/document_parser_test.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDocParser_parseFieldNotFound(t *testing.T) {
3131
parser *sheetParser
3232
args args
3333
wantErr bool
34-
errcode string
34+
err error
3535
}{
3636
{
3737
name: "no duplicate key",
@@ -120,7 +120,7 @@ func TestDocParser_parseFieldNotFound(t *testing.T) {
120120
},
121121
},
122122
wantErr: true,
123-
errcode: "E2014",
123+
err: xerrors.ErrE2014,
124124
},
125125
}
126126
for _, tt := range tests {
@@ -130,10 +130,7 @@ func TestDocParser_parseFieldNotFound(t *testing.T) {
130130
t.Errorf("sheetParser.Parse() error = %v, wantErr %v", err, tt.wantErr)
131131
}
132132
if err != nil {
133-
if tt.errcode != "" {
134-
desc := xerrors.NewDesc(err)
135-
require.Equal(t, tt.errcode, desc.ErrCode())
136-
}
133+
require.ErrorIs(t, err, tt.err)
137134
}
138135
})
139136
}
@@ -148,7 +145,7 @@ func TestDocParser_parseDocumentUniqueFieldStructList(t *testing.T) {
148145
parser *sheetParser
149146
args args
150147
wantErr bool
151-
errcode string
148+
err error
152149
}{
153150
{
154151
name: "no duplicate key",
@@ -305,7 +302,7 @@ func TestDocParser_parseDocumentUniqueFieldStructList(t *testing.T) {
305302
},
306303
},
307304
wantErr: true,
308-
errcode: "E2022",
305+
err: xerrors.ErrE2022,
309306
},
310307
{
311308
name: "duplicate name",
@@ -384,7 +381,7 @@ func TestDocParser_parseDocumentUniqueFieldStructList(t *testing.T) {
384381
},
385382
},
386383
wantErr: true,
387-
errcode: "E2022",
384+
err: xerrors.ErrE2022,
388385
},
389386
}
390387
for _, tt := range tests {
@@ -394,10 +391,8 @@ func TestDocParser_parseDocumentUniqueFieldStructList(t *testing.T) {
394391
t.Errorf("sheetParser.Parse() error = %+v, wantErr %v", err, tt.wantErr)
395392
}
396393
if err != nil {
397-
if tt.errcode != "" {
398-
desc := xerrors.NewDesc(err)
399-
require.Equal(t, tt.errcode, desc.ErrCode())
400-
}
394+
t.Logf("err: %+v", err)
395+
require.ErrorIs(t, err, tt.err)
401396
}
402397
})
403398
}
@@ -412,7 +407,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
412407
parser *sheetParser
413408
args args
414409
wantErr bool
415-
errcode string
410+
err error
416411
}{
417412
{
418413
name: "no duplicate key",
@@ -631,7 +626,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
631626
},
632627
},
633628
wantErr: true,
634-
errcode: "E2022",
629+
err: xerrors.ErrE2022,
635630
},
636631
{
637632
name: "duplicate section name",
@@ -741,7 +736,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
741736
},
742737
},
743738
wantErr: true,
744-
errcode: "E2022",
739+
err: xerrors.ErrE2022,
745740
},
746741
{
747742
name: "duplicate chapter id",
@@ -851,7 +846,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
851846
},
852847
},
853848
wantErr: true,
854-
errcode: "E2005",
849+
err: xerrors.ErrE2005,
855850
},
856851
{
857852
name: "duplicate scalar map key",
@@ -891,7 +886,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
891886
},
892887
},
893888
wantErr: true,
894-
errcode: "E2005",
889+
err: xerrors.ErrE2005,
895890
},
896891
{
897892
name: "duplicate incell map key",
@@ -917,7 +912,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
917912
},
918913
},
919914
wantErr: true,
920-
errcode: "E2005",
915+
err: xerrors.ErrE2005,
921916
},
922917
{
923918
name: "card prefix uniqueness",
@@ -1102,10 +1097,7 @@ func TestDocParser_parseDocumentUniqueFieldStructMap(t *testing.T) {
11021097
t.Errorf("sheetParser.Parse() error = %+v, wantErr %v", err, tt.wantErr)
11031098
}
11041099
if err != nil {
1105-
if tt.errcode != "" {
1106-
desc := xerrors.NewDesc(err)
1107-
require.Equal(t, tt.errcode, desc.ErrCode())
1108-
}
1100+
require.ErrorIs(t, err, tt.err)
11091101
}
11101102
})
11111103
}
@@ -1120,7 +1112,7 @@ func TestDocParser_parseSequenceUniqueFieldStructList(t *testing.T) {
11201112
parser *sheetParser
11211113
args args
11221114
wantErr bool
1123-
errcode string
1115+
err error
11241116
}{
11251117
{
11261118
name: "in sequence",
@@ -1277,7 +1269,7 @@ func TestDocParser_parseSequenceUniqueFieldStructList(t *testing.T) {
12771269
},
12781270
},
12791271
wantErr: true,
1280-
errcode: "E2003",
1272+
err: xerrors.ErrE2003,
12811273
},
12821274
}
12831275
for _, tt := range tests {
@@ -1287,10 +1279,7 @@ func TestDocParser_parseSequenceUniqueFieldStructList(t *testing.T) {
12871279
t.Errorf("sheetParser.Parse() error = %+v, wantErr %v", err, tt.wantErr)
12881280
}
12891281
if err != nil {
1290-
if tt.errcode != "" {
1291-
desc := xerrors.NewDesc(err)
1292-
require.Equal(t, tt.errcode, desc.ErrCode())
1293-
}
1282+
require.ErrorIs(t, err, tt.err)
12941283
}
12951284
})
12961285
}

internal/confgen/parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func (p *sheetParser) checkKeyUnique(md protoreflect.MessageDescriptor, fdOpts *
561561
defer keyField.release()
562562
if fieldprop.RequireUnique(keyField.opts.Prop) ||
563563
(!fieldprop.HasUnique(keyField.opts.Prop) && p.deduceKeyUnique(fdOpts.Layout, md)) {
564-
return xerrors.Wrap(xerrors.E2005(keyData))
564+
return xerrors.E2005(keyData)
565565
}
566566
return nil
567567
}
@@ -704,7 +704,7 @@ func (p *sheetParser) checkSubFieldProp(field *Field, cardPrefix string, newValu
704704
}
705705
valStr := newValue.Message().Get(field.fd).String()
706706
if field.values[valStr] {
707-
return name, xerrors.E2022(field.fd.Name(), valStr)
707+
return name, xerrors.E2022(string(field.fd.Name()), valStr)
708708
}
709709
// add new unique value
710710
field.values[valStr] = true
@@ -760,7 +760,7 @@ func (p *sheetParser) parseListElems(field *Field, list protoreflect.List, cardP
760760
// Guarantee all the remaining elements are not present,
761761
// otherwise report error!
762762
if elemPresent {
763-
return false, xerrors.Wrap(xerrors.E2016(firstNonePresentIndex, i))
763+
return false, xerrors.E2016(firstNonePresentIndex, i)
764764
}
765765
continue
766766
}

0 commit comments

Comments
 (0)