Skip to content

Commit f69949e

Browse files
committed
update documentation to note that both -> and _ are supported by default as a delimiter
1 parent e81af3f commit f69949e

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

DESIGN_PHILOSOPHIES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ Making a guess might seem helpful, but it can hide serious, silent bugs. The fol
4545
- **Destination:** `var blogs []BlogWithPosts`
4646
- **Behavior:** `carta` **gracefully handles** the fact that the same blog ID appears in multiple rows. It creates one `Blog` object and appends each unique `Post` to its `Posts` slice.
4747
- **Why this is Graceful:** This is the core purpose of the library. There is no ambiguity. The library uses the unique ID of the `Blog` (the `b.id` column) to understand that these rows all describe the same parent entity. This is a well-defined transformation, not a guess.
48+
49+
---
50+
51+
### Scenario 4: Default `_` Delimiter Support (Graceful Handling)
52+
53+
- **Query:** `SELECT b.id, a.id AS "author_id" FROM blogs b JOIN authors a ON b.author_id = a.id`
54+
- **Destination:** `var blogs []BlogWithAuthor`
55+
- **Behavior:** `carta` **gracefully handles** the `author_id` column, correctly mapping it to the `Author` struct's `id` field, even though the default delimiter is `->`.
56+
- **Why this is Graceful:** This convenience was a deliberate design choice. Since SQL `SELECT` statements must have unambiguous column names (which you control with aliases), there is no risk of conflict with actual database field names that contain underscores. This allows for more natural-looking column names in queries without requiring an explicit `delimiter=_` option in the `carta` tag. If another delimiter is desired, it must be set explicitly.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ type User struct {
134134
```
135135

136136
#### Associations (Nested Structs)
137-
For nested structs (has-one or has-many relationships), use the `carta` tag to define a prefix for the nested struct's columns. The default delimiter between the prefix and the field name is `->`.
137+
For nested structs (has-one or has-many relationships), use the `carta` tag to define a prefix for the nested struct's columns. The default delimiter is `->`, but `_` is also supported out-of-the-box for convenience. This dual support does not create conflicts with field names containing underscores (e.g., `first_name`) because mapping is based on the final, unambiguous column names returned by your `SELECT` query, which you control via SQL aliases.
138138

139139
**Example:**
140140
```go
@@ -165,17 +165,17 @@ left join authors a on b.author_id = a.id
165165
This design promotes struct reusability. The `Author` struct can be used on its own to map to a query like `select id, username from authors` or as a nested struct within `Blog` as shown above.
166166

167167
**Custom Delimiter:**
168-
You can override the default delimiter by specifying it in the `carta` tag.
168+
If you need to use a delimiter other than the default `->` or the supported `_`, you can override it by specifying it in the `carta` tag.
169169
```go
170170
type Blog struct {
171-
Author Author `carta:"author,delimiter=_"`
171+
Author Author `carta:"author,delimiter=."` // Using a dot as a delimiter
172172
}
173173
```
174174
**Corresponding SQL Query:**
175175
```sql
176176
select
177-
a.id as "author_id",
178-
a.username as "author_username"
177+
a.id as "author.id",
178+
a.username as "author.username"
179179
...
180180
```
181181

0 commit comments

Comments
 (0)