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

tools/generator-go-sdk: generate custom pager for list method #3958

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 35 additions & 2 deletions tools/generator-go-sdk/internal/generator/templater_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (c methodsPandoraTemplater) listOperationTemplate(data ServiceGeneratorData
if err != nil {
return nil, fmt.Errorf("building arguments for list operation: %+v", err)
}
customPagerStruct := c.requestOptionStruct()
requestOptions, err := c.requestOptions()
if err != nil {
return nil, fmt.Errorf("building request config: %+v", err)
Expand All @@ -298,6 +299,7 @@ func (c methodsPandoraTemplater) listOperationTemplate(data ServiceGeneratorData
templated := fmt.Sprintf(`
%[6]s
%[7]s
%[8]s

// %[2]s ...
func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse, err error) {
Expand All @@ -322,7 +324,7 @@ func (c %[1]s) %[2]s(ctx context.Context %[3]s) (result %[2]sOperationResponse,

return
}
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *unmarshalerCode, *responseStruct, *optionsStruct)
`, data.serviceClientName, c.operationName, *methodArguments, *requestOptions, *unmarshalerCode, *responseStruct, *optionsStruct, customPagerStruct)

// Only output predicate functions for models and not for base types like string, int etc.
if c.operation.ResponseObject.Type == models.ReferenceSDKObjectDefinitionType || c.operation.ResponseObject.Type == models.ListSDKObjectDefinitionType {
Expand Down Expand Up @@ -474,6 +476,31 @@ func (c methodsPandoraTemplater) argumentsTemplateForMethod(data ServiceGenerato
return &out, nil
}

// define struct used in requestOptions
func (c methodsPandoraTemplater) requestOptionStruct() string {
var output string

if c.operation.FieldContainingPaginationDetails != nil {
jsonTag := fmt.Sprintf("`json:%q`", *c.operation.FieldContainingPaginationDetails)

output = fmt.Sprintf(`
type %[2]sCustomPager struct {
NextLink *odata.Link %[1]s
}

func (p *%[2]sCustomPager) NextPageLink() *odata.Link {
defer func() {
p.NextLink = nil
}()

return p.NextLink
}
`, jsonTag, c.operationName)
}

return output
}

func (c methodsPandoraTemplater) requestOptions() (*string, error) {
method := capitalizeFirstLetter(c.operation.Method)
expectedStatusCodes := make([]string, 0)
Expand All @@ -500,6 +527,11 @@ func (c methodsPandoraTemplater) requestOptions() (*string, error) {
options = "OptionsObject: options,"
}

customPagerOption := ""
if c.operation.FieldContainingPaginationDetails != nil {
customPagerOption = fmt.Sprintf("Pager: &%sCustomPager{},", c.operationName)
}

contentType := c.operation.ContentType

out := fmt.Sprintf(`client.RequestOptions{
Expand All @@ -508,10 +540,11 @@ func (c methodsPandoraTemplater) requestOptions() (*string, error) {
%[2]s,
},
HttpMethod: http.Method%[3]s,
%[6]s
Path: %[4]s,
%[5]s
}
`, contentType, strings.Join(expectedStatusCodes, ",\n\t\t\t"), method, path, options)
`, contentType, strings.Join(expectedStatusCodes, ",\n\t\t\t"), method, path, options, customPagerOption)
return &out, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestTemplateMethods_Discriminator_ResponseObjectIsImplementation_Get(t *tes
},
operationName: "Get",
}.template(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -157,7 +156,6 @@ func TestTemplateMethods_Discriminator_ResponseObjectIsParent_Get(t *testing.T)
},
operationName: "Get",
}.template(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -267,7 +265,6 @@ func TestTemplateMethods_Discriminator_ResponseObjectIsImplementation_List(t *te
},
operationName: "List",
}.template(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -299,6 +296,18 @@ type ListCompleteResult struct {
Items []PandaPop
}

type ListCustomPager struct {
NextLink *odata.Link %[2]s
}

func (p *ListCustomPager) NextPageLink() *odata.Link {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that's mostly for testing purposes, so this is fine. If NextPageLink returns nil then we assume we're at the end of the dataset, so that's fine/should be logged elsewhere

defer func() {
p.NextLink = nil
}()

return p.NextLink
}

// List ...
func (c pandaClient) List(ctx context.Context ) (result ListOperationResponse, err error) {
opts := client.RequestOptions{
Expand All @@ -307,6 +316,7 @@ func (c pandaClient) List(ctx context.Context ) (result ListOperationResponse, e
http.StatusOK,
},
HttpMethod: http.MethodGet,
Pager: &ListCustomPager{},
Path: "/thing",
}

Expand Down Expand Up @@ -363,7 +373,7 @@ func (c pandaClient) ListCompleteMatchingPredicate(ctx context.Context, predicat
}
return
}
`, "`json:\"value\"`")
`, "`json:\"value\"`", "`json:\"someField\"`")

assertTemplatedCodeMatches(t, expected, *actual)
}
Expand Down Expand Up @@ -409,7 +419,6 @@ func TestTemplateMethods_Discriminator_ResponseObjectIsParent_List(t *testing.T)
},
operationName: "List",
}.template(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -441,6 +450,18 @@ type ListCompleteResult struct {
Items []FizzyDrink
}

type ListCustomPager struct {
NextLink *odata.Link %[2]s
}

func (p *ListCustomPager) NextPageLink() *odata.Link {
defer func() {
p.NextLink = nil
}()

return p.NextLink
}

// List ...
func (c pandaClient) List(ctx context.Context ) (result ListOperationResponse, err error) {
opts := client.RequestOptions{
Expand All @@ -449,6 +470,7 @@ func (c pandaClient) List(ctx context.Context ) (result ListOperationResponse, e
http.StatusOK,
},
HttpMethod: http.MethodGet,
Pager: &ListCustomPager{},
Path: "/thing",
}

Expand Down Expand Up @@ -514,7 +536,7 @@ func (c pandaClient) ListCompleteMatchingPredicate(ctx context.Context, predicat
}
return
}
`, "`json:\"value\"`")
`, "`json:\"value\"`", "`json:\"SomeField\"`")

