You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -272,6 +272,7 @@ type ATable struct {
272
272
unexported int// Unexported field is not visible to Struct.
273
273
Quotedstring`db:"quoted" fieldopt:"withquote"`// Add quote to the field using back quote or double quote. See `Flavor#Quote`.
274
274
Emptyuint`db:"empty" fieldopt:"omitempty"`// Omit the field in UPDATE if it is a nil or zero value.
275
+
ExpandedMeta`db:"expanded" fieldopt:"expand"`// Force a nested struct to expand even when sqlbuilder.NoExpand is true.
275
276
PayloadMeta`db:"payload" fieldopt:"noexpand"`// Treat a nested struct as one column instead of expanding it.
276
277
277
278
// The `omitempty` can be written as a function.
@@ -284,7 +285,7 @@ type ATable struct {
284
285
}
285
286
```
286
287
287
-
If a field is itself a struct and has a `db` tag, `Struct` treats that tag as a database alias and expands the nested fields. This is useful for `JOIN` projections built from reusable structs.
288
+
If a field is itself a struct and has a `db` tag, `Struct` treats that tag as a database alias and expands the nested fields by default. This is useful for `JOIN` projections built from reusable structs.
288
289
289
290
```go
290
291
typePoststruct {
@@ -312,6 +313,27 @@ fmt.Println(sql)
312
313
// SELECT post.id, post.text, comment.body FROM posts post JOIN comments comment ON post.id = comment.post_id
313
314
```
314
315
316
+
Set `sqlbuilder.NoExpand = true` before first use of a `Struct` to keep tagged nested structs as a single column by default. When the flag is enabled, add `fieldopt:"expand"` to opt a specific field back into expansion.
317
+
318
+
```go
319
+
typePayloadstruct {
320
+
Keystring`json:"key"`
321
+
}
322
+
323
+
typeRowstruct {
324
+
PayloadPayload`db:"payload"`
325
+
PostPost`db:"post" fieldopt:"expand"`
326
+
}
327
+
328
+
sqlbuilder.NoExpand = true
329
+
st:= sqlbuilder.NewStruct(new(Row))
330
+
331
+
fmt.Println(st.Columns())
332
+
333
+
// Output:
334
+
// [payload post.id post.text]
335
+
```
336
+
315
337
If a nested struct should stay as a single column, add `fieldopt:"noexpand"` to opt out of the automatic expansion. This is useful for JSON columns scanned into Go structs by the database driver.
0 commit comments