Title: Struct Overrides (proposal) · Issue #1176 · sqlc-dev/sqlc · GitHub
Open Graph Title: Struct Overrides (proposal) · Issue #1176 · sqlc-dev/sqlc
X Title: Struct Overrides (proposal) · Issue #1176 · sqlc-dev/sqlc
Description: I can see quite a few unresolved issues related to the customization of sqlc generated structs. I'm in a similar position where I have several custom struct tags and embedded structs in my product model that flattens out to a single Post...
Open Graph Description: I can see quite a few unresolved issues related to the customization of sqlc generated structs. I'm in a similar position where I have several custom struct tags and embedded structs in my product ...
X Description: I can see quite a few unresolved issues related to the customization of sqlc generated structs. I'm in a similar position where I have several custom struct tags and embedded structs in my prod...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/1176
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Struct Overrides (proposal)","articleBody":"I can see quite a few unresolved issues related to the customization of sqlc generated structs.\r\n\r\nI'm in a similar position where I have several custom struct tags and embedded structs in my product model that flattens out to a single Postgres table, e.g.:\r\n\r\n```Go\r\npackage models\r\n\r\n//Product is a Postgres table, parts of which are imported from the remote ERP and another remote website.\r\n//All fields of ERPProduct and WebProduct are contained in the Postgres table\r\ntype Product struct {\r\n\tERPProduct //note: embedded ERPProduct.ProductCode is primary key!\r\n\tWebProduct\r\n\tCulture string `json:\"culture\" pg:\"culture\"` \r\n\tIsEnabled bool `json:\"isEnabled,omitempty\" pg:\"is_enabled\"`\r\n}\r\n\r\n//ERPProduct represents an ERP product record from the remote SQL Server\r\ntype ERPProduct struct {\r\n\tProductCode string `json:\"productCode\" db:\"ProductCode\" pg:\"product_code\"` //db tag is for ERP SQL Server\r\n\tCategory string `json:\"categoryCode,omitempty\" db:\"Category\" pg:\"category\"`\r\n\t//...other columns...\r\n}\r\n\r\n//Import will connect to the ERP and import an ERPProduct from an SQL Server\r\nfunc (p *ERPProduct) Import() (error) {\r\n //code to import and populate p using sqlx\r\n return nil\r\n}\r\n\r\n//WebProduct represents a web product \r\ntype WebProduct struct {\r\n\tWebCode string `json:\"webcode,omitempty\" pg:\"web_code\"`\r\n\tWebDescription string `json:\"webDescription,omitempty\" pg:\"web_description\"`\r\n\tSlug string `json:\"slug,omitempty\" pg:\"slug\"`\r\n\t//...other columns...\r\n}\r\n\r\n//Import will connect to another remote website and import a WebProduct via an API\r\nfunc (p *WebProduct) Import() (error) {\r\n //code to import and populate p \r\n return nil\r\n}\r\n```\r\nAs you can see, there are too many issues with this product model to port across to sqlc.\r\n\r\nHowever, I have an idea that might solve all of those issues in one foul swoop.. :)\r\n\r\nWe can currently override fields like this:\r\n\r\n```YAML\r\nversion: \"1\"\r\npackages: [...]\r\noverrides:\r\n - go_type: \"github.com/gofrs/uuid.UUID\"\r\n db_type: \"uuid\"\r\n ```\r\nCould we not do the same for structs? i.e. override the generation of the 'product' struct with my custom product struct like this:\r\n\r\n```YAML\r\nversion: \"1\"\r\npackages: [...]\r\noverrides:\r\n - go_struct: \"github.com/me/myproj/models.Product\"\r\n db_struct: \"Product\"\r\n```\r\nThe flattenned out 'product' definition would still be required in the sqlc schema file (only for validation in sqlc generated queries):\r\n```SQL\r\ncreate table products\r\n(\r\n product_code text primary key,\r\n category text,\r\n ...\r\n webcode text,\r\n ...\r\n culture\t text,\r\n is_enabled boolean default true not null\r\n);\r\n```\r\nsqlc would then generate the struct of the 'product' schema but comment it out with an 'overriden by..' comment above it.\r\n\r\nsqlc would then import `github.com/me/myproj/models` in the `query.sql.go` file and use the `Product` struct in there.\r\n\r\nAny product related query errors thereafter will be the responsibility of the developer...\r\n","author":{"url":"https://github.com/nkev","@type":"Person","name":"nkev"},"datePublished":"2021-09-11T15:19:33Z","url":"https://github.com/sqlc-dev/sqlc/issues/1176"}
| 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:eeb87b4b-74dd-fde9-9a81-cceda891e460 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E036:6DC60:186A61A:1EB7057:6B017688 |
| html-safe-nonce | eb73693d0b0f803f8e60cd31245fa2eac1d1aeee18cb3dde7355c0dd3b10c655 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDM2OjZEQzYwOjE4NkE2MUE6MUVCNzA1Nzo2QjAxNzY4OCIsInZpc2l0b3JfaWQiOiIyMTU0NzUxNTMzMzI0MDA3MDQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4de97576ede1c8134888a500ed79650f482cb60a3ddce489f16461163f3b5d75 |
| hovercard-subject-tag | issue:993864868 |
| 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/1176/issue_layout |
| twitter:image | https://opengraph.githubassets.com/32684a1686fe44ffa32a4825a1c0596185c18ee2376eea4c5448b4e6519ceaa2/sqlc-dev/sqlc/issues/1176 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/32684a1686fe44ffa32a4825a1c0596185c18ee2376eea4c5448b4e6519ceaa2/sqlc-dev/sqlc/issues/1176 |
| og:image:alt | I can see quite a few unresolved issues related to the customization of sqlc generated structs. I'm in a similar position where I have several custom struct tags and embedded structs in my product ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nkev |
| 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