Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify mocking of iterator and waiter objects #769

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions .codegen/api.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type {{.PascalName}}Interface interface {
{{range .Methods}}
{{- $hasWaiter := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}}
{{if not .Pagination}}{{.Comment "// " 80}}
{{.PascalName}}(ctx context.Context{{if .Request}}, {{if $hasWaiter}}{{.Request.CamelName}}{{else}}request{{end}} {{.Request.PascalName}}{{end}}) {{if $hasWaiter}}(*{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[any]{{end}}, error){{else}}{{ template "response-type" .}}{{end}}
{{.PascalName}}(ctx context.Context{{if .Request}}, {{if $hasWaiter}}{{.Request.CamelName}}{{else}}request{{end}} {{.Request.PascalName}}{{end}}) {{if $hasWaiter}}(*{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[struct{}]{{end}}, error){{else}}{{ template "response-type" .}}{{end}}
{{end}}

{{if $hasWaiter}}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (a *{{.Method.Service.PascalName}}API) {{.PascalName}}(ctx context.Context{
type {{.PascalName}}[R any] struct {
Response *R{{range .Binding}}
{{.PollField.PascalName}} {{template "type" .PollField.Entity}} `json:"{{.PollField.Name}}"`{{end}}
poll func(time.Duration, func(*{{.Poll.Response.PascalName}})) (*{{.Poll.Response.PascalName}}, error)
Poll func(time.Duration, func(*{{.Poll.Response.PascalName}})) (*{{.Poll.Response.PascalName}}, error)
callback func(*{{.Poll.Response.PascalName}})
timeout time.Duration
}
Expand All @@ -187,29 +187,29 @@ func (w *{{.PascalName}}[R]) OnProgress(callback func(*{{.Poll.Response.PascalNa

// Get the {{.Poll.Response.PascalName}} with the default timeout of {{.Timeout}} minutes.
func (w *{{.PascalName}}[R]) Get() (*{{.Poll.Response.PascalName}}, error) {
return w.poll(w.timeout, w.callback)
return w.Poll(w.timeout, w.callback)
}

// Get the {{.Poll.Response.PascalName}} with custom timeout.
func (w *{{.PascalName}}[R]) GetWithTimeout(timeout time.Duration) (*{{.Poll.Response.PascalName}}, error) {
return w.poll(timeout, w.callback)
return w.Poll(timeout, w.callback)
}
{{end}}

{{range .Methods}}
{{- $hasWaiter := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}}
{{if not .Pagination}}{{.Comment "// " 80}}
func (a *{{.Service.PascalName}}API) {{.PascalName}}(ctx context.Context{{if .Request}}, {{if $hasWaiter}}{{.Request.CamelName}}{{else}}request{{end}} {{.Request.PascalName}}{{end}}) {{if $hasWaiter}}(*{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[any]{{end}}, error){{else}}{{ template "response-type" .}}{{end}} {
func (a *{{.Service.PascalName}}API) {{.PascalName}}(ctx context.Context{{if .Request}}, {{if $hasWaiter}}{{.Request.CamelName}}{{else}}request{{end}} {{.Request.PascalName}}{{end}}) {{if $hasWaiter}}(*{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[struct{}]{{end}}, error){{else}}{{ template "response-type" .}}{{end}} {
{{if $hasWaiter -}}
{{if .Response}}{{.Response.CamelName}}, {{end}}err := a.impl.{{.PascalName}}(ctx{{if .Request}}, {{.Request.CamelName}}{{end}})
if err != nil {
return nil, err
}
return &{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[any]{{end}}{
return &{{.Wait.PascalName}}{{with .Response}}[{{.PascalName}}]{{else}}[struct{}]{{end}}{
{{with .Response}}Response: {{.CamelName}},
{{- end}}{{range .Wait.Binding}}
{{.PollField.PascalName}}: {{.Bind.Of.CamelName}}.{{.Bind.PascalName}},{{end}}
poll: func(timeout time.Duration, callback func(*{{.Wait.Poll.Response.PascalName}})) (*{{.Wait.Poll.Response.PascalName}}, error) {
Poll: func(timeout time.Duration, callback func(*{{.Wait.Poll.Response.PascalName}})) (*{{.Wait.Poll.Response.PascalName}}, error) {
return a.{{.Wait.PascalName}}(ctx{{range .Wait.Binding}}, {{.Bind.Of.CamelName}}.{{.Bind.PascalName}}{{end}}, timeout, callback)
},
timeout: {{.Wait.Timeout}}*time.Minute,
Expand Down Expand Up @@ -400,10 +400,8 @@ func (a *{{.Service.Name}}API) {{.Shortcut.PascalName}}AndWait(ctx context.Conte
{{- define "paginated-request-type" -}}
{{if .Request}}{{.Request.PascalName}}{{else}}struct{}{{end}}
{{- end -}}

{{/* Note: we return the interface to make it possible to mock list responses more easily. */}}
{{- define "paginated-return-type" -}}
{{ if .NeedsOffsetDedupe -}}
*listing.DeduplicatingIterator[{{ template "type" .Pagination.Entity }}, {{ template "type" .IdentifierField.Entity }}]
{{- else -}}
*listing.PaginatingIterator[{{ template "paginated-request-type" .}}, *{{ .Response.PascalName }}, {{ template "type" .Pagination.Entity }}]
{{- end -}}
listing.Iterator[{{ template "type" .Pagination.Entity }}]
{{- end -}}
12 changes: 6 additions & 6 deletions experimental/mocks/service/billing/mock_budgets_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions experimental/mocks/service/catalog/mock_catalogs_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions experimental/mocks/service/catalog/mock_connections_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading