Skip to content

Commit 05841be

Browse files
authored
Merge pull request #258 from aliyun/sortjson
sort json
2 parents 7f5eadc + 88a7d71 commit 05841be

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

openapi/commando.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/aliyun/aliyun-cli/i18n"
2121
"github.com/aliyun/aliyun-cli/meta"
2222

23-
"bytes"
2423
"encoding/json"
2524
"fmt"
2625
"io"
@@ -164,19 +163,23 @@ func (c *Commando) processInvoke(ctx *cli.Context, productCode string, apiOrMeth
164163
}
165164
}
166165

167-
out = FormatJson(out)
166+
out = sortJSON(out)
168167

169168
cli.Println(ctx.Writer(), out)
170169
return nil
171170
}
172171

173-
func FormatJson(content string) string {
174-
var buf bytes.Buffer
175-
err := json.Indent(&buf, []byte(content), "", "\t")
176-
if err == nil {
177-
content = buf.String()
172+
func sortJSON(content string) string {
173+
var v interface{}
174+
err := json.Unmarshal([]byte(content), &v)
175+
if err != nil {
176+
return content
177+
}
178+
data, err := json.MarshalIndent(v, "", "\t")
179+
if err != nil {
180+
return content
178181
}
179-
return content
182+
return string(data)
180183
}
181184

182185
// invoke with helper

openapi/commando_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ func Test_processInvoke(t *testing.T) {
206206
assert.Nil(t, err)
207207

208208
out := `{"requestid":"test","name":"json"}`
209-
out = FormatJson(out)
210-
assert.Equal(t, "{\n\t\"requestid\": \"test\",\n\t\"name\": \"json\"\n}", out)
209+
out = sortJSON(out)
210+
assert.Equal(t, "{\n\t\"name\": \"json\",\n\t\"requestid\": \"test\"\n}", out)
211211
}
212212

213213
func Test_help(t *testing.T) {

0 commit comments

Comments
 (0)