code format
This commit is contained in:
parent
bb69d4de70
commit
61cc2727ca
@ -5,14 +5,11 @@ package demo_protobuf3_optional;
|
||||
option go_package = "git.jeffthecoder.xyz/public/demo-protobuf3-optional";
|
||||
|
||||
message WithOptional {
|
||||
string username = 1;
|
||||
optional int64 userid = 2;
|
||||
string username = 1;
|
||||
optional int64 userid = 2;
|
||||
}
|
||||
|
||||
message WithOneof {
|
||||
string username = 1;
|
||||
oneof optional_userid {
|
||||
int64 userid = 2;
|
||||
}
|
||||
string username = 1;
|
||||
oneof optional_userid { int64 userid = 2; }
|
||||
}
|
||||
|
||||
|
134
oneof_test.go
134
oneof_test.go
@ -7,82 +7,82 @@ import (
|
||||
)
|
||||
|
||||
func TestOneofNilId(t *testing.T) {
|
||||
noid := &WithOneof {
|
||||
Username: "me",
|
||||
OptionalUserid: nil,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
noid := &WithOneof{
|
||||
Username: "me",
|
||||
OptionalUserid: nil,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.OptionalUserid)
|
||||
if unmarshaled.OptionalUserid != nil {
|
||||
t.Fatalf("expected userid is nil, got %v", unmarshaled.OptionalUserid)
|
||||
}
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.OptionalUserid)
|
||||
if unmarshaled.OptionalUserid != nil {
|
||||
t.Fatalf("expected userid is nil, got %v", unmarshaled.OptionalUserid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneofZeroId(t *testing.T) {
|
||||
id := &WithOneof_Userid{
|
||||
Userid: 0,
|
||||
}
|
||||
noid := &WithOneof {
|
||||
Username: "me",
|
||||
OptionalUserid: id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
id := &WithOneof_Userid{
|
||||
Userid: 0,
|
||||
}
|
||||
noid := &WithOneof{
|
||||
Username: "me",
|
||||
OptionalUserid: id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
|
||||
if unmarshaled.OptionalUserid == nil {
|
||||
t.Fatalf("expected userid is not nil, got %v", unmarshaled.OptionalUserid)
|
||||
} else if userid, ok := unmarshaled.OptionalUserid.(*WithOneof_Userid); !ok {
|
||||
t.Fatalf("expected userid is instance of type %T , got %T", &WithOneof_Userid{}, unmarshaled.OptionalUserid )
|
||||
} else if userid.Userid != 0 {
|
||||
t.Errorf("expected userid is %v, got %v", id, userid.Userid)
|
||||
} else {
|
||||
t.Log("id: ", userid.Userid)
|
||||
}
|
||||
if unmarshaled.OptionalUserid == nil {
|
||||
t.Fatalf("expected userid is not nil, got %v", unmarshaled.OptionalUserid)
|
||||
} else if userid, ok := unmarshaled.OptionalUserid.(*WithOneof_Userid); !ok {
|
||||
t.Fatalf("expected userid is instance of type %T , got %T", &WithOneof_Userid{}, unmarshaled.OptionalUserid)
|
||||
} else if userid.Userid != 0 {
|
||||
t.Errorf("expected userid is %v, got %v", id, userid.Userid)
|
||||
} else {
|
||||
t.Log("id: ", userid.Userid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneofNonZeroId(t *testing.T) {
|
||||
id := &WithOneof_Userid{
|
||||
Userid: 1,
|
||||
}
|
||||
noid := &WithOneof {
|
||||
Username: "me",
|
||||
OptionalUserid: id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
id := &WithOneof_Userid{
|
||||
Userid: 1,
|
||||
}
|
||||
noid := &WithOneof{
|
||||
Username: "me",
|
||||
OptionalUserid: id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
unmarshaled := &WithOneof{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
|
||||
if unmarshaled.OptionalUserid == nil {
|
||||
t.Fatalf("expected userid is not nil, got %v", unmarshaled.OptionalUserid)
|
||||
} else if userid, ok := unmarshaled.OptionalUserid.(*WithOneof_Userid); !ok {
|
||||
t.Fatalf("expected userid is instance of type %T , got %T", &WithOneof_Userid{}, unmarshaled.OptionalUserid )
|
||||
} else if userid.Userid != 1{
|
||||
t.Errorf("expected userid is %v, got %v", id, userid.Userid)
|
||||
} else {
|
||||
t.Log("id: ", userid.Userid)
|
||||
}
|
||||
if unmarshaled.OptionalUserid == nil {
|
||||
t.Fatalf("expected userid is not nil, got %v", unmarshaled.OptionalUserid)
|
||||
} else if userid, ok := unmarshaled.OptionalUserid.(*WithOneof_Userid); !ok {
|
||||
t.Fatalf("expected userid is instance of type %T , got %T", &WithOneof_Userid{}, unmarshaled.OptionalUserid)
|
||||
} else if userid.Userid != 1 {
|
||||
t.Errorf("expected userid is %v, got %v", id, userid.Userid)
|
||||
} else {
|
||||
t.Log("id: ", userid.Userid)
|
||||
}
|
||||
}
|
||||
|
106
optional_test.go
106
optional_test.go
@ -7,66 +7,66 @@ import (
|
||||
)
|
||||
|
||||
func TestOptionalNilId(t *testing.T) {
|
||||
noid := &WithOptional {
|
||||
Username: "me",
|
||||
Userid: nil,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
noid := &WithOptional{
|
||||
Username: "me",
|
||||
Userid: nil,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
if unmarshaled.Userid != nil {
|
||||
t.Fatalf("expected userid is nil, got %v", unmarshaled.Userid)
|
||||
}
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
if unmarshaled.Userid != nil {
|
||||
t.Fatalf("expected userid is nil, got %v", unmarshaled.Userid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptionalZeroId(t *testing.T) {
|
||||
id := int64(0)
|
||||
noid := &WithOptional {
|
||||
Username: "me",
|
||||
Userid: &id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
id := int64(0)
|
||||
noid := &WithOptional{
|
||||
Username: "me",
|
||||
Userid: &id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
if unmarshaled.Userid == nil || *unmarshaled.Userid != 0 {
|
||||
t.Fatalf("expected userid is non nil 0, got %v", unmarshaled.Userid)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
if unmarshaled.Userid == nil || *unmarshaled.Userid != 0 {
|
||||
t.Fatalf("expected userid is non nil 0, got %v", unmarshaled.Userid)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
}
|
||||
|
||||
func TestOptionalNonZeroId(t *testing.T) {
|
||||
id := int64(1)
|
||||
noid := &WithOptional {
|
||||
Username: "me",
|
||||
Userid: &id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
id := int64(1)
|
||||
noid := &WithOptional{
|
||||
Username: "me",
|
||||
Userid: &id,
|
||||
}
|
||||
t.Log(noid.String())
|
||||
wiremessage, err := proto.Marshal(noid)
|
||||
if err != nil {
|
||||
t.Fatal("marshal failed: ", err)
|
||||
}
|
||||
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
if unmarshaled.Userid == nil || *unmarshaled.Userid != 1 {
|
||||
t.Fatalf("expected userid is non nil 1, got %v", unmarshaled.Userid)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
unmarshaled := &WithOptional{}
|
||||
if err := proto.Unmarshal(wiremessage, unmarshaled); err != nil {
|
||||
t.Fatal("failed to unmarshal message: ", err)
|
||||
}
|
||||
if unmarshaled.Userid == nil || *unmarshaled.Userid != 1 {
|
||||
t.Fatalf("expected userid is non nil 1, got %v", unmarshaled.Userid)
|
||||
}
|
||||
t.Log("id: ", unmarshaled.Userid)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user