forked from bufbuild/buf
/
protoencoding.go
215 lines (184 loc) · 7.21 KB
/
protoencoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package protoencoding
import (
"github.com/alis-exchange/buf/private/pkg/protodescriptor"
"github.com/bufbuild/protoyaml-go"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
// Resolver can resolve files, messages, enums, and extensions.
type Resolver interface {
protodesc.Resolver
protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
FindEnumByName(enum protoreflect.FullName) (protoreflect.EnumType, error)
}
// NewResolver creates a new Resolver.
//
// If the input slice is empty, this returns nil
// The given FileDescriptors must be self-contained, that is they must contain all imports.
// This can NOT be guaranteed for FileDescriptorSets given over the wire, and can only be guaranteed from builds.
func NewResolver[F protodescriptor.FileDescriptor](fileDescriptors ...F) (Resolver, error) {
return newResolver(fileDescriptors...)
}
// NewLazyResolver creates a new Resolver that is constructed from the given
// descriptors only as needed, if invoked.
//
// If there is an error when constructing the resolver, it will be returned by all
// method calls of the returned resolver.
func NewLazyResolver[F protodescriptor.FileDescriptor](fileDescriptors ...F) Resolver {
return &lazyResolver{fn: func() (Resolver, error) {
return newResolver(fileDescriptors...)
}}
}
// CombineResolvers returns a resolver that uses all of the given resolvers. It
// will use the first resolver, and if it returns an error, the second will be
// tried, and so on.
func CombineResolvers(resolvers ...Resolver) Resolver {
return combinedResolver(resolvers)
}
// Marshaler marshals Messages.
type Marshaler interface {
Marshal(message proto.Message) ([]byte, error)
}
// NewWireMarshaler returns a new Marshaler for wire.
//
// See https://godoc.org/google.golang.org/protobuf/proto#MarshalOptions for a discussion on stability.
// This has the potential to be unstable over time.
func NewWireMarshaler() Marshaler {
return newWireMarshaler()
}
// NewJSONMarshaler returns a new Marshaler for JSON.
//
// This has the potential to be unstable over time.
// resolver can be nil if unknown and is only needed for extensions.
func NewJSONMarshaler(resolver Resolver, options ...JSONMarshalerOption) Marshaler {
return newJSONMarshaler(resolver, options...)
}
// JSONMarshalerOption is an option for a new JSONMarshaler.
type JSONMarshalerOption func(*jsonMarshaler)
// JSONMarshalerWithIndent says to use an indent of two spaces.
func JSONMarshalerWithIndent() JSONMarshalerOption {
return func(jsonMarshaler *jsonMarshaler) {
jsonMarshaler.indent = " "
}
}
// JSONMarshalerWithUseProtoNames says to use proto names.
func JSONMarshalerWithUseProtoNames() JSONMarshalerOption {
return func(jsonMarshaler *jsonMarshaler) {
jsonMarshaler.useProtoNames = true
}
}
// JSONMarshalerWithUseEnumNumbers says to use enum numbers.
func JSONMarshalerWithUseEnumNumbers() JSONMarshalerOption {
return func(jsonMarshaler *jsonMarshaler) {
jsonMarshaler.useEnumNumbers = true
}
}
// JSONMarshalerWithEmitUnpopulated says to emit unpopulated values
func JSONMarshalerWithEmitUnpopulated() JSONMarshalerOption {
return func(jsonMarshaler *jsonMarshaler) {
jsonMarshaler.emitUnpopulated = true
}
}
// NewTxtpbMarshaler returns a new Marshaler for txtpb.
//
// resolver can be nil if unknown and is only needed for extensions.
func NewTxtpbMarshaler(resolver Resolver) Marshaler {
return newTxtpbMarshaler(resolver)
}
// NewYAMLMarshaler returns a new Marshaler for YAML.
//
// resolver can be nil if unknown and is only needed for extensions.
func NewYAMLMarshaler(resolver Resolver, options ...YAMLMarshalerOption) Marshaler {
return newYAMLMarshaler(resolver, options...)
}
// YAMLMarshalerOption is an option for a new YAMLMarshaler.
type YAMLMarshalerOption func(*yamlMarshaler)
// YAMLMarshalerWithIndent says to use an indent of two spaces.
func YAMLMarshalerWithIndent() YAMLMarshalerOption {
return func(yamlMarshaler *yamlMarshaler) {
yamlMarshaler.indent = 2
}
}
// YAMLMarshalerWithUseProtoNames says to use proto names.
func YAMLMarshalerWithUseProtoNames() YAMLMarshalerOption {
return func(yamlMarshaler *yamlMarshaler) {
yamlMarshaler.useProtoNames = true
}
}
// YAMLMarshalerWithUseEnumNumbers says to use enum numbers.
func YAMLMarshalerWithUseEnumNumbers() YAMLMarshalerOption {
return func(yamlMarshaler *yamlMarshaler) {
yamlMarshaler.useEnumNumbers = true
}
}
// YAMLMarshalerWithEmitUnpopulated says to emit unpopulated values
func YAMLMarshalerWithEmitUnpopulated() YAMLMarshalerOption {
return func(yamlMarshaler *yamlMarshaler) {
yamlMarshaler.emitUnpopulated = true
}
}
// Unmarshaler unmarshals Messages.
type Unmarshaler interface {
Unmarshal(data []byte, message proto.Message) error
}
// NewWireUnmarshaler returns a new Unmarshaler for wire.
//
// resolver can be nil if unknown and are only needed for extensions.
func NewWireUnmarshaler(resolver Resolver) Unmarshaler {
return newWireUnmarshaler(resolver)
}
// NewJSONUnmarshaler returns a new Unmarshaler for json.
//
// resolver can be nil if unknown and are only needed for extensions.
func NewJSONUnmarshaler(resolver Resolver, options ...JSONUnmarshalerOption) Unmarshaler {
return newJSONUnmarshaler(resolver, options...)
}
// JSONUnmarshalerOption is an option for a new JSONUnmarshaler.
type JSONUnmarshalerOption func(*jsonUnmarshaler)
// JSONUnmarshalerWithDisallowUnknown says to disallow unrecognized fields.
func JSONUnmarshalerWithDisallowUnknown() JSONUnmarshalerOption {
return func(jsonUnmarshaler *jsonUnmarshaler) {
jsonUnmarshaler.disallowUnknown = true
}
}
// NewTxtpbUnmarshaler returns a new Unmarshaler for txtpb.
//
// resolver can be nil if unknown and are only needed for extensions.
func NewTxtpbUnmarshaler(resolver Resolver) Unmarshaler {
return newTxtpbUnmarshaler(resolver)
}
// YAMLUnmarshalerOption is an option for a new YAMLUnmarshaler.
type YAMLUnmarshalerOption func(*yamlUnmarshaler)
// YAMLUnmarshalerWithPath says to use the given path.
func YAMLUnmarshalerWithPath(path string) YAMLUnmarshalerOption {
return func(yamlUnmarshaler *yamlUnmarshaler) {
yamlUnmarshaler.path = path
}
}
func YAMLUnmarshalerWithValidator(validator protoyaml.Validator) YAMLUnmarshalerOption {
return func(yamlUnmarshaler *yamlUnmarshaler) {
yamlUnmarshaler.validator = validator
}
}
// NewYAMLUnmarshaler returns a new Unmarshaler for yaml.
//
// resolver can be nil if unknown and are only needed for extensions.
func NewYAMLUnmarshaler(resolver Resolver, options ...YAMLUnmarshalerOption) Unmarshaler {
return newYAMLUnmarshaler(resolver, options...)
}