Codebase list golang-goprotobuf / 53eaa57
Import upstream version 1.5.2 Debian Janitor 2 years ago
14 changed file(s) with 118 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
0 on: [push, pull_request]
1 name: Test
2 jobs:
3 test:
4 strategy:
5 matrix:
6 go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x]
7 os: [ubuntu-latest, macos-latest]
8 runs-on: ${{ matrix.os }}
9 steps:
10 - name: Install Go
11 uses: actions/setup-go@v2
12 with:
13 go-version: ${{ matrix.go-version }}
14 - name: Checkout code
15 uses: actions/checkout@v2
16 - name: TestLatest
17 if: matrix.go-version == '1.16.x'
18 run: ./test.bash
19 - name: TestAll
20 if: matrix.go-version != '1.16.x'
21 run: go test ./...
+0
-16
.travis.yml less more
0 language: go
1 before_install:
2 - mkdir /tmp/go1.12
3 - curl -L -s https://dl.google.com/go/go1.12.linux-amd64.tar.gz | tar -zxf - -C /tmp/go1.12 --strip-components 1
4 - unset GOROOT
5 - (GO111MODULE=on /tmp/go1.12/bin/go mod vendor)
6 matrix:
7 include:
8 - go: 1.9.x
9 script: go test -tags protolegacy -v ./...
10 - go: 1.10.x
11 script: go test -tags protolegacy -v ./...
12 - go: 1.11.x
13 script: go test -tags protolegacy -v ./...
14 - go: 1.12.x
15 script: ./test.bash
7878 }
7979
8080 // Obtain the raw file descriptor.
81 var raw []byte
82 switch fd := d.(type) {
83 case interface{ ProtoLegacyRawDesc() []byte }:
84 raw = fd.ProtoLegacyRawDesc()
85 case protoreflect.FileDescriptor:
86 raw, _ = proto.Marshal(protodesc.ToFileDescriptorProto(fd))
87 }
88 file := protoimpl.X.CompressGZIP(raw)
81 fd := d.(protoreflect.FileDescriptor)
82 b, _ := proto.Marshal(protodesc.ToFileDescriptorProto(fd))
83 file := protoimpl.X.CompressGZIP(b)
8984
9085 // Reverse the indexes, since we populated it in reverse.
9186 for i, j := 0, len(idxs)-1; i < j; i, j = i+1, j-1 {
0 // Deprecated: Use the "google.golang.org/protobuf" module instead.
01 module github.com/golang/protobuf
12
23 go 1.9
34
45 require (
5 github.com/google/go-cmp v0.4.0
6 google.golang.org/protobuf v1.23.0
6 github.com/google/go-cmp v0.5.5
7 google.golang.org/protobuf v1.26.0
78 )
0 github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
1 github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
2 github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
3 github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
4 github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
5 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
6 github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
7 github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
8 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
0 github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1 github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
2 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
93 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
104 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11 google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
12 google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
13 google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
14 google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
15 google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
16 google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
17 google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
5 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
6 google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
7 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
134134 md := m.Descriptor()
135135 fds := md.Fields()
136136
137 if string(in) == "null" && md.FullName() != "google.protobuf.Value" {
138 return nil
139 }
140
141137 if jsu, ok := proto.MessageV1(m.Interface()).(JSONPBUnmarshaler); ok {
142138 return jsu.UnmarshalJSONPB(u, in)
139 }
140
141 if string(in) == "null" && md.FullName() != "google.protobuf.Value" {
142 return nil
143143 }
144144
145145 switch wellKnownType(md.FullName()) {
331331 raw = v
332332 }
333333
334 field := m.NewField(fd)
334335 // Unmarshal the field value.
335 if raw == nil || (string(raw) == "null" && !isSingularWellKnownValue(fd)) {
336 if raw == nil || (string(raw) == "null" && !isSingularWellKnownValue(fd) && !isSingularJSONPBUnmarshaler(field, fd)) {
336337 continue
337338 }
338 v, err := u.unmarshalValue(m.NewField(fd), raw, fd)
339 v, err := u.unmarshalValue(field, raw, fd)
339340 if err != nil {
340341 return err
341342 }
363364 return fmt.Errorf("extension field %q does not extend message %q", xname, m.Descriptor().FullName())
364365 }
365366
367 field := m.NewField(fd)
366368 // Unmarshal the field value.
367 if raw == nil || (string(raw) == "null" && !isSingularWellKnownValue(fd)) {
369 if raw == nil || (string(raw) == "null" && !isSingularWellKnownValue(fd) && !isSingularJSONPBUnmarshaler(field, fd)) {
368370 continue
369371 }
370 v, err := u.unmarshalValue(m.NewField(fd), raw, fd)
372 v, err := u.unmarshalValue(field, raw, fd)
371373 if err != nil {
372374 return err
373375 }
385387 func isSingularWellKnownValue(fd protoreflect.FieldDescriptor) bool {
386388 if md := fd.Message(); md != nil {
387389 return md.FullName() == "google.protobuf.Value" && fd.Cardinality() != protoreflect.Repeated
390 }
391 return false
392 }
393
394 func isSingularJSONPBUnmarshaler(v protoreflect.Value, fd protoreflect.FieldDescriptor) bool {
395 if fd.Message() != nil && fd.Cardinality() != protoreflect.Repeated {
396 _, ok := proto.MessageV1(v.Interface()).(JSONPBUnmarshaler)
397 return ok
388398 }
389399 return false
390400 }
165165 fd := fds.ByNumber(1)
166166 return w.marshalValue(fd, m.Get(fd), indent)
167167 case "Duration":
168 const maxSecondsInDuration = 315576000000
168169 // "Generated output always contains 0, 3, 6, or 9 fractional digits,
169170 // depending on required precision."
170171 s := m.Get(fds.ByNumber(1)).Int()
171172 ns := m.Get(fds.ByNumber(2)).Int()
173 if s < -maxSecondsInDuration || s > maxSecondsInDuration {
174 return fmt.Errorf("seconds out of range %v", s)
175 }
172176 if ns <= -secondInNanos || ns >= secondInNanos {
173177 return fmt.Errorf("ns out of range (%v, %v)", -secondInNanos, secondInNanos)
174178 }
175179 if (s > 0 && ns < 0) || (s < 0 && ns > 0) {
176180 return errors.New("signs of seconds and nanos do not match")
177181 }
178 if s < 0 {
179 ns = -ns
180 }
181 x := fmt.Sprintf("%d.%09d", s, ns)
182 var sign string
183 if s < 0 || ns < 0 {
184 sign, s, ns = "-", -1*s, -1*ns
185 }
186 x := fmt.Sprintf("%s%d.%09d", sign, s, ns)
182187 x = strings.TrimSuffix(x, "000")
183188 x = strings.TrimSuffix(x, "000")
184189 x = strings.TrimSuffix(x, ".000")
447447 {"Any with message and indent", marshalerAllOptions, anySimple, anySimplePrettyJSON},
448448 {"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
449449 {"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
450 {"Duration", marshaler, &pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3s"}`},
451 {"Duration", marshaler, &pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 3, Nanos: 1e6}}, `{"dur":"3.001s"}`},
452 {"Duration beyond float64 precision", marshaler, &pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 100000000, Nanos: 1}}, `{"dur":"100000000.000000001s"}`},
453 {"negative Duration", marshaler, &pb2.KnownTypes{Dur: &durpb.Duration{Seconds: -123, Nanos: -456}}, `{"dur":"-123.000000456s"}`},
450 {"Duration empty", marshaler, &durpb.Duration{}, `"0s"`},
451 {"Duration with secs", marshaler, &durpb.Duration{Seconds: 3}, `"3s"`},
452 {"Duration with -secs", marshaler, &durpb.Duration{Seconds: -3}, `"-3s"`},
453 {"Duration with nanos", marshaler, &durpb.Duration{Nanos: 1e6}, `"0.001s"`},
454 {"Duration with -nanos", marshaler, &durpb.Duration{Nanos: -1e6}, `"-0.001s"`},
455 {"Duration with large secs", marshaler, &durpb.Duration{Seconds: 1e10, Nanos: 1}, `"10000000000.000000001s"`},
456 {"Duration with 6-digit nanos", marshaler, &durpb.Duration{Nanos: 1e4}, `"0.000010s"`},
457 {"Duration with 3-digit nanos", marshaler, &durpb.Duration{Nanos: 1e6}, `"0.001s"`},
458 {"Duration with -secs -nanos", marshaler, &durpb.Duration{Seconds: -123, Nanos: -450}, `"-123.000000450s"`},
459 {"Duration max value", marshaler, &durpb.Duration{Seconds: 315576000000, Nanos: 999999999}, `"315576000000.999999999s"`},
460 {"Duration min value", marshaler, &durpb.Duration{Seconds: -315576000000, Nanos: -999999999}, `"-315576000000.999999999s"`},
454461 {"Struct", marshaler, &pb2.KnownTypes{St: &stpb.Struct{
455462 Fields: map[string]*stpb.Value{
456463 "one": {Kind: &stpb.Value_StringValue{"loneliest number"}},
523530 pb proto.Message
524531 fail bool
525532 }{
526 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 0}}, false},
527 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 0}}, false},
528 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: -1}}, true},
529 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 1}}, true},
530 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 1000000000}}, true},
531 {&pb2.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: -1000000000}}, true},
532 {&pb2.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1}}, false},
533 {&pb2.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: -1}}, true},
534 {&pb2.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1000000000}}, true},
533 {&durpb.Duration{Seconds: 1, Nanos: 0}, false},
534 {&durpb.Duration{Seconds: -1, Nanos: 0}, false},
535 {&durpb.Duration{Seconds: 1, Nanos: -1}, true},
536 {&durpb.Duration{Seconds: -1, Nanos: 1}, true},
537 {&durpb.Duration{Seconds: 315576000001}, true},
538 {&durpb.Duration{Seconds: -315576000001}, true},
539 {&durpb.Duration{Seconds: 1, Nanos: 1000000000}, true},
540 {&durpb.Duration{Seconds: -1, Nanos: -1000000000}, true},
541 {&tspb.Timestamp{Seconds: 1, Nanos: 1}, false},
542 {&tspb.Timestamp{Seconds: 1, Nanos: -1}, true},
543 {&tspb.Timestamp{Seconds: 1, Nanos: 1000000000}, true},
535544 }
536545 for _, tt := range tests {
537546 _, err := marshaler.MarshalToString(tt.pb)
9991008 t.Errorf("unmarshal error: %v", err)
10001009 }
10011010
1002 want := ptrFieldMessage{}
1011 want := ptrFieldMessage{StringField: &stringField{IsSet: true, StringValue: "null"}}
10031012 if !proto.Equal(&ptrFieldMsg, &want) {
10041013 t.Errorf("unmarshal result StringField: got %v, want %v", ptrFieldMsg, want)
10051014 }
1212 "strings"
1313 "sync"
1414
15 "google.golang.org/protobuf/reflect/protodesc"
1516 "google.golang.org/protobuf/reflect/protoreflect"
1617 "google.golang.org/protobuf/reflect/protoregistry"
1718 "google.golang.org/protobuf/runtime/protoimpl"
6162 // Find the descriptor in the v2 registry.
6263 var b []byte
6364 if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil {
64 if fd, ok := fd.(interface{ ProtoLegacyRawDesc() []byte }); ok {
65 b = fd.ProtoLegacyRawDesc()
66 } else {
67 // TODO: Use protodesc.ToFileDescriptorProto to construct
68 // a descriptorpb.FileDescriptorProto and marshal it.
69 // However, doing so causes the proto package to have a dependency
70 // on descriptorpb, leading to cyclic dependency issues.
71 }
65 b, _ = Marshal(protodesc.ToFileDescriptorProto(fd))
7266 }
7367
7468 // Locally cache the raw descriptor form for the file.
764764 if i > utf8.MaxRune {
765765 return "", "", fmt.Errorf(`\%c%s is not a valid Unicode code point`, r, ss)
766766 }
767 return string(i), s, nil
767 return string(rune(i)), s, nil
768768 }
769769 return "", "", fmt.Errorf(`unknown escape \%c`, r)
770770 }
1818
1919 // AnyMessageName returns the message name contained in an anypb.Any message.
2020 // Most type assertions should use the Is function instead.
21 //
22 // Deprecated: Call the any.MessageName method instead.
2123 func AnyMessageName(any *anypb.Any) (string, error) {
2224 name, err := anyMessageName(any)
2325 return string(name), err
3739 }
3840
3941 // MarshalAny marshals the given message m into an anypb.Any message.
42 //
43 // Deprecated: Call the anypb.New function instead.
4044 func MarshalAny(m proto.Message) (*anypb.Any, error) {
4145 switch dm := m.(type) {
4246 case DynamicAny:
5761 // Empty returns a new message of the type specified in an anypb.Any message.
5862 // It returns protoregistry.NotFound if the corresponding message type could not
5963 // be resolved in the global registry.
64 //
65 // Deprecated: Use protoregistry.GlobalTypes.FindMessageByName instead
66 // to resolve the message name and create a new instance of it.
6067 func Empty(any *anypb.Any) (proto.Message, error) {
6168 name, err := anyMessageName(any)
6269 if err != nil {
7582 //
7683 // The target message m may be a *DynamicAny message. If the underlying message
7784 // type could not be resolved, then this returns protoregistry.NotFound.
85 //
86 // Deprecated: Call the any.UnmarshalTo method instead.
7887 func UnmarshalAny(any *anypb.Any, m proto.Message) error {
7988 if dm, ok := m.(*DynamicAny); ok {
8089 if dm.Message == nil {
99108 }
100109
101110 // Is reports whether the Any message contains a message of the specified type.
111 //
112 // Deprecated: Call the any.MessageIs method instead.
102113 func Is(any *anypb.Any, m proto.Message) bool {
103114 if any == nil || m == nil {
104115 return false
118129 // var x ptypes.DynamicAny
119130 // if err := ptypes.UnmarshalAny(a, &x); err != nil { ... }
120131 // fmt.Printf("unmarshaled message: %v", x.Message)
132 //
133 // Deprecated: Use the any.UnmarshalNew method instead to unmarshal
134 // the any message contents into a new instance of the underlying message.
121135 type DynamicAny struct{ proto.Message }
122136
123137 func (m DynamicAny) String() string {
22 // license that can be found in the LICENSE file.
33
44 // Package ptypes provides functionality for interacting with well-known types.
5 //
6 // Deprecated: Well-known types have specialized functionality directly
7 // injected into the generated packages for each message type.
8 // See the deprecation notice for each function for the suggested alternative.
59 package ptypes
2020
2121 // Duration converts a durationpb.Duration to a time.Duration.
2222 // Duration returns an error if dur is invalid or overflows a time.Duration.
23 //
24 // Deprecated: Call the dur.AsDuration and dur.CheckValid methods instead.
2325 func Duration(dur *durationpb.Duration) (time.Duration, error) {
2426 if err := validateDuration(dur); err != nil {
2527 return 0, err
3840 }
3941
4042 // DurationProto converts a time.Duration to a durationpb.Duration.
43 //
44 // Deprecated: Call the durationpb.New function instead.
4145 func DurationProto(d time.Duration) *durationpb.Duration {
4246 nanos := d.Nanoseconds()
4347 secs := nanos / 1e9
3232 //
3333 // A nil Timestamp returns an error. The first return value in that case is
3434 // undefined.
35 //
36 // Deprecated: Call the ts.AsTime and ts.CheckValid methods instead.
3537 func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) {
3638 // Don't return the zero value on error, because corresponds to a valid
3739 // timestamp. Instead return whatever time.Unix gives us.
4547 }
4648
4749 // TimestampNow returns a google.protobuf.Timestamp for the current time.
50 //
51 // Deprecated: Call the timestamppb.Now function instead.
4852 func TimestampNow() *timestamppb.Timestamp {
4953 ts, err := TimestampProto(time.Now())
5054 if err != nil {
5559
5660 // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
5761 // It returns an error if the resulting Timestamp is invalid.
62 //
63 // Deprecated: Call the timestamppb.New function instead.
5864 func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) {
5965 ts := &timestamppb.Timestamp{
6066 Seconds: t.Unix(),
6874
6975 // TimestampString returns the RFC 3339 string for valid Timestamps.
7076 // For invalid Timestamps, it returns an error message in parentheses.
77 //
78 // Deprecated: Call the ts.AsTime method instead,
79 // followed by a call to the Format method on the time.Time value.
7180 func TimestampString(ts *timestamppb.Timestamp) string {
7281 t, err := Timestamp(ts)
7382 if err != nil {