Skip to content

Commit 5da59f4

Browse files
night556JacksonTian
authored andcommitted
update flag --pager
1 parent a65a8d9 commit 5da59f4

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Master :
44

5+
- flag `--pager` can specified collections path
56
- add row number for flag `--output` output format.
67
> If you want show row number at output format, you can use field `num=ture` after flag `--output` to enable the num.
78

openapi/pager.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ package openapi
66
import (
77
"encoding/json"
88
"fmt"
9-
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
10-
"github.com/aliyun/aliyun-cli/cli"
11-
"github.com/aliyun/aliyun-cli/i18n"
12-
"github.com/jmespath/go-jmespath"
139
"math"
1410
"strconv"
1511
"strings"
12+
13+
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
14+
"github.com/aliyun/aliyun-cli/cli"
15+
"github.com/aliyun/aliyun-cli/i18n"
16+
jmespath "github.com/jmespath/go-jmespath"
1617
)
1718

1819
var PagerFlag = &cli.Flag{Category: "caller",
@@ -24,7 +25,7 @@ var PagerFlag = &cli.Flag{Category: "caller",
2425
"use `--pager` to merge pages for pageable APIs",
2526
"使用 `--pager` 在访问分页的API时合并结果分页"),
2627
Fields: []cli.Field{
27-
{Key: "", Required: false},
28+
{Key: "path", Required: false},
2829
{Key: "PageNumber", DefaultValue: "PageNumber", Short: i18n.T(" PageNumber", "指定PageNumber的属性")},
2930
{Key: "PageSize", DefaultValue: "PageSize", Short: i18n.T("PageSize", "")},
3031
{Key: "TotalCount", DefaultValue: "TotalCount", Short: i18n.T("TotalCount", "")},
@@ -60,7 +61,7 @@ func GetPager() *Pager {
6061
pager.PageSizeExpr, _ = PagerFlag.GetFieldValue("PageSize")
6162
pager.TotalCountExpr, _ = PagerFlag.GetFieldValue("TotalCount")
6263

63-
pager.collectionPath, _ = PagerFlag.GetFieldValue("")
64+
pager.collectionPath, _ = PagerFlag.GetFieldValue("path")
6465
return pager
6566
}
6667

@@ -124,25 +125,44 @@ func (a *Pager) GetResponseCollection() string {
124125

125126
func (a *Pager) FeedResponse(body string) error {
126127
var j interface{}
128+
127129
err := json.Unmarshal([]byte(body), &j)
128130
if err != nil {
129131
return fmt.Errorf("unmarshal %s", err.Error())
130132
}
131133

132134
if total, err := jmespath.Search(a.TotalCountExpr, j); err == nil {
133-
a.totalCount = int(total.(float64))
135+
var totalCount float64
136+
if strCount, ok := total.(string); ok {
137+
totalCount, _ = strconv.ParseFloat(strCount, 64)
138+
} else {
139+
totalCount = total.(float64)
140+
}
141+
a.totalCount = int(totalCount)
134142
} else {
135143
return fmt.Errorf("jmespath: '%s' failed %s", a.TotalCountExpr, err)
136144
}
137145

138146
if pageNumber, err := jmespath.Search(a.PageNumberExpr, j); err == nil {
139-
a.currentPageNumber = int(pageNumber.(float64))
147+
var currentPageNumber float64
148+
if strpageNumber, ok := pageNumber.(string); ok {
149+
currentPageNumber, _ = strconv.ParseFloat(strpageNumber, 64)
150+
} else {
151+
currentPageNumber = pageNumber.(float64)
152+
}
153+
a.currentPageNumber = int(currentPageNumber)
140154
} else {
141155
return fmt.Errorf("jmespath: '%s' failed %s", a.PageNumberExpr, err)
142156
}
143157

144158
if pageSize, err := jmespath.Search(a.PageSizeExpr, j); err == nil {
145-
a.PageSize = int(pageSize.(float64))
159+
var PageSize float64
160+
if strpageSize, ok := pageSize.(string); ok {
161+
PageSize, _ = strconv.ParseFloat(strpageSize, 64)
162+
} else {
163+
PageSize = pageSize.(float64)
164+
}
165+
a.PageSize = int(PageSize)
146166
} else {
147167
return fmt.Errorf("jmespath: '%s' failed %s", a.PageSizeExpr, err)
148168
}
@@ -155,7 +175,6 @@ func (a *Pager) FeedResponse(body string) error {
155175
a.collectionPath = p2
156176
}
157177
}
158-
159178
a.mergeCollections(j)
160179
return nil
161180
}

openapi/pager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
88
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
99
"github.com/aliyun/aliyun-cli/cli"
10-
"github.com/jmespath/go-jmespath"
10+
jmespath "github.com/jmespath/go-jmespath"
1111
"github.com/stretchr/testify/assert"
1212

1313
"bytes"
@@ -213,8 +213,8 @@ func Test_mergeCollections(t *testing.T) {
213213

214214
var pagerTestJson = []byte(`{
215215
"PageNumber": 5,
216-
"TotalCount": 37,
217-
"PageSize": 5,
216+
"TotalCount": "37",
217+
"PageSize": "5",
218218
"RegionId": "cn-beijing",
219219
"RequestId": "5CC3B94E-ED7E-4481-98D4-4330C5A8FCA8",
220220
"Images": {

0 commit comments

Comments
 (0)