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

Introduce PaginationResult type to return errors in WithPagination methods #1755

Merged
merged 8 commits into from
Nov 18, 2022
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
12 changes: 8 additions & 4 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (a *{{ classname }}) {{ operation.operationId }}(ctx _context.Context{% for
{%- set pagination = operation["x-pagination"] %}
// {{ operation.operationId }}WithPagination provides a paginated version of {{ operation.operationId }} returning a channel with all items.
{%- set itemType = get_type_at_path(operation, pagination.resultsPath) %}
func (a *{{ classname }}) {{ operation.operationId }}WithPagination(ctx _context.Context{% for name, parameter in operation|parameters if parameter.required or parameter.in == "path" %}, {{ name|variable_name}} {{ get_type_for_parameter(parameter) }}{% endfor %}{%- for name, parameter in operation|parameters if not parameter.required and parameter.in != "path" %}{%- if loop.first %}, o ...{{ operation.operationId }}OptionalParameters{% endif %}{% endfor %}) (<-chan {{ itemType }}, func(), error) {
func (a *{{ classname }}) {{ operation.operationId }}WithPagination(ctx _context.Context{% for name, parameter in operation|parameters if parameter.required or parameter.in == "path" %}, {{ name|variable_name}} {{ get_type_for_parameter(parameter) }}{% endfor %}{%- for name, parameter in operation|parameters if not parameter.required and parameter.in != "path" %}{%- if loop.first %}, o ...{{ operation.operationId }}OptionalParameters{% endif %}{% endfor %}) (<-chan datadog.PaginationResult[{{ itemType }}], func()) {
ctx, cancel := _context.WithCancel(ctx)

{%- set pageSizeType = get_container_type(operation, pagination.limitParam) %}
Expand Down Expand Up @@ -141,16 +141,20 @@ func (a *{{ classname }}) {{ operation.operationId }}WithPagination(ctx _context
}
{%- endfor %}

items := make(chan {{ itemType }}, pageSize_)
items := make(chan datadog.PaginationResult[{{ itemType }}], pageSize_)
go func() {
for {
req, err := a.build{{ operation.operationId }}Request(ctx{% for name, parameter in operation|parameters if parameter.required or parameter.in == "path" %}, {{ name|variable_name}}{% endfor %}{%- for name, parameter in operation|parameters if not parameter.required and parameter.in != "path" %}{%- if loop.first %}, o...{% endif %}{% endfor %})
if err != nil {
var returnItem {{ itemType }}
items <- datadog.PaginationResult[{{ itemType }}]{returnItem, err}
break
}

resp, _, err := a.{{ operation.operationId|untitle_case }}Execute(req)
if err != nil {
var returnItem {{ itemType }}
items <- datadog.PaginationResult[{{ itemType }}]{returnItem, err}
break
}
{%- set previous = {"part": ""} %}
Expand All @@ -167,7 +171,7 @@ func (a *{{ classname }}) {{ operation.operationId }}WithPagination(ctx _context

for _, item := range results {
select {
case items <- item:
case items <- datadog.PaginationResult[{{ itemType }}]{item, nil}:
case <-ctx.Done():
close(items)
return
Expand Down Expand Up @@ -208,7 +212,7 @@ func (a *{{ classname }}) {{ operation.operationId }}WithPagination(ctx _context
}
close(items)
}()
return items, cancel, nil
return items, cancel
}
{%- endif %}

Expand Down
15 changes: 7 additions & 8 deletions .generator/src/generator/templates/example.j2
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ func main() {
apiClient := datadog.NewAPIClient(configuration)
api := datadog{{ version|upper }}.New{{ context.api_instance.name }}Api(apiClient)
{%- if context.pagination %}
resp, _, err := api.{{ context.api_request.operation_id }}WithPagination(ctx{% if parameters %}, {{ parameters }}{% endif %})
resp, _ := api.{{ context.api_request.operation_id }}WithPagination(ctx{% if parameters %}, {{ parameters }}{% endif %})

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `{{ context.api_instance.name }}Api.{{ context.api_request.operation_id }}`: %v\n", err)
}

for item := range resp {
responseContent, _ := json.MarshalIndent(item, "", " ")
fmt.Fprintf(os.Stdout, "%s\n", responseContent)
for paginationResult := range resp {
if paginationResult.Error != nil {
fmt.Fprintf(os.Stderr, "Error when calling `{{ context.api_instance.name }}Api.{{ context.api_request.operation_id }}`: %v\n", paginationResult.Error)
}
responseContent, _ := json.MarshalIndent(paginationResult.Item, "", " ")
fmt.Fprintf(os.Stdout, "%s\n", responseContent)
}
{%- else %}
{%- if response_has_three_values %}
Expand Down
6 changes: 6 additions & 0 deletions .generator/src/generator/templates/utils.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func PtrString(v string) *string { return &v }
// PtrTime is helper routine that returns a pointer to given Time value.
func PtrTime(v time.Time) *time.Time { return &v }

// PaginationResult pagination item helper struct
type PaginationResult[T any] struct {
Item T
Error error
}

// NullableBool is a struct to hold a nullable boolean value.
type NullableBool struct {
value *bool
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.17.11
go-version: 1.18.x

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x, 1.18.x]
go-version: [1.18.x, 1.19.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
if: github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.17.x
go-version: 1.18.x
cache: true
cache-dependency-path: tests/go.sum
- name: Run integration tests
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ func main() {
apiClient := datadog.NewAPIClient(configuration)
incidentsApi := datadogV2.NewIncidentsApi(apiClient)

resp, _, err := incidentsApi.ListIncidentsWithPagination(ctx, *datadog.NewListIncidentsOptionalParameters())
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `IncidentsApi.ListIncidents`: %v\n", err)
}

for incident := range resp {
fmt.Fprintf(os.Stdout, "Got incident %s\n", incident.GetId())
resp, _ := incidentsApi.ListIncidentsWithPagination(ctx, *datadog.NewListIncidentsOptionalParameters())
for paginationResult := range resp {
if paginationResult.Error != nil {
fmt.Fprintf(os.Stderr, "Error when calling `IncidentsApi.ListIncidentsWithPagination`: %v\n", paginationResult.Error)
}
responseContent, _ := json.MarshalIndent(paginationResult.Item, "", " ")
fmt.Fprintf(os.Stdout, "%s\n", responseContent)
}

}
Expand Down
6 changes: 6 additions & 0 deletions api/datadog/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func PtrString(v string) *string { return &v }
// PtrTime is helper routine that returns a pointer to given Time value.
func PtrTime(v time.Time) *time.Time { return &v }

// PaginationResult pagination item helper struct
type PaginationResult[T any] struct {
Item T
Error error
}

// NullableBool is a struct to hold a nullable boolean value.
type NullableBool struct {
value *bool
Expand Down
24 changes: 16 additions & 8 deletions api/datadogV2/api_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (a *AuditApi) ListAuditLogs(ctx _context.Context, o ...ListAuditLogsOptiona
}

// ListAuditLogsWithPagination provides a paginated version of ListAuditLogs returning a channel with all items.
func (a *AuditApi) ListAuditLogsWithPagination(ctx _context.Context, o ...ListAuditLogsOptionalParameters) (<-chan AuditLogsEvent, func(), error) {
func (a *AuditApi) ListAuditLogsWithPagination(ctx _context.Context, o ...ListAuditLogsOptionalParameters) (<-chan datadog.PaginationResult[AuditLogsEvent], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int32(10)
if len(o) == 0 {
Expand All @@ -129,16 +129,20 @@ func (a *AuditApi) ListAuditLogsWithPagination(ctx _context.Context, o ...ListAu
}
o[0].PageLimit = &pageSize_

items := make(chan AuditLogsEvent, pageSize_)
items := make(chan datadog.PaginationResult[AuditLogsEvent], pageSize_)
go func() {
for {
req, err := a.buildListAuditLogsRequest(ctx, o...)
if err != nil {
var returnItem AuditLogsEvent
items <- datadog.PaginationResult[AuditLogsEvent]{returnItem, err}
break
}

resp, _, err := a.listAuditLogsExecute(req)
if err != nil {
var returnItem AuditLogsEvent
items <- datadog.PaginationResult[AuditLogsEvent]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
Expand All @@ -149,7 +153,7 @@ func (a *AuditApi) ListAuditLogsWithPagination(ctx _context.Context, o ...ListAu

for _, item := range results {
select {
case items <- item:
case items <- datadog.PaginationResult[AuditLogsEvent]{item, nil}:
case <-ctx.Done():
close(items)
return
Expand All @@ -175,7 +179,7 @@ func (a *AuditApi) ListAuditLogsWithPagination(ctx _context.Context, o ...ListAu
}
close(items)
}()
return items, cancel, nil
return items, cancel
}

// listAuditLogsExecute executes the request.
Expand Down Expand Up @@ -344,7 +348,7 @@ func (a *AuditApi) SearchAuditLogs(ctx _context.Context, o ...SearchAuditLogsOpt
}

// SearchAuditLogsWithPagination provides a paginated version of SearchAuditLogs returning a channel with all items.
func (a *AuditApi) SearchAuditLogsWithPagination(ctx _context.Context, o ...SearchAuditLogsOptionalParameters) (<-chan AuditLogsEvent, func(), error) {
func (a *AuditApi) SearchAuditLogsWithPagination(ctx _context.Context, o ...SearchAuditLogsOptionalParameters) (<-chan datadog.PaginationResult[AuditLogsEvent], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int32(10)
if len(o) == 0 {
Expand All @@ -361,16 +365,20 @@ func (a *AuditApi) SearchAuditLogsWithPagination(ctx _context.Context, o ...Sear
}
o[0].Body.Page.Limit = &pageSize_

items := make(chan AuditLogsEvent, pageSize_)
items := make(chan datadog.PaginationResult[AuditLogsEvent], pageSize_)
go func() {
for {
req, err := a.buildSearchAuditLogsRequest(ctx, o...)
if err != nil {
var returnItem AuditLogsEvent
items <- datadog.PaginationResult[AuditLogsEvent]{returnItem, err}
break
}

resp, _, err := a.searchAuditLogsExecute(req)
if err != nil {
var returnItem AuditLogsEvent
items <- datadog.PaginationResult[AuditLogsEvent]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
Expand All @@ -381,7 +389,7 @@ func (a *AuditApi) SearchAuditLogsWithPagination(ctx _context.Context, o ...Sear

for _, item := range results {
select {
case items <- item:
case items <- datadog.PaginationResult[AuditLogsEvent]{item, nil}:
case <-ctx.Done():
close(items)
return
Expand All @@ -407,7 +415,7 @@ func (a *AuditApi) SearchAuditLogsWithPagination(ctx _context.Context, o ...Sear
}
close(items)
}()
return items, cancel, nil
return items, cancel
}

// searchAuditLogsExecute executes the request.
Expand Down
24 changes: 16 additions & 8 deletions api/datadogV2/api_ci_visibility_pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEvents(ctx _context.Context,
}

// ListCIAppPipelineEventsWithPagination provides a paginated version of ListCIAppPipelineEvents returning a channel with all items.
func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEventsWithPagination(ctx _context.Context, o ...ListCIAppPipelineEventsOptionalParameters) (<-chan CIAppPipelineEvent, func(), error) {
func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEventsWithPagination(ctx _context.Context, o ...ListCIAppPipelineEventsOptionalParameters) (<-chan datadog.PaginationResult[CIAppPipelineEvent], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int32(10)
if len(o) == 0 {
Expand All @@ -251,16 +251,20 @@ func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEventsWithPagination(ctx _co
}
o[0].PageLimit = &pageSize_

items := make(chan CIAppPipelineEvent, pageSize_)
items := make(chan datadog.PaginationResult[CIAppPipelineEvent], pageSize_)
go func() {
for {
req, err := a.buildListCIAppPipelineEventsRequest(ctx, o...)
if err != nil {
var returnItem CIAppPipelineEvent
items <- datadog.PaginationResult[CIAppPipelineEvent]{returnItem, err}
break
}

resp, _, err := a.listCIAppPipelineEventsExecute(req)
if err != nil {
var returnItem CIAppPipelineEvent
items <- datadog.PaginationResult[CIAppPipelineEvent]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
Expand All @@ -271,7 +275,7 @@ func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEventsWithPagination(ctx _co

for _, item := range results {
select {
case items <- item:
case items <- datadog.PaginationResult[CIAppPipelineEvent]{item, nil}:
case <-ctx.Done():
close(items)
return
Expand All @@ -297,7 +301,7 @@ func (a *CIVisibilityPipelinesApi) ListCIAppPipelineEventsWithPagination(ctx _co
}
close(items)
}()
return items, cancel, nil
return items, cancel
}

// listCIAppPipelineEventsExecute executes the request.
Expand Down Expand Up @@ -464,7 +468,7 @@ func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEvents(ctx _context.Contex
}

// SearchCIAppPipelineEventsWithPagination provides a paginated version of SearchCIAppPipelineEvents returning a channel with all items.
func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEventsWithPagination(ctx _context.Context, o ...SearchCIAppPipelineEventsOptionalParameters) (<-chan CIAppPipelineEvent, func(), error) {
func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEventsWithPagination(ctx _context.Context, o ...SearchCIAppPipelineEventsOptionalParameters) (<-chan datadog.PaginationResult[CIAppPipelineEvent], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int32(10)
if len(o) == 0 {
Expand All @@ -481,16 +485,20 @@ func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEventsWithPagination(ctx _
}
o[0].Body.Page.Limit = &pageSize_

items := make(chan CIAppPipelineEvent, pageSize_)
items := make(chan datadog.PaginationResult[CIAppPipelineEvent], pageSize_)
go func() {
for {
req, err := a.buildSearchCIAppPipelineEventsRequest(ctx, o...)
if err != nil {
var returnItem CIAppPipelineEvent
items <- datadog.PaginationResult[CIAppPipelineEvent]{returnItem, err}
break
}

resp, _, err := a.searchCIAppPipelineEventsExecute(req)
if err != nil {
var returnItem CIAppPipelineEvent
items <- datadog.PaginationResult[CIAppPipelineEvent]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
Expand All @@ -501,7 +509,7 @@ func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEventsWithPagination(ctx _

for _, item := range results {
select {
case items <- item:
case items <- datadog.PaginationResult[CIAppPipelineEvent]{item, nil}:
case <-ctx.Done():
close(items)
return
Expand All @@ -527,7 +535,7 @@ func (a *CIVisibilityPipelinesApi) SearchCIAppPipelineEventsWithPagination(ctx _
}
close(items)
}()
return items, cancel, nil
return items, cancel
}

// searchCIAppPipelineEventsExecute executes the request.
Expand Down
Loading