Skip to content

Commit f9065fe

Browse files
committed
wkt any. removed 'any' from generating a standard NewPopulated method and rather use a jsonpb friendly version. when you marshal with jsonpb it expects a registered proto message since it has to unmarshal it in order to format it in the 'any' json format. The new populated method just uses a random Duration wkt which is already in the same package.
1 parent 4c00d2f commit f9065fe

File tree

12 files changed

+1464
-1243
lines changed

12 files changed

+1464
-1243
lines changed

protoc-gen-gogotypes/main.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,17 @@ func main() {
5353

5454
vanity.ForEachFile(files, vanity.TurnOnMessageNameAll)
5555

56-
for _, file := range files {
57-
if strings.HasSuffix(file.GetName(), "struct.proto") {
58-
// TODO struct can also get a compare method when
59-
// https://github.com/gogo/protobuf/issues/221 is fixed
60-
//continue
61-
}
62-
vanity.TurnOnCompareAll(file)
63-
}
56+
vanity.ForEachFile(files, vanity.TurnOnCompareAll)
6457

6558
for _, file := range files {
6659
if strings.HasSuffix(file.GetName(), "timestamp.proto") ||
6760
strings.HasSuffix(file.GetName(), "duration.proto") {
6861
continue
6962
}
7063
vanity.TurnOnStringerAll(file)
64+
if strings.HasSuffix(file.GetName(), "any.proto") {
65+
continue
66+
}
7167
vanity.TurnOnPopulateAll(file)
7268
}
7369

test/types/combos/both/types.pb.go

+467-395
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/types/combos/both/types.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package types;
3232

3333
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
3434

35-
//import "google/protobuf/any.proto";
35+
import "google/protobuf/any.proto";
3636
import "google/protobuf/duration.proto";
3737
import "google/protobuf/struct.proto";
3838
import "google/protobuf/timestamp.proto";
@@ -62,10 +62,8 @@ message KnownTypes {
6262
google.protobuf.BoolValue bool = 9;
6363
google.protobuf.StringValue str = 10;
6464
google.protobuf.BytesValue bytes = 11;
65-
66-
// TODO uncomment this once https://github.com/gogo/protobuf/issues/197 is fixed
6765
google.protobuf.Struct st = 12;
68-
// google.protobuf.Any an = 14;
66+
google.protobuf.Any an = 14;
6967
}
7068

7169
message ProtoTypes {

test/types/combos/marshaler/types.pb.go

+433-396
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/types/combos/marshaler/types.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package types;
3232

3333
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
3434

35-
//import "google/protobuf/any.proto";
35+
import "google/protobuf/any.proto";
3636
import "google/protobuf/duration.proto";
3737
import "google/protobuf/struct.proto";
3838
import "google/protobuf/timestamp.proto";
@@ -62,10 +62,8 @@ message KnownTypes {
6262
google.protobuf.BoolValue bool = 9;
6363
google.protobuf.StringValue str = 10;
6464
google.protobuf.BytesValue bytes = 11;
65-
66-
// TODO uncomment this once https://github.com/gogo/protobuf/issues/197 is fixed
6765
google.protobuf.Struct st = 12;
68-
// google.protobuf.Any an = 14;
66+
google.protobuf.Any an = 14;
6967
}
7068

7169
message ProtoTypes {

test/types/combos/neither/types.pb.go

+188-164
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/types/combos/neither/types.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package types;
3232

3333
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
3434

35-
//import "google/protobuf/any.proto";
35+
import "google/protobuf/any.proto";
3636
import "google/protobuf/duration.proto";
3737
import "google/protobuf/struct.proto";
3838
import "google/protobuf/timestamp.proto";
@@ -62,10 +62,8 @@ message KnownTypes {
6262
google.protobuf.BoolValue bool = 9;
6363
google.protobuf.StringValue str = 10;
6464
google.protobuf.BytesValue bytes = 11;
65-
66-
// TODO uncomment this once https://github.com/gogo/protobuf/issues/197 is fixed
6765
google.protobuf.Struct st = 12;
68-
// google.protobuf.Any an = 14;
66+
google.protobuf.Any an = 14;
6967
}
7068

7169
message ProtoTypes {

test/types/combos/unmarshaler/types.pb.go

+225-164
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/types/combos/unmarshaler/types.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package types;
3232

3333
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
3434

35-
//import "google/protobuf/any.proto";
35+
import "google/protobuf/any.proto";
3636
import "google/protobuf/duration.proto";
3737
import "google/protobuf/struct.proto";
3838
import "google/protobuf/timestamp.proto";
@@ -62,10 +62,8 @@ message KnownTypes {
6262
google.protobuf.BoolValue bool = 9;
6363
google.protobuf.StringValue str = 10;
6464
google.protobuf.BytesValue bytes = 11;
65-
66-
// TODO uncomment this once https://github.com/gogo/protobuf/issues/197 is fixed
6765
google.protobuf.Struct st = 12;
68-
// google.protobuf.Any an = 14;
66+
google.protobuf.Any an = 14;
6967
}
7068

7169
message ProtoTypes {

test/types/types.proto

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package types;
3232

3333
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
3434

35-
//import "google/protobuf/any.proto";
35+
import "google/protobuf/any.proto";
3636
import "google/protobuf/duration.proto";
3737
import "google/protobuf/struct.proto";
3838
import "google/protobuf/timestamp.proto";
@@ -62,10 +62,8 @@ message KnownTypes {
6262
google.protobuf.BoolValue bool = 9;
6363
google.protobuf.StringValue str = 10;
6464
google.protobuf.BytesValue bytes = 11;
65-
66-
// TODO uncomment this once https://github.com/gogo/protobuf/issues/197 is fixed
6765
google.protobuf.Struct st = 12;
68-
// google.protobuf.Any an = 14;
66+
google.protobuf.Any an = 14;
6967
}
7068

7169
message ProtoTypes {

types/any.pb.go

+9-96
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/any_gogo.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Protocol Buffers for Go with Gadgets
2+
//
3+
// Copyright (c) 2019, The GoGo Authors. All rights reserved.
4+
// http://github.com/gogo/protobuf
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are
8+
// met:
9+
//
10+
// * Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
// * Redistributions in binary form must reproduce the above
13+
// copyright notice, this list of conditions and the following disclaimer
14+
// in the documentation and/or other materials provided with the
15+
// distribution.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
package types
30+
31+
// The 'easy' option for a random any needs to be suitable
32+
// for json un/marshaling.
33+
// Use a random duration wkt for easy and the normal
34+
// generated random population for not easy.
35+
func NewPopulatedAny(r randyAny, easy bool) *Any {
36+
if easy {
37+
dur := NewPopulatedDuration(r, easy)
38+
this, err := MarshalAny(dur)
39+
if err != nil {
40+
return nil
41+
}
42+
return this
43+
}
44+
this := &Any{}
45+
this.TypeUrl = string(randStringAny(r))
46+
v1 := r.Intn(100)
47+
this.Value = make([]byte, v1)
48+
for i := 0; i < v1; i++ {
49+
this.Value[i] = byte(r.Intn(256))
50+
}
51+
if r.Intn(10) != 0 {
52+
this.XXX_unrecognized = randUnrecognizedAny(r, 3)
53+
}
54+
return this
55+
}
56+
57+
type randyAny interface {
58+
Int63() int64
59+
Intn(n int) int
60+
}
61+
62+
func randUTF8RuneAny(r randyAny) rune {
63+
ru := r.Intn(62)
64+
if ru < 10 {
65+
return rune(ru + 48)
66+
} else if ru < 36 {
67+
return rune(ru + 55)
68+
}
69+
return rune(ru + 61)
70+
}
71+
72+
func randStringAny(r randyAny) string {
73+
v2 := r.Intn(100)
74+
tmps := make([]rune, v2)
75+
for i := 0; i < v2; i++ {
76+
tmps[i] = randUTF8RuneAny(r)
77+
}
78+
return string(tmps)
79+
}
80+
81+
func randUnrecognizedAny(r randyAny, maxFieldNumber int) (dAtA []byte) {
82+
l := r.Intn(5)
83+
for i := 0; i < l; i++ {
84+
wire := r.Intn(4)
85+
if wire == 3 {
86+
wire = 5
87+
}
88+
fieldNumber := maxFieldNumber + r.Intn(100)
89+
dAtA = randFieldAny(dAtA, r, fieldNumber, wire)
90+
}
91+
return dAtA
92+
}
93+
94+
func randFieldAny(dAtA []byte, r randyAny, fieldNumber int, wire int) []byte {
95+
key := uint32(fieldNumber)<<3 | uint32(wire)
96+
switch wire {
97+
case 0:
98+
dAtA = encodeVarintPopulateAny(dAtA, uint64(key))
99+
v3 := r.Int63()
100+
if r.Intn(2) == 0 {
101+
v3 *= -1
102+
}
103+
dAtA = encodeVarintPopulateAny(dAtA, uint64(v3))
104+
case 1:
105+
dAtA = encodeVarintPopulateAny(dAtA, uint64(key))
106+
dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
107+
case 2:
108+
dAtA = encodeVarintPopulateAny(dAtA, uint64(key))
109+
ll := r.Intn(100)
110+
dAtA = encodeVarintPopulateAny(dAtA, uint64(ll))
111+
for j := 0; j < ll; j++ {
112+
dAtA = append(dAtA, byte(r.Intn(256)))
113+
}
114+
default:
115+
dAtA = encodeVarintPopulateAny(dAtA, uint64(key))
116+
dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))
117+
}
118+
return dAtA
119+
}
120+
121+
func encodeVarintPopulateAny(dAtA []byte, v uint64) []byte {
122+
for v >= 1<<7 {
123+
dAtA = append(dAtA, uint8(uint64(v)&0x7f|0x80))
124+
v >>= 7
125+
}
126+
dAtA = append(dAtA, uint8(v))
127+
return dAtA
128+
}

0 commit comments

Comments
 (0)