-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generate constructor and builder methods for DTOs (#2001)
* Generate constructor and builder methods for DTOs * Refactor a bit * Move example files to separate dir * Add go build exclude directive
- Loading branch information
1 parent
5c6fdbe
commit 79d9c9c
Showing
5 changed files
with
473 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
## SDK DTO builder generation | ||
|
||
Short PoC of generating DTO constructors and builder methods. | ||
|
||
### Description | ||
|
||
There is an example file ready for generation [pipes_dto.go](example/pipes_dto.go) which creates file [pipes_dto_generated.go](example/pipes_dto_generated.go). | ||
|
||
Required fields should be marked with `// required` comment. | ||
|
||
To mark file inside `pkg/sdk/` directory as ready for generation add to the file: `//go:generate go run ./dto-builder-generator/main.go`. | ||
|
||
Output file will contain the same set of imports as the input file and will be formatted. | ||
|
||
### Usage | ||
|
||
To invoke example generation run: | ||
```shell | ||
go generate pkg/sdk/dto-builder-generator/example/pipes_dto.go | ||
``` | ||
|
||
To invoke all generations run: | ||
```shell | ||
make generate-all-dto | ||
``` | ||
|
||
To invoke only generation of given resource (e.g. pipes), run: | ||
```shell | ||
make generate-dto-pipes | ||
``` | ||
|
||
### Next steps | ||
- if comments are not enough, use different method to mark required fields (e.g. struct tags) | ||
- generate mappings between dto and Options struct | ||
- add more meta info to generated file header comment (e.g. time of generation etc.) |
This file contains 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,56 @@ | ||
package example | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
) | ||
|
||
//go:generate go run ../main.go | ||
|
||
type CreatePipeRequest struct { | ||
orReplace bool | ||
ifNotExists bool | ||
name sdk.SchemaObjectIdentifier // required | ||
autoIngest bool | ||
errorIntegration string | ||
awsSnsTopic string | ||
integration string | ||
comment string | ||
copyStatement string // required | ||
exampleOfImport bytes.Buffer | ||
} | ||
|
||
type AlterPipeRequest struct { | ||
ifExists bool | ||
name sdk.SchemaObjectIdentifier // required | ||
set PipeSetRequest | ||
unset PipeUnsetRequest | ||
setTags PipeSetTagsRequest | ||
unsetTags PipeUnsetTagsRequest | ||
refresh PipeRefreshRequest | ||
} | ||
|
||
type PipeSetRequest struct { | ||
errorIntegration string | ||
pipeExecutionPaused bool | ||
comment string | ||
} | ||
|
||
type PipeUnsetRequest struct { | ||
pipeExecutionPaused bool | ||
comment bool | ||
} | ||
|
||
type PipeSetTagsRequest struct { | ||
tag []sdk.TagAssociation // required | ||
} | ||
|
||
type PipeUnsetTagsRequest struct { | ||
tag []sdk.ObjectIdentifier // required | ||
} | ||
|
||
type PipeRefreshRequest struct { | ||
prefix string | ||
modifiedAfter string | ||
} |
159 changes: 159 additions & 0 deletions
159
pkg/sdk/dto-builder-generator/example/pipes_dto_generated.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.