-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Any helper functions (#92)
- Loading branch information
1 parent
47964f2
commit ad42ce8
Showing
4 changed files
with
116 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2008 Google Inc. All rights reserved. | ||
// https://developers.google.com/protocol-buffers/ | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
package any | ||
|
||
import ( | ||
"google.golang.org/protobuf/proto" | ||
protoimpl "google.golang.org/protobuf/runtime/protoimpl" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
// New marshals src into a new Any instance. | ||
func New(src proto.Message) (*anypb.Any, error) { | ||
dst := new(anypb.Any) | ||
if err := MarshalFrom(dst, src, proto.MarshalOptions{}); err != nil { | ||
return nil, err | ||
} | ||
return dst, nil | ||
} | ||
|
||
// MarshalFrom marshals src into dst as the underlying message | ||
// using the provided marshal options. | ||
// | ||
// If no options are specified, call dst.MarshalFrom instead. | ||
func MarshalFrom(dst *anypb.Any, src proto.Message, opts proto.MarshalOptions) error { | ||
if src == nil { | ||
return protoimpl.X.NewError("invalid nil source message") | ||
} | ||
b, err := opts.Marshal(src) | ||
if err != nil { | ||
return err | ||
} | ||
dst.TypeUrl = string(src.ProtoReflect().Descriptor().FullName()) | ||
dst.Value = b | ||
return nil | ||
} |
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,33 @@ | ||
package any_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
"github.com/cosmos/cosmos-proto/any" | ||
"github.com/cosmos/cosmos-proto/testpb" | ||
) | ||
|
||
func TestAny(t *testing.T) { | ||
value := &testpb.A{SomeBoolean: true} | ||
|
||
dst1 := &anypb.Any{} | ||
err := any.MarshalFrom(dst1, value, proto.MarshalOptions{}) | ||
require.NoError(t, err) | ||
require.Equal(t, "A", dst1.TypeUrl) // Make sure there's no "type.googleapis.com/" prefix. | ||
|
||
dst2, err := any.New(value) | ||
require.NoError(t, err) | ||
require.Equal(t, "A", dst2.TypeUrl) // Make sure there's no "type.googleapis.com/" prefix. | ||
|
||
// Round trip. | ||
newValue, err := anypb.UnmarshalNew(dst2, proto.UnmarshalOptions{}) | ||
require.NoError(t, err) | ||
diff := cmp.Diff(value, newValue, protocmp.Transform()) | ||
require.Empty(t, diff) | ||
} |
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,19 @@ | ||
// Package any is a helper library to be used as a replacement for some of the | ||
// official "google.golang.org/protobuf/types/known/anypb"'s functions which | ||
// prepend the "type.googleapis.com" prefix in type URLs. | ||
// | ||
// In the Cosmos SDK, an Any's TypeURL starts with a `/` character, e.g. | ||
// "/cosmos.bank.v1beta1.MsgSend". However, if we pack this MsgSend into | ||
// an anypb.Any using the offical anypb package's helper functions, we get a | ||
// TypeURL which is "type.googleapis.com/cosmos.bank.v1beta1.MsgSend". | ||
// | ||
// Therefore, the following 3 functions/methods cannot be used within the | ||
// Cosmos context: | ||
// - anypb.New | ||
// - anypb.MarshalFrom | ||
// - anypb.Any#MarshalFrom | ||
// as all of them append the unwanted prefix. | ||
// | ||
// This package exposes the `New` and `MarshalFrom` helper functions, which do | ||
// not prepend any prefix to type URLs. | ||
package any |