Debian Codegraph @

descriptors.go

raw symbols SCIP
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package main
18
19 import (
20 "bytes"
21 "fmt"
22 "io"
23 "io/ioutil"
24 "log"
25 "os"
26 "os/exec"
27 "strings"
28
29 "github.com/golang/protobuf/proto"
30 "github.com/golang/protobuf/protoc-gen-go/descriptor"
31 )
32
33 type descriptorSet struct {
34 merged descriptor.FileDescriptorSet
35 seen map[string]struct{}
36 ignoreFiles map[string]struct{}
37 descProto string
38 includeDir string
39 }
40
41 func newDescriptorSet(ignoreFiles []string, d string, i string) *descriptorSet {
42 ifm := make(map[string]struct{}, len(ignoreFiles))
43 for _, ignore := range ignoreFiles {
44 ifm[ignore] = struct{}{}
45 }
46 return &descriptorSet{
47 seen: make(map[string]struct{}),
48 ignoreFiles: ifm,
49 descProto: d,
50 includeDir: i,
51 }
52 }
53
54 func (d *descriptorSet) add(descs ...*descriptor.FileDescriptorProto) {
55 for _, file := range descs {
56 name := file.GetName()
57 if _, ok := d.seen[name]; ok {
58 continue
59 }
60
61 if _, ok := d.ignoreFiles[name]; ok {
62 continue
63 }
64
65 // TODO(stevvooe): If we want to filter certain fields in the descriptor,
66 // this is the place to do it. May be necessary if certain fields are
67 // noisy, such as option fields.
68 d.merged.File = append(d.merged.File, file)
69 d.seen[name] = struct{}{}
70 }
71 }
72
73 // stabilize outputs the merged protobuf descriptor set into the provided writer.
74 //
75 // This is equivalent to the following command:
76 //
77 // cat merged.pb | protoc -I /path/to --decode google.protobuf.FileDescriptorSet /path/to/google/protobuf/descriptor.proto
78 func (d *descriptorSet) marshalTo(w io.Writer) error {
79 p, err := proto.Marshal(&d.merged)
80 if err != nil {
81 return err
82 }
83
84 args := []string{
85 "protoc",
86 "-I",
87 d.includeDir,
88 "--decode",
89 "google.protobuf.FileDescriptorSet",
90 d.descProto,
91 }
92
93 cmd := exec.Command(args[0], args[1:]...)
94 cmd.Stdin = bytes.NewReader(p)
95 cmd.Stdout = w
96 cmd.Stderr = os.Stderr
97
98 if !quiet {
99 fmt.Println(strings.Join(args, " "))
100 }
101 return cmd.Run()
102 }
103
104 func readDesc(path string) (*descriptor.FileDescriptorSet, error) {
105 var desc descriptor.FileDescriptorSet
106
107 p, err := ioutil.ReadFile(path)
108 if err != nil {
109 return nil, err
110 }
111
112 if err := proto.Unmarshal(p, &desc); err != nil {
113 log.Fatalln(err)
114 }
115
116 return &desc, nil
117 }