Last active
March 5, 2024 05:56
-
-
Save Jeongseup/54734ea372c0870a5b1905e819066fec to your computer and use it in GitHub Desktop.
Golang Anypb Testing Code
This file contains hidden or 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
module example.com | |
go 1.21.0 | |
require ( | |
github.com/cosmos/gogoproto v1.4.11 | |
github.com/gogo/protobuf v1.3.2 | |
github.com/golang/protobuf v1.5.3 | |
google.golang.org/protobuf v1.31.0 | |
) | |
require ( | |
github.com/google/go-cmp v0.5.9 // indirect | |
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect | |
) |
This file contains hidden or 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
# Reademe |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"log" | |
"testing" | |
"github.com/golang/protobuf/ptypes" | |
"google.golang.org/protobuf/proto" | |
"google.golang.org/protobuf/types/known/anypb" | |
) | |
func Test_ExampleMessage2(t *testing.T) { | |
// Create an ExampleMessage | |
person := Person{ | |
Name: "John", | |
Age: 30, | |
} | |
anyData, err := ptypes.MarshalAny(&person) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Print the Any message | |
fmt.Printf("message: %v\n", anyData) | |
myMessage := &MyMessage{ | |
Data: anyData, | |
} | |
out, err := proto.Marshal(myMessage) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("encoded message: %v\n", out) | |
newMessage := &MyMessage{} | |
if err := proto.Unmarshal(out, newMessage); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("decoded message: %v\n", newMessage) | |
// Any 데이터 추출 | |
encodedAnyData := newMessage.GetData() | |
// Any 데이터를 Person으로 디코딩 | |
newPerson := &Person{} | |
if err := ptypes.UnmarshalAny(encodedAnyData, newPerson); err != nil { | |
log.Fatal(err) | |
} | |
// 결과 출력 | |
fmt.Printf("Name: %s, Age: %d\n", newPerson.Name, newPerson.Age) | |
} | |
func Test_ExampleMessage(t *testing.T) { | |
// Create an ExampleMessage | |
msg := &ExampleMessage{ | |
Data: "Hello, World!", | |
} | |
// Marshal the ExampleMessage into an Any message | |
anyData, err := anypb.New(msg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Print the Any message | |
fmt.Printf("Any message: %v\n", anyData) | |
// Unmarshal the Any message back into an ExampleMessage | |
var unpackedMsg ExampleMessage | |
err = anypb.UnmarshalTo(anyData, &unpackedMsg, proto.UnmarshalOptions{}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Print the unmarshaled message | |
fmt.Printf("Unpacked message: %v\n", unpackedMsg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment