protobuf-go/internal/legacy/file.go

79 lines
2.4 KiB
Go
Raw Normal View History

// 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 legacy
import (
"bytes"
"compress/gzip"
"io/ioutil"
"sync"
"github.com/golang/protobuf/v2/proto"
internal/legacy: drop dependency on protoV1 via vendoring In order to transition more of v1 proto package to use the v2 API, we need the v2 API to stop depending on v1 proto. The legacy package currently depends on v1 proto because it needs to unmarshal the descriptor protos. Ideally, we would switch this to use the v2 implementation of wire unmarshaling. However, that is not available yet. So, instead, we vendor a minified version of the v1 proto package that only supports unmarshaling. The only changes to the vendored v1 code are: * Delete code not needed to implement proto.Unmarshal * Drop support for message sets * Drop support for reporting the full field name for required not set errors The unused tool was used to delete unrelated code: https://github.com/dominikh/go-tools/tree/master/cmd/unused To verify that the dependency was dropped: $ cd internal/legacy $ go list -f "{{join .Deps \"\n\"}}" | sort | uniq | grep protobuf github.com/golang/protobuf/protoapi github.com/golang/protobuf/v2/internal/detrand github.com/golang/protobuf/v2/internal/encoding/tag github.com/golang/protobuf/v2/internal/encoding/text github.com/golang/protobuf/v2/internal/encoding/wire github.com/golang/protobuf/v2/internal/errors github.com/golang/protobuf/v2/internal/flags github.com/golang/protobuf/v2/internal/impl github.com/golang/protobuf/v2/internal/legacy/protoV1 github.com/golang/protobuf/v2/internal/pragma github.com/golang/protobuf/v2/internal/scalar github.com/golang/protobuf/v2/internal/set github.com/golang/protobuf/v2/internal/value github.com/golang/protobuf/v2/reflect/protoreflect github.com/golang/protobuf/v2/reflect/prototype github.com/golang/protobuf/v2/runtime/protoimpl github.com/golang/protobuf/v2/types/descriptor Change-Id: I470865f1a987203574339fefc7d83843a12af966 Reviewed-on: https://go-review.googlesource.com/c/152545 Reviewed-by: Damien Neil <dneil@google.com>
2018-12-05 00:24:22 +00:00
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
descriptorpb "github.com/golang/protobuf/v2/types/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 (
enumV1 interface {
EnumDescriptor() ([]byte, []int)
}
messageV1 interface {
Descriptor() ([]byte, []int)
}
)
var fileDescCache sync.Map // map[*byte]*descriptorpb.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) *descriptorpb.FileDescriptorProto {
// Fast-path: check whether we already have a cached file descriptor.
if fd, ok := fileDescCache.Load(&b[0]); ok {
return fd.(*descriptorpb.FileDescriptorProto)
}
// Slow-path: decompress and unmarshal the file descriptor proto.
fd := new(descriptorpb.FileDescriptorProto)
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
panic(err)
}
b, err = ioutil.ReadAll(zr)
if err != nil {
panic(err)
}
err = proto.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, fd)
if err != nil {
panic(err)
}
if fd, ok := fileDescCache.LoadOrStore(&b[0], fd); ok {
return fd.(*descriptorpb.FileDescriptorProto)
}
return fd
}
// parentFileDescriptor returns the parent protoreflect.FileDescriptor for the
// provide descriptor. It returns nil if there is no parent.
func parentFileDescriptor(d pref.Descriptor) pref.FileDescriptor {
for ok := true; ok; d, ok = d.Parent() {
if fd, _ := d.(pref.FileDescriptor); fd != nil {
return fd
}
}
return nil
}