-
Notifications
You must be signed in to change notification settings - Fork 169
refactor(api): apply type conversion naming pattern #4135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| --- | ||
| name: go-types-conversion | ||
| description: Naming convention for type-translation files and functions. Use when creating or editing files that convert between domain, API, and DB types. | ||
| user-invocable: true | ||
| allowed-tools: Read, Edit, Write, Grep, Glob | ||
| --- | ||
|
|
||
| # Type Translation Naming | ||
|
|
||
| Apply to new and touched code. Do not rename legacy symbols unsolicited. | ||
|
|
||
| ## File naming | ||
|
|
||
| | Path contains | File name | Purpose | | ||
| | ------------------------------------------------- | ------------ | ------------ | | ||
| | `httpdriver/`, `httphandler/`, `api/v3/handlers/` | `convert.go` | API ↔ domain | | ||
| | `adapter/`, `repo/` | `mapping.go` | DB ↔ domain | | ||
|
|
||
| Split large files by entity: `convert_plan.go`, `mapping_subscription.go`. | ||
|
|
||
| `mapper.go` is forbidden. Rename it to `convert.go` or `mapping.go` (based on layer) when the file is touched. | ||
|
|
||
| ## Function naming | ||
|
|
||
| ### Shape: `From<Qualifier><Thing>` / `To<Qualifier><Thing>` | ||
|
|
||
| The qualifier is `API` or `DB` — no other qualifiers (`Domain`, `Model`, package-name infixes). | ||
|
|
||
| The suffix `<Thing>` is the **non-domain type's unqualified name** — the API type or DB type, not the domain type. This keeps it stable: a matched pair (`FromAPI<Thing>` / `ToAPI<Thing>`) always refers to the same non-domain type, regardless of direction. | ||
|
|
||
| - `FromAPI<Thing>` — takes the API type `<Thing>` as input, returns the domain representation. | ||
| - `ToAPI<Thing>` — takes the domain type as input, returns the API type `<Thing>`. | ||
| - Same for `FromDB<Thing>` / `ToDB<Thing>`. | ||
|
|
||
| ### Examples | ||
|
|
||
| ```go | ||
| // API ↔ domain | ||
| FromAPIPlan(a api.Plan) (plan.Plan, error) | ||
| ToAPIPlan(p plan.Plan) api.Plan | ||
|
|
||
| // Suffix is the API type name, even when domain type differs | ||
| FromAPIPlanCreate(a api.PlanCreate) (plan.CreateInput, error) | ||
| ToAPIPlanCreate(p plan.CreateInput) api.PlanCreate | ||
|
|
||
| FromAPIProRatingConfig(a api.ProRatingConfig) (productcatalog.ProRatingConfig, error) | ||
| ToAPIProRatingConfig(p productcatalog.ProRatingConfig) *api.ProRatingConfig | ||
|
|
||
| // DB ↔ domain — suffix is the DB type name | ||
| FromDBSubscription(row *db.Subscription) (subscription.Subscription, error) | ||
| ToDBSubscription(s subscription.Subscription) *db.Subscription | ||
|
|
||
| FromDBChargeFlatFee(row *entdb.ChargeFlatFee) (flatfee.Charge, error) | ||
| ToDBChargeFlatFee(c flatfee.Charge) *entdb.ChargeFlatFee | ||
| ``` | ||
|
|
||
| ### Additional rules | ||
|
|
||
| - **Exported** functions always include the type suffix (`FromAPIPlanCreate`, not bare `FromAPI`). | ||
| - **Unexported** helpers in a single-type file may drop the suffix (`fromDB`, `toAPI`). | ||
| - **Fallible** (parse/validate) → `(T, error)`. **Infallible** (projection) → `T`. Typically `FromAPI…` / `FromDB…` is fallible; the reverse is not. | ||
| - **Batch helpers** use the plural: `FromAPIPlans`, `ToDBSubscriptions`. Same suffix rule — the plural of the non-domain type name. | ||
|
|
||
| ### Forbidden patterns | ||
|
|
||
| - `Map…`, `Convert…To…`, primary `As…` | ||
| - `<Source>To<Target>` shape (e.g. `APIToPlan`) | ||
| - Bare `FromAPI` / `ToDB` without a type suffix | ||
| - goverter or other codegen type mappers | ||
|
|
||
| ## Decision tree | ||
|
|
||
| ### Naming a function | ||
|
|
||
| 1. **Pick the qualifier:** API/HTTP/wire on one side → `API`. DB/persistence on one side → `DB`. | ||
| 2. **Pick the suffix:** the non-domain type's unqualified name (`Plan`, `PlanCreate`, `ChargeFlatFee`). | ||
| 3. **Pick the return style:** fallible → `(T, error)`, infallible → `T`. | ||
| 4. **Exported?** Must include the type suffix. Unexported single-type helper may drop it. | ||
|
|
||
| ### Interacting with the user | ||
|
|
||
| - **File is `mapper.go`?** Flag it — should be `convert.go` or `mapping.go` based on layer. Offer to rename as part of the edit. Don't rename silently. | ||
| - **Adding new functions to a legacy file?** Use the new convention for new functions. Don't rename old ones unless asked. | ||
| - **Task is "clean up this file"?** Rename, update call sites, `Grep` for the old name to catch misses, keep the rename in its own commit. | ||
| - **`// Code generated` header?** Off-limits regardless. | ||
|
|
||
| ## Suggestion phrasing | ||
|
|
||
| Lead with the specific rename and the reason. Keep it short. | ||
|
|
||
| > `MapChargeFlatFeeFromDB` — use `FromDBChargeFlatFee`. Want me to rename and update callers? | ||
|
|
||
| > Direction looks inverted — `FromAPI…` returns a domain type, so this should be `ToAPIPlan`. Drop the error return if it can't actually fail. | ||
|
|
||
| > This file is `mapper.go` — should be `convert.go` since it lives in `httphandler/`. Want me to rename it? | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.