mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-04 02:38:50 +00:00
90fe996bd1
In order for the v2 rollout to be as seamless as possible, we need to support the situation where a v2 message depends on some other generated v1 message that may be stale and does not support the v2 API. In such a situation, there needs to be some way to wrap a legacy message or enum in such a way that it satisfies the v2 API. This wrapping is comprised of two parts: 1) Deriving an enum or message descriptor 2) Providing an reflection implementation for messages This CL addresses part 1 (while part 2 has already been partially implemented, since the implementation applies to both v1 and v2). To derive the enum and message descriptor we rely on a mixture of parsing the raw descriptor proto and also introspection on the fields in the message. Methods for obtaining the raw descriptor protos were added in February, 2016, and so has not always been available. For that reason, we attempt to derive as much information from the Go type as possible. As part of this change, we modify prototype to be able to create multiple standalone messages as a set. This is needed since cyclic dependencies is allowed between messages within a single proto file. Change-Id: I71aaf5f977faf9fba03c370b1ee17b3758ce60a6 Reviewed-on: https://go-review.googlesource.com/c/143539 Reviewed-by: Damien Neil <dneil@google.com>
69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package impl
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"io/ioutil"
|
|
"sync"
|
|
|
|
// TODO: Avoid reliance on old API. However, there is currently a
|
|
// chicken and egg problem where we need the descriptor protos to implement
|
|
// the new API.
|
|
protoV1 "github.com/golang/protobuf/proto"
|
|
descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
)
|
|
|
|
// Every enum and message type generated by protoc-gen-go since commit 2fc053c5
|
|
// on February 25th, 2016 has had a method to get the raw descriptor.
|
|
// Types that were not generated by protoc-gen-go or were generated prior
|
|
// to that version are not supported.
|
|
//
|
|
// The []byte returned is the encoded form of a FileDescriptorProto message
|
|
// compressed using GZIP. The []int is the path from the top-level file
|
|
// to the specific message or enum declaration.
|
|
type (
|
|
legacyEnum interface {
|
|
EnumDescriptor() ([]byte, []int)
|
|
}
|
|
legacyMessage interface {
|
|
Descriptor() ([]byte, []int)
|
|
}
|
|
)
|
|
|
|
var fileDescCache sync.Map // map[*byte]*descriptorV1.FileDescriptorProto
|
|
|
|
// loadFileDesc unmarshals b as a compressed FileDescriptorProto message.
|
|
//
|
|
// This assumes that b is immutable and that b does not refer to part of a
|
|
// concatenated series of GZIP files (which would require shenanigans that
|
|
// rely on the concatenation properties of both protobufs and GZIP).
|
|
// File descriptors generated by protoc-gen-go do not rely on that property.
|
|
func loadFileDesc(b []byte) *descriptorV1.FileDescriptorProto {
|
|
// Fast-path: check whether we already have a cached file descriptor.
|
|
if v, ok := fileDescCache.Load(&b[0]); ok {
|
|
return v.(*descriptorV1.FileDescriptorProto)
|
|
}
|
|
|
|
// Slow-path: decompress and unmarshal the file descriptor proto.
|
|
m := new(descriptorV1.FileDescriptorProto)
|
|
zr, err := gzip.NewReader(bytes.NewReader(b))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
b, err = ioutil.ReadAll(zr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// TODO: What about extensions?
|
|
// The protoV1 API does not eagerly unmarshal extensions.
|
|
if err := protoV1.Unmarshal(b, m); err != nil {
|
|
panic(err)
|
|
}
|
|
fileDescCache.Store(&b[0], m)
|
|
return m
|
|
}
|