Skip to content

Commit b0d45c0

Browse files
denikIsaac
andcommitted
direct: satisfy linters in copier (exhaustruct, exhaustive, modernize)
Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 4b26281 commit b0d45c0

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

‎bundle/direct/dresources/copier.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dresources
22

33
import (
44
"fmt"
5+
"maps"
56
"reflect"
67

78
"github.com/databricks/cli/libs/structs/structaccess"
@@ -72,16 +73,16 @@ func compileCopier(remoteType, stateType reflect.Type) (*copier, error) {
7273
}
7374
switch {
7475
case dst.typ == src.typ:
75-
ops = append(ops, copyOp{dstIndex: dst.index, srcIndex: src.index, dstType: dst.typ})
76+
ops = append(ops, copyOp{forceSendFields: false, dstIndex: dst.index, srcIndex: src.index, convert: false, dstType: dst.typ, ownerType: nil})
7677
case safeConvert(dst.typ, src.typ):
77-
ops = append(ops, copyOp{dstIndex: dst.index, srcIndex: src.index, convert: true, dstType: dst.typ})
78+
ops = append(ops, copyOp{forceSendFields: false, dstIndex: dst.index, srcIndex: src.index, convert: true, dstType: dst.typ, ownerType: nil})
7879
default:
7980
return nil, fmt.Errorf("field %q: state type %s cannot be copied from remote type %s; implement RemapState", name, dst.typ, src.typ)
8081
}
8182
}
8283

8384
for _, target := range dstForceSend {
84-
ops = append(ops, copyOp{forceSendFields: true, dstIndex: target.index, ownerType: target.ownerType})
85+
ops = append(ops, copyOp{forceSendFields: true, dstIndex: target.index, srcIndex: nil, convert: false, dstType: nil, ownerType: target.ownerType})
8586
}
8687

8788
return &copier{stateElem: stateElem, ops: ops}, nil
@@ -101,7 +102,7 @@ func rootForceSendFields(remote reflect.Value) []string {
101102
if !f.IsValid() || f.Kind() != reflect.Slice {
102103
return nil
103104
}
104-
fields, _ := f.Interface().([]string)
105+
fields, _ := reflect.TypeAssert[[]string](f)
105106
return fields
106107
}
107108

@@ -152,9 +153,7 @@ func flattenStruct(t reflect.Type, prefix []int) (map[string]jsonFieldInfo, []fo
152153
}
153154
if structaccess.IsFlattenedEmbed(sf) {
154155
nested, nestedForceSend := flattenStruct(sf.Type, index)
155-
for name, info := range nested {
156-
fields[name] = info
157-
}
156+
maps.Copy(fields, nested)
158157
forceSend = append(forceSend, nestedForceSend...)
159158
continue
160159
}

‎bundle/direct/dresources/copier_test.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dresources
22

33
import (
44
"reflect"
5-
"sort"
5+
"slices"
66
"testing"
77

88
"github.com/stretchr/testify/assert"
@@ -50,7 +50,7 @@ func TestNoRedundantRemapState(t *testing.T) {
5050
}
5151
}
5252

53-
sort.Strings(redundant)
53+
slices.Sort(redundant)
5454
assert.Empty(t, redundant, "these resources have a RemapState the auto-copier reproduces exactly; delete the method and let buildCopiers handle it")
5555
}
5656

@@ -175,5 +175,7 @@ func fillValue(v reflect.Value, depth int, visited map[reflect.Type]bool) {
175175
if f := v.FieldByName("ForceSendFields"); f.IsValid() && f.Kind() == reflect.Slice && f.Type().Elem().Kind() == reflect.String {
176176
f.Set(reflect.ValueOf(names))
177177
}
178+
default:
179+
// other kinds (interface, chan, func, array, complex, uintptr, ...) are left zero
178180
}
179181
}

0 commit comments

Comments
 (0)