Skip to content

feat: Implement type flattener#419

Merged
murarustefaan merged 6 commits intomainfrom
feat/implement-type-flattener
Mar 24, 2026
Merged

feat: Implement type flattener#419
murarustefaan merged 6 commits intomainfrom
feat/implement-type-flattener

Conversation

@murarustefaan
Copy link
Member

Adds a structs package and cmd/flatten CLI to codegen that generates optimized Go structs from deeply nested API models. Nested struct fields are replaced with map[string]any to eliminate the decode -> allocate -> re-encode cycle when the SDK serializes them as JSON columns.

Usage:

//go:generate go run github.com/cloudquery/codegen/cmd/flatten -source=../api/models.go -from=Finding -to=flatFinding -output=finding_generated.go -package=services -sort

Options: -sort (ID first, then alphabetical), -extra (prepend fields like AssetType), -package (output package name).

@murarustefaan murarustefaan requested a review from a team as a code owner March 24, 2026 09:26
@murarustefaan murarustefaan requested review from Copilot, erezrokah and stoovon and removed request for a team March 24, 2026 09:26
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new codegen utility to “flatten” Go API model structs into decode-efficient equivalents by replacing nested/complex field types with map[string]any, plus a cmd/flatten CLI wrapper for go:generate usage.

Changes:

  • Introduces structs.Flatten generator, type-resolution logic, options, and Go template for emitting flattened structs.
  • Adds a cmd/flatten CLI to drive generation from flags (-source, -from, -to, -output, -package, -sort, -extra).
  • Adds snapshot-based tests + testdata covering base behavior, multiple structs, extra fields, extra scalars, and sorting.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
structs/flatten.go Core generator: renders template, formats output, writes generated files, optional sorting.
structs/resolve.go AST parsing helpers, JSON tag extraction, and type-flattening rules.
structs/options.go Generator options (package name, header, scalar handling, sorting).
structs/templates/struct.go.tpl Template for the generated flattened struct definition.
structs/testdata/input.go Input models used by tests to validate flattening behavior.
structs/flatten_test.go Snapshot tests validating output and edge cases/errors.
structs/.snapshots/* Approved snapshots for generator output across scenarios.
cmd/flatten/main.go CLI wrapper for running the generator via go run / go:generate.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@murarustefaan murarustefaan merged commit de8ece6 into main Mar 24, 2026
9 checks passed
@murarustefaan murarustefaan deleted the feat/implement-type-flattener branch March 24, 2026 09:46
kodiakhq bot pushed a commit that referenced this pull request Mar 24, 2026
🤖 I have created a release *beep* *boop*
---


## [0.4.0](v0.3.37...v0.4.0) (2026-03-24)


### Features

* Implement type flattener ([#419](#419)) ([de8ece6](de8ece6))


### Bug Fixes

* **deps:** Update dependency go to v1.26.0 ([#414](#414)) ([94c3c5f](94c3c5f))
* **deps:** Update golang.org/x/exp digest to 3dfff04 ([#412](#412)) ([28e9880](28e9880))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.94.3 ([#410](#410)) ([144ce71](144ce71))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.94.4 ([#415](#415)) ([35c026e](35c026e))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.94.5 ([#416](#416)) ([4586c6d](4586c6d))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.94.6 ([#417](#417)) ([16902da](16902da))
* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.94.7 ([#418](#418)) ([71d532b](71d532b))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants