Title: Generate types with methods that can be used as interfaces · Issue #444 · sqlc-dev/sqlc · GitHub
Open Graph Title: Generate types with methods that can be used as interfaces · Issue #444 · sqlc-dev/sqlc
X Title: Generate types with methods that can be used as interfaces · Issue #444 · sqlc-dev/sqlc
Description: I'm using sqlc together with a package that generates an http router based on an OpenAPI spec. I find that I need to do a lot of mapping between sqlc generated types and types for the http responses. This is ok if my queries are relative...
Open Graph Description: I'm using sqlc together with a package that generates an http router based on an OpenAPI spec. I find that I need to do a lot of mapping between sqlc generated types and types for the http response...
X Description: I'm using sqlc together with a package that generates an http router based on an OpenAPI spec. I find that I need to do a lot of mapping between sqlc generated types and types for the http resp...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/444
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generate types with methods that can be used as interfaces","articleBody":"I'm using sqlc together with a package that generates an http router based on an OpenAPI spec. I find that I need to do a lot of mapping between sqlc generated types and types for the http responses. This is ok if my queries are relatively simple, but I've found that with joins I end up with a lot of structs that share field types but have dedicated types. If these structs instead had methods whose signatures matched, I could create interfaces that these types implement and use these interfaces in the functions that generate API responses.\r\n\r\nFor example, say I've got a `jobs` table and a `deliveries` table.\r\n```sql\r\nCREATE TABLE jobs (\r\n\tid UUID PRIMARY KEY,\r\n\tstatus TEXT NOT NULL,\r\n\tcreated TIMESTAMP WITH TIME ZONE NOT NULL,\r\n\tupdated TIMESTAMP WITH TIME ZONE NOT NULL\r\n);\r\n\r\nCREATE TABLE deliveries (\r\n\tid UUID PRIMARY KEY,\r\n\tjob_id UUID UNIQUE NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,\r\n\tstatus TEXT NOT NULL,\r\n\tcreated TIMESTAMP WITH TIME ZONE NOT NULL,\r\n\tupdated TIMESTAMP WITH TIME ZONE NOT NULL\r\n);\r\n```\r\n\r\nAnd here are two example queries to get a single job and to list all jobs:\r\n```sql\r\n-- name: GetJob :one\r\nSELECT jobs.*, deliveries.id AS delivery_id\r\nFROM jobs\r\nLEFT JOIN deliveries ON jobs.id = deliveries.job_id\r\nWHERE jobs.id = $1;\r\n\r\n-- name: ListJobs :many\r\nSELECT jobs.*, deliveries.id AS delivery_id\r\nFROM jobs\r\nLEFT JOIN deliveries ON jobs.id = deliveries.job_id;\r\n```\r\n\r\nI end up with two structs whose fields match:\r\n```go\r\ntype GetJobRow struct {\r\n\tID uuid.UUID `json:\"id\"`\r\n\tStatus string `json:\"status\"`\r\n\tCreated time.Time `json:\"created\"`\r\n\tUpdated time.Time `json:\"updated\"`\r\n\tDeliveryID uuid.UUID `json:\"delivery_id\"`\r\n}\r\n\r\ntype ListJobsRow struct {\r\n\tID uuid.UUID `json:\"id\"`\r\n\tStatus string `json:\"status\"`\r\n\tCreated time.Time `json:\"created\"`\r\n\tUpdated time.Time `json:\"updated\"`\r\n\tDeliveryID uuid.UUID `json:\"delivery_id\"`\r\n}\r\n```\r\n\r\nIf these structs had methods for each of the fields, I could create a single `JobWithDeliveryID` interface and use that in the signature for my function that generates values for my API responses. Without that interface, I need to create two functions that map the types above to the same type for the API response.\r\n\r\nDoing this twice is not so bad, but I've found that the types proliferate pretty quickly, while the number of corresponding interfaces I would have to create is relatively low.\r\n","author":{"url":"https://github.com/tschaub","@type":"Person","name":"tschaub"},"datePublished":"2020-04-08T21:05:44Z","url":"https://github.com/sqlc-dev/sqlc/issues/444"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:513e08ea-091e-2223-4841-189c0595220a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87B0:38C5CF:27363E8:31C08FF:6B017A6E |
| html-safe-nonce | 71ba3c02f2cc3d05ea879b6278b760ed7174640a0e65c0ee597eaad85dbbe98d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0IwOjM4QzVDRjoyNzM2M0U4OjMxQzA4RkY6NkIwMTdBNkUiLCJ2aXNpdG9yX2lkIjoiMTYyNDEzODEwOTUwMTQwNTgwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3a4b94268cf8040e7cda8488d3c7b8a8d0c864ae3638bac4b7bcfba5ee23ec31 |
| hovercard-subject-tag | issue:596862857 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/444/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e5d861c2977f230356b16c26da3e7aab0211557d15a01dce4eafa26570df4605/sqlc-dev/sqlc/issues/444 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e5d861c2977f230356b16c26da3e7aab0211557d15a01dce4eafa26570df4605/sqlc-dev/sqlc/issues/444 |
| og:image:alt | I'm using sqlc together with a package that generates an http router based on an OpenAPI spec. I find that I need to do a lot of mapping between sqlc generated types and types for the http response... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tschaub |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git |
| octolytics-dimension-user_id | 136738596 |
| octolytics-dimension-user_login | sqlc-dev |
| octolytics-dimension-repository_id | 193160679 |
| octolytics-dimension-repository_nwo | sqlc-dev/sqlc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 193160679 |
| octolytics-dimension-repository_network_root_nwo | sqlc-dev/sqlc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width