Skip to content

Commit 9d1bb88

Browse files
authored
ci: added forcetypeassert and misspell lint rules (#248)
* misspelling wins * fixed type assertion + added todo * fixed + added ignores * elevate type assertions to switch
1 parent 1a1ac22 commit 9d1bb88

File tree

8 files changed

+45
-25
lines changed

8 files changed

+45
-25
lines changed

.golangci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ issues:
55
linters:
66
disable-all: true
77
enable:
8-
- deadcode
98
- durationcheck
109
- errcheck
1110
- exportloopref
11+
- forcetypeassert
1212
- gofmt
1313
- gosimple
1414
- ineffassign
1515
- makezero
16+
- misspell
1617
- nilerr
1718
# - paralleltest # Reference: https://github.com/kunwardeep/paralleltest/issues/14
1819
- predeclared
1920
- staticcheck
2021
- tenv
2122
- unconvert
2223
- unparam
23-
- varcheck
24+
- unused
2425
- vet

tfprotov5/dynamic_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewDynamicValue(t tftypes.Type, v tftypes.Value) (DynamicValue, error) {
3535

3636
// DynamicValue represents a nested encoding value that came from the protocol.
3737
// The only way providers should ever interact with it is by calling its
38-
// `Unmarshal` method to retrive a `tftypes.Value`. Although the type system
38+
// `Unmarshal` method to retrieve a `tftypes.Value`. Although the type system
3939
// allows for other interactions, they are explicitly not supported, and will
4040
// not be considered when evaluating for breaking changes. Treat this type as
4141
// an opaque value, and *only* call its `Unmarshal` method.

tfprotov6/dynamic_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewDynamicValue(t tftypes.Type, v tftypes.Value) (DynamicValue, error) {
3535

3636
// DynamicValue represents a nested encoding value that came from the protocol.
3737
// The only way providers should ever interact with it is by calling its
38-
// `Unmarshal` method to retrive a `tftypes.Value`. Although the type system
38+
// `Unmarshal` method to retrieve a `tftypes.Value`. Although the type system
3939
// allows for other interactions, they are explicitly not supported, and will
4040
// not be considered when evaluating for breaking changes. Treat this type as
4141
// an opaque value, and *only* call its `Unmarshal` method.

tftypes/attribute_path.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,17 @@ func builtinAttributePathStepper(in interface{}) (AttributePathStepper, bool) {
324324
type mapStringInterfaceAttributePathStepper map[string]interface{}
325325

326326
func (m mapStringInterfaceAttributePathStepper) ApplyTerraform5AttributePathStep(step AttributePathStep) (interface{}, error) {
327-
_, isAttributeName := step.(AttributeName)
328-
_, isElementKeyString := step.(ElementKeyString)
327+
attributeName, isAttributeName := step.(AttributeName)
328+
elementKeyString, isElementKeyString := step.(ElementKeyString)
329329
if !isAttributeName && !isElementKeyString {
330330
return nil, ErrInvalidStep
331331
}
332332
var stepValue string
333333
if isAttributeName {
334-
stepValue = string(step.(AttributeName))
334+
stepValue = string(attributeName)
335335
}
336336
if isElementKeyString {
337-
stepValue = string(step.(ElementKeyString))
337+
stepValue = string(elementKeyString)
338338
}
339339
v, ok := m[stepValue]
340340
if !ok {

tftypes/diff.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ func (val1 Value) Diff(val2 Value) ([]ValueDiff, error) {
122122
return true, nil
123123
}
124124

125-
// convert from an interface{} to a Value
126-
value2 := value2I.(Value)
125+
value2, ok := value2I.(Value)
126+
if !ok {
127+
return false, fmt.Errorf("unexpected type %T in Diff", value2I)
128+
}
127129

128130
// if they're both unknown, no need to continue
129131
if !value1.IsKnown() && !value2.IsKnown() {

tftypes/value.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,15 @@ func (val Value) IsFullyKnown() bool {
540540
case primitive:
541541
return true
542542
case List, Set, Tuple:
543+
//nolint:forcetypeassert // NewValue func validates the type
543544
for _, v := range val.value.([]Value) {
544545
if !v.IsFullyKnown() {
545546
return false
546547
}
547548
}
548549
return true
549550
case Map, Object:
551+
//nolint:forcetypeassert // NewValue func validates the type
550552
for _, v := range val.value.(map[string]Value) {
551553
if !v.IsFullyKnown() {
552554
return false

tftypes/value_json.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ func jsonUnmarshal(buf []byte, typ Type, p *AttributePath, opts ValueFromJSONOpt
6565
case typ.Is(DynamicPseudoType):
6666
return jsonUnmarshalDynamicPseudoType(buf, typ, p, opts)
6767
case typ.Is(List{}):
68+
//nolint:forcetypeassert // Is func above guarantees this type assertion
6869
return jsonUnmarshalList(buf, typ.(List).ElementType, p, opts)
6970
case typ.Is(Set{}):
71+
//nolint:forcetypeassert // Is func above guarantees this type assertion
7072
return jsonUnmarshalSet(buf, typ.(Set).ElementType, p, opts)
7173
case typ.Is(Map{}):
74+
//nolint:forcetypeassert // Is func above guarantees this type assertion
7275
return jsonUnmarshalMap(buf, typ.(Map).ElementType, p, opts)
7376
case typ.Is(Tuple{}):
77+
//nolint:forcetypeassert // Is func above guarantees this type assertion
7478
return jsonUnmarshalTuple(buf, typ.(Tuple).ElementTypes, p, opts)
7579
case typ.Is(Object{}):
80+
//nolint:forcetypeassert // Is func above guarantees this type assertion
7681
return jsonUnmarshalObject(buf, typ.(Object).AttributeTypes, p, opts)
7782
}
7883
return Value{}, p.NewErrorf("unknown type %s", typ)

tftypes/value_msgpack.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,19 @@ func msgpackUnmarshal(dec *msgpack.Decoder, typ Type, path *AttributePath) (Valu
118118
}
119119
return NewValue(Bool, rv), nil
120120
case typ.Is(List{}):
121+
//nolint:forcetypeassert // Is func above guarantees this type assertion
121122
return msgpackUnmarshalList(dec, typ.(List).ElementType, path)
122123
case typ.Is(Set{}):
124+
//nolint:forcetypeassert // Is func above guarantees this type assertion
123125
return msgpackUnmarshalSet(dec, typ.(Set).ElementType, path)
124126
case typ.Is(Map{}):
127+
//nolint:forcetypeassert // Is func above guarantees this type assertion
125128
return msgpackUnmarshalMap(dec, typ.(Map).ElementType, path)
126129
case typ.Is(Tuple{}):
130+
//nolint:forcetypeassert // Is func above guarantees this type assertion
127131
return msgpackUnmarshalTuple(dec, typ.(Tuple).ElementTypes, path)
128132
case typ.Is(Object{}):
133+
//nolint:forcetypeassert // Is func above guarantees this type assertion
129134
return msgpackUnmarshalObject(dec, typ.(Object).AttributeTypes, path)
130135
}
131136
return Value{}, path.NewErrorf("unsupported type %s", typ.String())
@@ -364,15 +369,20 @@ func marshalMsgPack(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder)
364369
case typ.Is(Bool):
365370
return marshalMsgPackBool(val, typ, p, enc)
366371
case typ.Is(List{}):
367-
return marshalMsgPackList(val, typ, p, enc)
372+
//nolint:forcetypeassert // Is func above guarantees this type assertion
373+
return marshalMsgPackList(val, typ.(List), p, enc)
368374
case typ.Is(Set{}):
369-
return marshalMsgPackSet(val, typ, p, enc)
375+
//nolint:forcetypeassert // Is func above guarantees this type assertion
376+
return marshalMsgPackSet(val, typ.(Set), p, enc)
370377
case typ.Is(Map{}):
371-
return marshalMsgPackMap(val, typ, p, enc)
378+
//nolint:forcetypeassert // Is func above guarantees this type assertion
379+
return marshalMsgPackMap(val, typ.(Map), p, enc)
372380
case typ.Is(Tuple{}):
373-
return marshalMsgPackTuple(val, typ, p, enc)
381+
//nolint:forcetypeassert // Is func above guarantees this type assertion
382+
return marshalMsgPackTuple(val, typ.(Tuple), p, enc)
374383
case typ.Is(Object{}):
375-
return marshalMsgPackObject(val, typ, p, enc)
384+
//nolint:forcetypeassert // Is func above guarantees this type assertion
385+
return marshalMsgPackObject(val, typ.(Object), p, enc)
376386
}
377387
return fmt.Errorf("unknown type %s", typ)
378388
}
@@ -459,7 +469,7 @@ func marshalMsgPackBool(val Value, typ Type, p *AttributePath, enc *msgpack.Enco
459469
return nil
460470
}
461471

462-
func marshalMsgPackList(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
472+
func marshalMsgPackList(val Value, typ List, p *AttributePath, enc *msgpack.Encoder) error {
463473
l, ok := val.value.([]Value)
464474
if !ok {
465475
return unexpectedValueTypeError(p, l, val.value, typ)
@@ -469,15 +479,15 @@ func marshalMsgPackList(val Value, typ Type, p *AttributePath, enc *msgpack.Enco
469479
return p.NewErrorf("error encoding list length: %w", err)
470480
}
471481
for pos, i := range l {
472-
err := marshalMsgPack(i, typ.(List).ElementType, p.WithElementKeyInt(pos), enc)
482+
err := marshalMsgPack(i, typ.ElementType, p.WithElementKeyInt(pos), enc)
473483
if err != nil {
474484
return err
475485
}
476486
}
477487
return nil
478488
}
479489

480-
func marshalMsgPackSet(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
490+
func marshalMsgPackSet(val Value, typ Set, p *AttributePath, enc *msgpack.Encoder) error {
481491
s, ok := val.value.([]Value)
482492
if !ok {
483493
return unexpectedValueTypeError(p, s, val.value, typ)
@@ -487,15 +497,15 @@ func marshalMsgPackSet(val Value, typ Type, p *AttributePath, enc *msgpack.Encod
487497
return p.NewErrorf("error encoding set length: %w", err)
488498
}
489499
for _, i := range s {
490-
err := marshalMsgPack(i, typ.(Set).ElementType, p.WithElementKeyValue(i), enc)
500+
err := marshalMsgPack(i, typ.ElementType, p.WithElementKeyValue(i), enc)
491501
if err != nil {
492502
return err
493503
}
494504
}
495505
return nil
496506
}
497507

498-
func marshalMsgPackMap(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
508+
func marshalMsgPackMap(val Value, typ Map, p *AttributePath, enc *msgpack.Encoder) error {
499509
m, ok := val.value.(map[string]Value)
500510
if !ok {
501511
return unexpectedValueTypeError(p, m, val.value, typ)
@@ -509,20 +519,20 @@ func marshalMsgPackMap(val Value, typ Type, p *AttributePath, enc *msgpack.Encod
509519
if err != nil {
510520
return p.NewErrorf("error encoding map key: %w", err)
511521
}
512-
err = marshalMsgPack(v, typ.(Map).ElementType, p, enc)
522+
err = marshalMsgPack(v, typ.ElementType, p, enc)
513523
if err != nil {
514524
return err
515525
}
516526
}
517527
return nil
518528
}
519529

520-
func marshalMsgPackTuple(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
530+
func marshalMsgPackTuple(val Value, typ Tuple, p *AttributePath, enc *msgpack.Encoder) error {
521531
t, ok := val.value.([]Value)
522532
if !ok {
523533
return unexpectedValueTypeError(p, t, val.value, typ)
524534
}
525-
types := typ.(Tuple).ElementTypes
535+
types := typ.ElementTypes
526536
err := enc.EncodeArrayLen(len(types))
527537
if err != nil {
528538
return p.NewErrorf("error encoding tuple length: %w", err)
@@ -537,12 +547,12 @@ func marshalMsgPackTuple(val Value, typ Type, p *AttributePath, enc *msgpack.Enc
537547
return nil
538548
}
539549

540-
func marshalMsgPackObject(val Value, typ Type, p *AttributePath, enc *msgpack.Encoder) error {
550+
func marshalMsgPackObject(val Value, typ Object, p *AttributePath, enc *msgpack.Encoder) error {
541551
o, ok := val.value.(map[string]Value)
542552
if !ok {
543553
return unexpectedValueTypeError(p, o, val.value, typ)
544554
}
545-
types := typ.(Object).AttributeTypes
555+
types := typ.AttributeTypes
546556
keys := make([]string, 0, len(types))
547557
for k := range types {
548558
keys = append(keys, k)

0 commit comments

Comments
 (0)