Skip to content

Commit

Permalink
feat: Generate constructor and builder methods for DTOs (#2001)
Browse files Browse the repository at this point in the history
* Generate constructor and builder methods for DTOs

* Refactor a bit

* Move example files to separate dir

* Add go build exclude directive
  • Loading branch information
sfc-gh-asawicki committed Aug 11, 2023
1 parent 5c6fdbe commit 79d9c9c
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ lint-fix: ## Run static code analysis, check formatting and try to fix findings

pre-push: mod-check docs-check lint-check; ## Run a few checks before pushing a change (docs, fmt, mod, etc.)
.PHONY: pre-push

generate-all-dto:
go generate ./pkg/sdk/*_dto.go
.PHONY: generate-all

generate-dto-%: ./pkg/sdk/%_dto.go
go generate $<
35 changes: 35 additions & 0 deletions pkg/sdk/dto-builder-generator/README.md
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.)
56 changes: 56 additions & 0 deletions pkg/sdk/dto-builder-generator/example/pipes_dto.go
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 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.

0 comments on commit 79d9c9c

Please sign in to comment.