Skip to content

Commit

Permalink
user generics and named attr
Browse files Browse the repository at this point in the history
  • Loading branch information
skarimo committed Nov 15, 2022
1 parent 23b99e1 commit a4c54f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
19 changes: 5 additions & 14 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 struct { {{ itemType }}; error }, 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(), error) {
ctx, cancel := _context.WithCancel(ctx)

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

items := make(chan struct { {{ itemType }}; error }, 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 <- struct {
{{ itemType }}
error
}{returnItem, err}
items <- datadog.PaginationResult[{{ itemType }}]{returnItem, err}
break
}

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

for _, item := range results {
select {
case items <- struct {
{{ itemType }}
error
}{item, nil}:
case items <- datadog.PaginationResult[{{ itemType }}]{item, nil}:
case <-ctx.Done():
close(items)
return
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 go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/DataDog/datadog-api-client-go/v2

go 1.17
go 1.18

retract (
// Version used to retract v2.0.0 and v2.0.1. DO NOT USE.
Expand Down
4 changes: 2 additions & 2 deletions tests/api/datadogV2/api_processes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGetProcessesWithPaginationReturnsError(t *testing.T) {
From: datadog.PtrInt64(1000),
To: datadog.PtrInt64(1000),
})

item := <-resp
assert.Equal(item.Error(), "400 Bad Request")
assert.Error(item.Error)
assert.Equal(item.Error.Error(), "400 Bad Request")
}

0 comments on commit a4c54f4

Please sign in to comment.