assertTemplatedCodeMatches(t, expected, *actual)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestTemplateMethodsGet(t *testing.T) {
},
operationName: "Get",
}.immediateOperationTemplate(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -111,7 +110,6 @@ func TestTemplateMethodsGetAsTextPowerShell(t *testing.T) {
},
operationName: "Get",
}.immediateOperationTemplate(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand Down Expand Up @@ -221,7 +219,6 @@ func TestTemplateMethodsListWithDiscriminatedType(t *testing.T) {
},
operationName: "List",
}.listOperationTemplate(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand All @@ -238,6 +235,18 @@ type ListCompleteResult struct {
Items []Bottle
}

type ListCustomPager struct {
NextLink *odata.Link %[2]s
}

func (p *ListCustomPager) NextPageLink() *odata.Link {
defer func() {
p.NextLink = nil
}()

return p.NextLink
}

// List ...
func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperationResponse, err error) {
opts := client.RequestOptions{
Expand All @@ -246,6 +255,7 @@ func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperati
http.StatusOK,
},
HttpMethod: http.MethodGet,
Pager: &ListCustomPager{},
Path: fmt.Sprintf("%%s/pandas", id.ID()),
}

Expand Down Expand Up @@ -316,13 +326,12 @@ func (c pandaClient) ListCompleteMatchingPredicate(ctx context.Context, id Panda
}
return
}
`, "`json:\"value\"`")
`, "`json:\"value\"`", "`json:\"nextLink\"`")

assertTemplatedCodeMatches(t, expected, *actual)
}

func TestTemplateMethodsListWithSimpleType(t *testing.T) {

input := ServiceGeneratorData{
packageName: "chubbyPandas",
serviceClientName: "pandaClient",
Expand All @@ -348,7 +357,6 @@ func TestTemplateMethodsListWithSimpleType(t *testing.T) {
},
operationName: "List",
}.listOperationTemplate(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand All @@ -365,6 +373,18 @@ type ListCompleteResult struct {
Items []string
}

type ListCustomPager struct {
NextLink *odata.Link %[2]s
}

func (p *ListCustomPager) NextPageLink() *odata.Link {
defer func() {
p.NextLink = nil
}()

return p.NextLink
}

// List ...
func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperationResponse, err error) {
opts := client.RequestOptions{
Expand All @@ -373,6 +393,7 @@ func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperati
http.StatusOK,
},
HttpMethod: http.MethodGet,
Pager: &ListCustomPager{},
Path: fmt.Sprintf("%%s/pandas", id.ID()),
}

Expand Down Expand Up @@ -425,7 +446,7 @@ func (c pandaClient) ListComplete(ctx context.Context, id PandaPop) (result List
}
return
}
`, "`json:\"value\"`")
`, "`json:\"value\"`", "`json:\"nextLink\"`")

assertTemplatedCodeMatches(t, expected, *actual)
}
Expand Down Expand Up @@ -457,7 +478,6 @@ func TestTemplateMethodsListWithObject(t *testing.T) {
},
operationName: "List",
}.listOperationTemplate(input)

if err != nil {
t.Fatalf("err %+v", err)
}
Expand All @@ -474,6 +494,18 @@ type ListCompleteResult struct {
Items []LingLing
}

type ListCustomPager struct {
NextLink *odata.Link %[2]s
}

func (p *ListCustomPager) NextPageLink() *odata.Link {
defer func() {
p.NextLink = nil
}()

return p.NextLink
}

// List ...
func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperationResponse, err error) {
opts := client.RequestOptions{
Expand All @@ -482,6 +514,7 @@ func (c pandaClient) List(ctx context.Context , id PandaPop) (result ListOperati
http.StatusOK,
},
HttpMethod: http.MethodGet,
Pager: &ListCustomPager{},
Path: fmt.Sprintf("%%s/pandas", id.ID()),
}

Expand Down Expand Up @@ -541,7 +574,7 @@ func (c pandaClient) ListCompleteMatchingPredicate(ctx context.Context, id Panda
}
return
}
`, "`json:\"value\"`")
`, "`json:\"value\"`", "`json:\"nextLink\"`")

assertTemplatedCodeMatches(t, expected, *actual)
}
Loading