Skip to content

Commit feb3554

Browse files
committed
Merge remote-tracking branch 'origin/main' into chore/include-opts
2 parents b675d89 + 17b79b5 commit feb3554

File tree

14 files changed

+207
-58
lines changed

14 files changed

+207
-58
lines changed

.golangci.yaml

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
linters:
2+
enable:
3+
- revive
4+
- sloglint
5+
- godox
6+
- gosec
7+
- musttag
8+
- nilnil
9+
- goconst
10+
- gocritic
11+
- gofmt
12+
- iface
13+
- thelper
14+
- tparallel
15+
issues:
16+
exclude-rules:
17+
- path: (.+)_test.go
18+
linters:
19+
- revive
20+
text: "^(enforce-slice-style|enforce-map-style)"
21+
linters-settings:
22+
revive:
23+
# Default: false
24+
ignore-generated-header: true
25+
# Default: 0.8
26+
confidence: 0.1
27+
rules:
28+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#context-as-argument
29+
- name: context-as-argument
30+
severity: error
31+
disabled: false
32+
exclude: [ "" ]
33+
arguments:
34+
- allowTypesBefore: "*testing.T"
35+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#early-return
36+
- name: early-return
37+
severity: error
38+
disabled: false
39+
exclude: [ "" ]
40+
arguments:
41+
- "preserveScope"
42+
- "allowJump"
43+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#enforce-map-style
44+
- name: enforce-map-style
45+
severity: error
46+
disabled: false
47+
exclude: [ "" ]
48+
arguments:
49+
- "make"
50+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#enforce-slice-style
51+
- name: enforce-slice-style
52+
severity: error
53+
disabled: false
54+
exclude: [ "" ]
55+
arguments:
56+
- "make"
57+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#filename-format
58+
- name: filename-format
59+
severity: error
60+
disabled: false
61+
exclude: [ "" ]
62+
arguments:
63+
- "^[_a-z][_a-z0-9]*.go$"
64+
# https://github.com/mgechev/revive/blob/HEAD/RULES_DESCRIPTIONS.md#optimize-operands-order
65+
- name: optimize-operands-order
66+
severity: error
67+
disabled: false
68+
exclude: [ "" ]
69+
sloglint:
70+
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
71+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#attributes-only
72+
# Default: false
73+
attr-only: true
74+
# Enforce using static values for log messages.
75+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#static-messages
76+
# Default: false
77+
static-msg: true
78+
# Enforce using constants instead of raw keys.
79+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-raw-keys
80+
# Default: false
81+
no-raw-keys: true
82+
# Enforce a single key naming convention.
83+
# Values: snake, kebab, camel, pascal
84+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#key-naming-convention
85+
# Default: ""
86+
key-naming-case: snake
87+
# Enforce not using specific keys.
88+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#forbidden-keys
89+
# Default: []
90+
forbidden-keys:
91+
- time
92+
- level
93+
- msg
94+
- source
95+
- foo
96+
# Enforce putting arguments on separate lines.
97+
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#arguments-on-separate-lines
98+
# Default: false
99+
args-on-sep-lines: true
100+
goconst:
101+
# Ignore test files.
102+
# Default: false
103+
ignore-tests: true
104+
# Ignore when constant is not used as function argument.
105+
# Default: true
106+
ignore-calls: true
107+
# Exclude strings matching the given regular expression.
108+
# Default: ""
109+
ignore-strings: ''
110+
nilnil:
111+
# In addition, detect opposite situation (simultaneous return of non-nil error and valid value).
112+
# Default: false
113+
detect-opposite: true
114+
# List of return types to check.
115+
# Default: ["chan", "func", "iface", "map", "ptr", "uintptr", "unsafeptr"]
116+
checked-types:
117+
- chan
118+
- func
119+
- iface
120+
- map
121+
- ptr
122+
- uintptr
123+
- unsafeptr
124+
gocritic:
125+
enable-all: true
126+
gofmt:
127+
# Simplify code: gofmt with `-s` option.
128+
# Default: true
129+
simplify: false
130+
# Apply the rewrite rules to the source before reformatting.
131+
# https://pkg.go.dev/cmd/gofmt
132+
# Default: []
133+
rewrite-rules:
134+
- pattern: 'interface{}'
135+
replacement: 'any'
136+
- pattern: 'a[b:len(a)]'
137+
replacement: 'a[b:]'
138+
iface:
139+
# List of analyzers.
140+
# Default: ["identical"]
141+
enable:
142+
- identical # Identifies interfaces in the same package that have identical method sets.
143+
- unused # Identifies interfaces that are not used anywhere in the same package where the interface is defined.

