Skip to content

Commit cdd88ce

Browse files
committed
fix(golangci-lint): fix forcetypeassert errors
Go 1.22 introduced a restriction where type assertions in multiple assignments must be standalone. This fix ensures compliance by: - Separating type assertions before passing values to functions. - Adding error handling for failed assertions to prevent runtime panics. - Consolidating duplicate cases for cleaner logic. This resolves golangci-lint 'forcetypeassert' errors. Fixes #1932 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent e6ea231 commit cdd88ce

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

.golangci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ linters:
115115
- stylecheck
116116
- tagalign
117117
#- tagliatelle
118-
- tenv
119118
- testableexamples
120119
# - testifylint
121120
#- testpackage

pkg/templates/templating.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -71,44 +71,48 @@ func ReplacePlaceHoldersVariables(template string, dico map[string]string, rawEv
7171
if v, ok := val.Value().(string); ok {
7272
b = []byte(v)
7373
}
74-
case types.Bytes:
75-
raw, err = val.ConvertToNative(structType)
76-
if err == nil {
77-
b, err = raw.(*structpb.Value).MarshalJSON()
78-
if err != nil {
79-
b = []byte{}
80-
}
81-
}
82-
case types.Double, types.Int:
74+
75+
case types.Bytes, types.Double, types.Int:
8376
raw, err = val.ConvertToNative(structType)
8477
if err == nil {
85-
b, err = raw.(*structpb.Value).MarshalJSON()
86-
if err != nil {
87-
b = []byte{}
78+
if structVal, ok := raw.(*structpb.Value); ok {
79+
b, err = structVal.MarshalJSON()
80+
if err != nil {
81+
b = []byte{}
82+
}
8883
}
8984
}
85+
9086
case traits.Lister:
9187
raw, err = val.ConvertToNative(listType)
9288
if err == nil {
93-
s, err := protojson.Marshal(raw.(proto.Message))
94-
if err == nil {
95-
b = s
89+
if msg, ok := raw.(proto.Message); ok {
90+
b, err = protojson.Marshal(msg)
91+
if err != nil {
92+
b = []byte{}
93+
}
9694
}
9795
}
96+
9897
case traits.Mapper:
9998
raw, err = val.ConvertToNative(mapType)
10099
if err == nil {
101-
s, err := protojson.Marshal(raw.(proto.Message))
102-
if err == nil {
103-
b = s
100+
if msg, ok := raw.(proto.Message); ok {
101+
b, err = protojson.Marshal(msg)
102+
if err != nil {
103+
b = []byte{}
104+
}
104105
}
105106
}
107+
106108
case types.Bool:
107109
raw, err = val.ConvertToNative(structType)
108110
if err == nil {
109-
b, err = json.Marshal(raw.(*structpb.Value).GetBoolValue())
110-
if err != nil {
111-
b = []byte{}
111+
if structVal, ok := raw.(*structpb.Value); ok {
112+
b, err = json.Marshal(structVal.GetBoolValue())
113+
if err != nil {
114+
b = []byte{}
115+
}
112116
}
113117
}
114118

0 commit comments

Comments
 (0)