-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get rid of deprecated proto so we dont need GOLANG_PROTOBUF_REGISTRAT…
…ION_CONFLICT (#10533)
- Loading branch information
Showing
17 changed files
with
37 additions
and
104 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
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
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
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
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
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 |
---|---|---|
@@ -1,70 +1,17 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/golang/protobuf/proto" | ||
gogoproto "github.com/golang/protobuf/proto" | ||
"github.com/golang/protobuf/ptypes" | ||
pany "github.com/golang/protobuf/ptypes/any" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
// MessageToAny takes any given proto message msg and returns the marshalled bytes of the proto, and a url to the type | ||
// definition for the proto in the form of a *pany.Any, errors if nil or if the proto type doesnt exist or if there is | ||
// a marshalling error | ||
func MessageToAny(msg proto.Message) (*pany.Any, error) { | ||
if msg == nil { | ||
return nil, errors.New("MessageToAny: message cannot be nil") | ||
} | ||
name, err := protoToMessageName(msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Marshalls the message into bytes using the proto library, or gogoproto if proto errors | ||
buf, err := protoToMessageBytes(msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &pany.Any{ | ||
TypeUrl: name, | ||
Value: buf, | ||
}, nil | ||
} | ||
|
||
func AnyToMessage(a *pany.Any) (proto.Message, error) { | ||
var x ptypes.DynamicAny | ||
err := ptypes.UnmarshalAny(a, &x) | ||
return x.Message, err | ||
} | ||
|
||
func protoToMessageName(msg proto.Message) (string, error) { | ||
typeUrlPrefix := "type.googleapis.com/" | ||
|
||
potentialName := gogoproto.MessageName(msg) | ||
if potentialName != "" { | ||
return typeUrlPrefix + potentialName, nil | ||
} | ||
return "", fmt.Errorf("can't determine message name") | ||
} | ||
|
||
func protoToMessageBytes(msg proto.Message) ([]byte, error) { | ||
if b, err := protoToMessageBytesGolang(msg); err == nil { | ||
return b, nil | ||
} | ||
return protoToMessageBytesGogo(msg) | ||
} | ||
|
||
func protoToMessageBytesGogo(msg proto.Message) ([]byte, error) { | ||
b := gogoproto.NewBuffer(nil) | ||
b.SetDeterministic(true) | ||
err := b.Marshal(msg) | ||
return b.Bytes(), err | ||
func MessageToAny(msg proto.Message) (*anypb.Any, error) { | ||
return anypb.New(msg) | ||
} | ||
|
||
func protoToMessageBytesGolang(msg proto.Message) ([]byte, error) { | ||
b := proto.NewBuffer(nil) | ||
b.SetDeterministic(true) | ||
err := b.Marshal(msg) | ||
return b.Bytes(), err | ||
func AnyToMessage(a *anypb.Any) (proto.Message, error) { | ||
return anypb.UnmarshalNew(a, proto.UnmarshalOptions{}) | ||
} |