(t *testing.T)
| 266 | } |
| 267 | |
| 268 | func TestStrictStructArgs(t *testing.T) { |
| 269 | t.Parallel() |
| 270 | |
| 271 | type MyInt int |
| 272 | |
| 273 | for _, tt := range []struct { |
| 274 | name string |
| 275 | input any |
| 276 | sql string |
| 277 | expectedSQL string |
| 278 | expectedArgs []any |
| 279 | expectError bool |
| 280 | }{ |
| 281 | { |
| 282 | name: "fallback to field name without db tag", |
| 283 | input: struct { |
| 284 | ID int |
| 285 | }{ID: 1}, |
| 286 | sql: "select * from t where ID=@ID", |
| 287 | expectedSQL: "select * from t where ID=$1", |
| 288 | expectedArgs: []any{1}, |
| 289 | }, |
| 290 | { |
| 291 | name: "empty db tag errors", |
| 292 | input: struct { |
| 293 | ID int `db:","` |
| 294 | }{ID: 1}, |
| 295 | sql: "select * from t where ID=@ID", |
| 296 | expectError: true, |
| 297 | }, |
| 298 | { |
| 299 | name: "duplicate keys error", |
| 300 | input: struct { |
| 301 | A int `db:"x"` |
| 302 | B int `db:"x"` |
| 303 | }{A: 1, B: 2}, |
| 304 | sql: "select * from t where x=@x", |
| 305 | expectError: true, |
| 306 | }, |
| 307 | { |
| 308 | name: "skips anonymous embedded structs without flattening", |
| 309 | input: func() any { |
| 310 | type Embedded struct { |
| 311 | ID int `db:"id"` |
| 312 | } |
| 313 | type S struct { |
| 314 | Embedded |
| 315 | Name string `db:"name"` |
| 316 | } |
| 317 | return S{Embedded: Embedded{ID: 1}, Name: "x"} |
| 318 | }(), |
| 319 | sql: "select * from t where name=@name and id=@id", |
| 320 | expectError: true, |
| 321 | }, |
| 322 | { |
| 323 | name: "anonymous embedded non-struct still requires tag in strict mode", |
| 324 | input: func() any { |
| 325 | type S struct { |
nothing calls this directly
no test coverage detected