Skip to content

Commit 845e795

Browse files
committed
FAB-3: Fix Json output for empty network list
The generated pb.PeersMessage struct will be added "omitempty" tag automatically. So when marshal an empty PeersMessage, we get a "{}". Add a new tempoprary struct without "omitempty" to replace PeerMesssage. Now we can get "{"Peer": []}" Change-Id: I1fc310c951cf4e85a61660f2c45f700136dc5010 Signed-off-by: jiangyaoguo <[email protected]>
1 parent 0363e1d commit 845e795

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

bddtests/peer_cli.feature

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ Feature: Peer Command Line Interface
1616
When I execute "peer network list" in container vp0
1717
Then the command should complete successfully
1818
And stdout should contain JSON
19+
And I should get result with "{"Peers":[]}"
1920

2021
Scenario: List Peers when two are up
2122
Given we compose "docker-compose-2.yml"
2223
When I execute "peer network list" in container vp0
2324
Then the command should complete successfully
24-
And stdout should contain JSON with "peers" array of length 1
25+
And stdout should contain JSON with "Peers" array of length 1

bddtests/steps/peer_cli_impl.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def step_impl(context, stream, attribute, length):
6666
array = getAttribute(attribute, json)
6767
assertLength(array, int(length))
6868

69+
@then(u'I should get result with "{expectResult}"')
70+
def step_impl(context, expectResult):
71+
assert context.command["stdout"].strip('\n') == expectResult
72+
6973
def assertIsJson(data):
7074
assert isJson(data), "Data is not in JSON format"
7175

@@ -86,4 +90,4 @@ def getAttribute(attribute, json):
8690

8791
def assertLength(array, length):
8892
arrayLength = len(array)
89-
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)
93+
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)

peer/network/list.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func networkList() (err error) {
5858
return
5959
}
6060

61-
jsonOutput, _ := json.Marshal(peers)
61+
// The generated pb.PeersMessage struct will be added "omitempty" tag automatically.
62+
// But we still want to print it when pb.PeersMessage is empty.
63+
jsonOutput, _ := json.Marshal(struct{ Peers []*pb.PeerEndpoint }{append([]*pb.PeerEndpoint{}, peers.GetPeers()...)})
6264
fmt.Println(string(jsonOutput))
6365
return nil
6466
}

0 commit comments

Comments
 (0)