examples/basic_with_opts/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
patcher.WithIncludeZeroValues(true),
5151
patcher.WithIncludeNilValues(true),
5252
patcher.WithIgnoredFields("ignoredbylist"),
53-
patcher.WithIgnoredFieldsFunc(func(field reflect.StructField) bool {
53+
patcher.WithIgnoredFieldsFunc(func(field *reflect.StructField) bool {
5454
return strings.ToLower(field.Name) == "ignoredbyfunc"
5555
}),
5656
)

examples/loader_with_opts/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
patcher.WithIncludeZeroValues(true),
5454
patcher.WithIncludeNilValues(true),
5555
patcher.WithIgnoredFields("ignoredField", "IgNoReDfIeLdTwO"),
56-
patcher.WithIgnoredFieldsFunc(func(field reflect.StructField) bool {
56+
patcher.WithIgnoredFieldsFunc(func(field *reflect.StructField) bool {
5757
return strings.ToLower(field.Name) == "ignoredfieldbyfunc"
5858
}),
5959
); err != nil {

inserter/sql.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (b *SQLBatch) genBatch(resources []any) {
6262
}
6363

6464
// Skip fields that are to be ignored
65-
if b.checkSkipField(f) {
65+
if b.checkSkipField(&f) {
6666
continue
6767
}
6868

@@ -79,7 +79,7 @@ func (b *SQLBatch) genBatch(resources []any) {
7979
tag = f.Name
8080
}
8181

82-
b.args = append(b.args, b.getFieldValue(v.Field(i), f))
82+
b.args = append(b.args, b.getFieldValue(v.Field(i), &f))
8383

8484
// if the field is not unique, skip it
8585
if _, ok := uniqueFields[tag]; ok {
@@ -93,7 +93,7 @@ func (b *SQLBatch) genBatch(resources []any) {
9393
}
9494
}
9595

96-
func (b *SQLBatch) getFieldValue(v reflect.Value, f reflect.StructField) any {
96+
func (b *SQLBatch) getFieldValue(v reflect.Value, f *reflect.StructField) any {
9797
if f.Type.Kind() == reflect.Ptr {
9898
if v.IsNil() {
9999
return nil
@@ -104,7 +104,7 @@ func (b *SQLBatch) getFieldValue(v reflect.Value, f reflect.StructField) any {
104104
return v.Interface()
105105
}
106106

107-
func (b *SQLBatch) GenerateSQL() (string, []any, error) {
107+
func (b *SQLBatch) GenerateSQL() (sqlStr string, args []any, err error) {
108108
if err := b.validateSQLGen(); err != nil {
109109
return "", nil, err
110110
}
@@ -145,7 +145,7 @@ func (b *SQLBatch) Perform() (sql.Result, error) {
145145
return b.db.Exec(sqlStr, args...)
146146
}
147147

148-
func (b *SQLBatch) checkSkipField(field reflect.StructField) bool {
148+
func (b *SQLBatch) checkSkipField(field *reflect.StructField) bool {
149149
// The ignore fields tag takes precedence over the ignore fields list
150150
if b.checkSkipTag(field) {
151151
return true
@@ -159,7 +159,7 @@ func (b *SQLBatch) checkSkipField(field reflect.StructField) bool {
159159
return b.ignoredFieldsCheck(field)
160160
}
161161

162-
func (b *SQLBatch) checkSkipTag(field reflect.StructField) bool {
162+
func (b *SQLBatch) checkSkipTag(field *reflect.StructField) bool {
163163
val, ok := field.Tag.Lookup(patcher.TagOptsName)
164164
if !ok {
165165
return false
@@ -169,7 +169,7 @@ func (b *SQLBatch) checkSkipTag(field reflect.StructField) bool {
169169
return slices.Contains(tags, patcher.TagOptSkip)
170170
}
171171

172-
func (b *SQLBatch) checkPrimaryKey(field reflect.StructField) bool {
172+
func (b *SQLBatch) checkPrimaryKey(field *reflect.StructField) bool {
173173
// If we are including the primary key, we can immediately return false
174174
if b.includePrimaryKey {
175175
return false
@@ -184,14 +184,14 @@ func (b *SQLBatch) checkPrimaryKey(field reflect.StructField) bool {
184184
return slices.Contains(tags, patcher.DBTagPrimaryKey)
185185
}
186186

187-
func (b *SQLBatch) ignoredFieldsCheck(field reflect.StructField) bool {
188-
return b.checkIgnoredFields(strings.ToLower(field.Name)) || b.checkIgnoreFunc(field)
187+
func (b *SQLBatch) ignoredFieldsCheck(field *reflect.StructField) bool {
188+
return b.checkIgnoredFields(field.Name) || b.checkIgnoreFunc(field)
189189
}
190190

191-
func (b *SQLBatch) checkIgnoreFunc(field reflect.StructField) bool {
191+
func (b *SQLBatch) checkIgnoreFunc(field *reflect.StructField) bool {
192192
return b.ignoreFieldsFunc != nil && b.ignoreFieldsFunc(field)
193193
}
194194

195195
func (b *SQLBatch) checkIgnoredFields(field string) bool {
196-
return len(b.ignoreFields) > 0 && slices.Contains(b.ignoreFields, strings.ToLower(field))
196+
return len(b.ignoreFields) > 0 && slices.Contains(b.ignoreFields, field)
197197
}

inserter/sql_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_WithPointedFields() {
416416

417417
s.Equal("INSERT INTO temp (id, name) VALUES (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)", sql)
418418

419-
expectedArgs := []any{1, "test", interface{}(nil), "test2", 3, "test3", 4, "test4", 5, "test5"}
419+
expectedArgs := []any{1, "test", any(nil), "test2", 3, "test3", 4, "test4", 5, "test5"}
420420
s.Require().Equal(expectedArgs, args)
421421
}
422422

@@ -440,7 +440,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_WithPointedFields_noDbTag() {
440440

441441
s.Equal("INSERT INTO temp (ID, Name) VALUES (?, ?), (?, ?), (?, ?), (?, ?), (?, ?)", sql)
442442

443-
expectedArgs := []any{1, "test", interface{}(nil), "test2", 3, "test3", 4, "test4", 5, "test5"}
443+
expectedArgs := []any{1, "test", any(nil), "test2", 3, "test3", 4, "test4", 5, "test5"}
444444
s.Require().Equal(expectedArgs, args)
445445
}
446446

@@ -484,7 +484,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_IgnoredFieldsFunc() {
484484
}
485485

486486
mif := patcher.NewMockIgnoreFieldsFunc(s.T())
487-
mif.On("Execute", mock.Anything).Return(func(f reflect.StructField) bool {
487+
mif.On("Execute", mock.AnythingOfType("*reflect.StructField")).Return(func(f *reflect.StructField) bool {
488488
return f.Name == "ID"
489489
})
490490

joiner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ type joinStringOption struct {
2525
args []any
2626
}
2727

28-
func (j *joinStringOption) Join() (string, []any) {
28+
func (j *joinStringOption) Join() (sqlStr string, args []any) {
2929
return j.join, j.args
3030
}

loader.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
//
2121
// This function is useful if you are inserting a patch into an existing object but require a new object to be returned with
2222
// all fields updated.
23-
func LoadDiff[T any](old *T, newT *T, opts ...PatchOpt) error {
23+
func LoadDiff[T any](old, newT *T, opts ...PatchOpt) error {
2424
return newPatchDefaults(opts...).loadDiff(old, newT)
2525
}
2626

@@ -74,12 +74,14 @@ func (s *SQLPatch) loadDiff(old, newT any) error {
7474
continue
7575
}
7676

77+
oldField := oElem.Type().Field(i)
78+
7779
// See if the field should be ignored.
78-
if s.checkSkipField(oElem.Type().Field(i)) {
80+
if s.checkSkipField(&oldField) {
7981
continue
8082
}
8183

82-
patcherOptsTag := oElem.Type().Field(i).Tag.Get(TagOptsName)
84+
patcherOptsTag := oldField.Tag.Get(TagOptsName)
8385

8486
// Compare the old and new fields.
8587
//
@@ -94,7 +96,7 @@ func (s *SQLPatch) loadDiff(old, newT any) error {
9496
return nil
9597
}
9698

97-
func (s *SQLPatch) checkSkipField(field reflect.StructField) bool {
99+
func (s *SQLPatch) checkSkipField(field *reflect.StructField) bool {
98100
// The ignore fields tag takes precedence over the ignore fields list
99101
if s.checkSkipTag(field) {
100102
return true
@@ -103,7 +105,7 @@ func (s *SQLPatch) checkSkipField(field reflect.StructField) bool {
103105
return s.ignoredFieldsCheck(field)
104106
}
105107

106-
func (s *SQLPatch) checkSkipTag(field reflect.StructField) bool {
108+
func (s *SQLPatch) checkSkipTag(field *reflect.StructField) bool {
107109
val, ok := field.Tag.Lookup(TagOptsName)
108110
if !ok {
109111
return false
@@ -113,11 +115,11 @@ func (s *SQLPatch) checkSkipTag(field reflect.StructField) bool {
113115
return slices.Contains(tags, TagOptSkip)
114116
}
115117

116-
func (s *SQLPatch) ignoredFieldsCheck(field reflect.StructField) bool {
118+
func (s *SQLPatch) ignoredFieldsCheck(field *reflect.StructField) bool {
117119
return s.checkIgnoredFields(field.Name) || s.checkIgnoreFunc(field)
118120
}
119121

120-
func (s *SQLPatch) checkIgnoreFunc(field reflect.StructField) bool {
122+
func (s *SQLPatch) checkIgnoreFunc(field *reflect.StructField) bool {
121123
return s.ignoreFieldsFunc != nil && s.ignoreFieldsFunc(field)
122124
}
123125

loader_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFields() {
672672

673673
func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFunc() {
674674
l := s.patch
675-
l.ignoreFieldsFunc = func(field reflect.StructField) bool {
676-
return strings.ToLower(field.Name) == "name"
675+
l.ignoreFieldsFunc = func(field *reflect.StructField) bool {
676+
return strings.EqualFold(field.Name, "name")
677677
}
678678

679679
type testStruct struct {
@@ -700,8 +700,8 @@ func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFunc() {
700700
func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFuncAndIgnoreFields() {
701701
l := s.patch
702702
l.ignoreFields = []string{"name"}
703-
l.ignoreFieldsFunc = func(field reflect.StructField) bool {
704-
return strings.ToLower(field.Name) == "name"
703+
l.ignoreFieldsFunc = func(field *reflect.StructField) bool {
704+
return strings.EqualFold(field.Name, "name")
705705
}
706706

707707
type testStruct struct {

mock_IgnoreFieldsFunc.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

multifilter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type multiFilter struct {
1515
whereArgs []any
1616
}
1717

18-
func (m *multiFilter) Join() (string, []any) {
18+
func (m *multiFilter) Join() (sqlStr string, args []any) {
1919
return m.joinSql.String(), m.joinArgs
2020
}
2121

22-
func (m *multiFilter) Where() (string, []any) {
22+
func (m *multiFilter) Where() (sqlStr string, args []any) {
2323
return m.whereSql.String(), m.whereArgs
2424
}
2525

0 commit comments

Comments
 (0)