-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABG-897 Convert protobuf to/from JSON
At the moment, the SDK works on protobuf messages in a namespace that is different from the namespace used by Fabric, so the native Fabric JSON marshalling/unmarshalling cannot be applied. This change exposes JSON marshalling/unmarashalling of protobuf messages in the SDK namespace. Change-Id: I160234ad176b42345620495e2cda017d1d0837ec Signed-off-by: Aleksandar Likic <[email protected]>
- Loading branch information
Aleksandar Likic
committed
Aug 20, 2019
1 parent
bdd5eed
commit 7eeb143
Showing
21 changed files
with
155 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package protolator | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/golang/protobuf/proto" | ||
plator "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/tools/protolator" | ||
) | ||
|
||
// DeepMarshalJSON marshals msg to w as JSON, but instead of marshaling bytes fields which contain nested | ||
// marshaled messages as base64 (like the standard proto encoding), these nested messages are remarshaled | ||
// as the JSON representation of those messages. This is done so that the JSON representation is as non-binary | ||
// and human readable as possible. | ||
func DeepMarshalJSON(w io.Writer, msg proto.Message) error { | ||
return plator.DeepMarshalJSON(w, msg) | ||
} | ||
|
||
// DeepUnmarshalJSON takes JSON output as generated by DeepMarshalJSON and decodes it into msg | ||
// This includes re-marshaling the expanded nested elements to binary form | ||
func DeepUnmarshalJSON(r io.Reader, msg proto.Message) error { | ||
return plator.DeepUnmarshalJSON(r, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package protolator | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" | ||
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var cert = `-----BEGIN CERTIFICATE----- | ||
MIICYjCCAgmgAwIBAgIUB3CTDOU47sUC5K4kn/Caqnh114YwCgYIKoZIzj0EAwIw | ||
fzELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh | ||
biBGcmFuY2lzY28xHzAdBgNVBAoTFkludGVybmV0IFdpZGdldHMsIEluYy4xDDAK | ||
BgNVBAsTA1dXVzEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMTYxMDEyMTkzMTAw | ||
WhcNMjExMDExMTkzMTAwWjB/MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv | ||
cm5pYTEWMBQGA1UEBxMNU2FuIEZyYW5jaXNjbzEfMB0GA1UEChMWSW50ZXJuZXQg | ||
V2lkZ2V0cywgSW5jLjEMMAoGA1UECxMDV1dXMRQwEgYDVQQDEwtleGFtcGxlLmNv | ||
bTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABKIH5b2JaSmqiQXHyqC+cmknICcF | ||
i5AddVjsQizDV6uZ4v6s+PWiJyzfA/rTtMvYAPq/yeEHpBUB1j053mxnpMujYzBh | ||
MA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBQXZ0I9 | ||
qp6CP8TFHZ9bw5nRtZxIEDAfBgNVHSMEGDAWgBQXZ0I9qp6CP8TFHZ9bw5nRtZxI | ||
EDAKBggqhkjOPQQDAgNHADBEAiAHp5Rbp9Em1G/UmKn8WsCbqDfWecVbZPQj3RK4 | ||
oG5kQQIgQAe4OOKYhJdh3f7URaKfGTf492/nmRmtK+ySKjpHSrU= | ||
-----END CERTIFICATE----- | ||
` | ||
|
||
func TestMarshalBlock(t *testing.T) { | ||
|
||
capability1 := "V1_1_PVTDATA_EXPERIMENTAL" | ||
v2_0Capability := "V2_0" | ||
|
||
builder := &mocks.MockConfigBlockBuilder{ | ||
MockConfigGroupBuilder: mocks.MockConfigGroupBuilder{ | ||
ModPolicy: "Admins", | ||
MSPNames: []string{ | ||
"Org1MSP", | ||
"Org2MSP", | ||
}, | ||
OrdererAddress: "localhost:9999", | ||
RootCA: cert, | ||
ChannelCapabilities: []string{fab.V1_1Capability}, | ||
OrdererCapabilities: []string{fab.V1_1Capability, v2_0Capability}, | ||
ApplicationCapabilities: []string{fab.V1_2Capability, capability1}, | ||
}, | ||
Index: 0, | ||
LastConfigIndex: 0, | ||
} | ||
|
||
block := builder.Build() | ||
var buf bytes.Buffer | ||
err := DeepMarshalJSON(&buf, block) | ||
assert.Nil(t, err, "Error marshalling block") | ||
|
||
newBlock := &common.Block{} | ||
newBlock.Reset() | ||
err = DeepUnmarshalJSON(bytes.NewReader(buf.Bytes()), newBlock) | ||
assert.Nil(t, err, "Error unmarshalling block") | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27 h1:XA/VH+SzpYyukhgh7v2mTp8rZoKKITXR/x3FIizVEXs= | ||
github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27/go.mod h1:WCBAbTOdfhHhz7YXujeZMF7owC4tPb1naKFsgfUISjo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.