Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -125,7 +125,7 @@ github.com/containerd/continuity 004b46473808b3e7a4a3049c20e4 |
| 125 | 125 |
github.com/containerd/cgroups 4994991857f9b0ae8dc439551e8bebdbb4bf66c1 |
| 126 | 126 |
github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23 |
| 127 | 127 |
github.com/containerd/go-runc 5a6d9f37cfa36b15efba46dc7ea349fa9b7143c3 |
| 128 |
-github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40 |
|
| 128 |
+github.com/containerd/typeurl 2a93cfde8c20b23de8eb84a5adbc234ddf7a9e8d |
|
| 129 | 129 |
github.com/containerd/ttrpc f02858b1457c5ca3aaec3a0803eb0d59f96e41d6 |
| 130 | 130 |
github.com/gogo/googleapis 08a7655d27152912db7aaf4f983275eaf8d128ef |
| 131 | 131 |
|
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 |
+ |
|
| 1 | 2 |
Apache License |
| 2 | 3 |
Version 2.0, January 2004 |
| 3 |
- http://www.apache.org/licenses/ |
|
| 4 |
+ https://www.apache.org/licenses/ |
|
| 4 | 5 |
|
| 5 | 6 |
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION |
| 6 | 7 |
|
| ... | ... |
@@ -175,24 +176,13 @@ |
| 175 | 175 |
|
| 176 | 176 |
END OF TERMS AND CONDITIONS |
| 177 | 177 |
|
| 178 |
- APPENDIX: How to apply the Apache License to your work. |
|
| 179 |
- |
|
| 180 |
- To apply the Apache License to your work, attach the following |
|
| 181 |
- boilerplate notice, with the fields enclosed by brackets "[]" |
|
| 182 |
- replaced with your own identifying information. (Don't include |
|
| 183 |
- the brackets!) The text should be enclosed in the appropriate |
|
| 184 |
- comment syntax for the file format. We also recommend that a |
|
| 185 |
- file or class name and description of purpose be included on the |
|
| 186 |
- same "printed page" as the copyright notice for easier |
|
| 187 |
- identification within third-party archives. |
|
| 188 |
- |
|
| 189 |
- Copyright [yyyy] [name of copyright owner] |
|
| 178 |
+ Copyright The containerd Authors |
|
| 190 | 179 |
|
| 191 | 180 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 192 | 181 |
you may not use this file except in compliance with the License. |
| 193 | 182 |
You may obtain a copy of the License at |
| 194 | 183 |
|
| 195 |
- http://www.apache.org/licenses/LICENSE-2.0 |
|
| 184 |
+ https://www.apache.org/licenses/LICENSE-2.0 |
|
| 196 | 185 |
|
| 197 | 186 |
Unless required by applicable law or agreed to in writing, software |
| 198 | 187 |
distributed under the License is distributed on an "AS IS" BASIS, |
| ... | ... |
@@ -7,3 +7,13 @@ |
| 7 | 7 |
A Go package for managing the registration, marshaling, and unmarshaling of encoded types. |
| 8 | 8 |
|
| 9 | 9 |
This package helps when types are sent over a GRPC API and marshaled as a [protobuf.Any](). |
| 10 |
+ |
|
| 11 |
+## Project details |
|
| 12 |
+ |
|
| 13 |
+**typeurl** is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE). |
|
| 14 |
+As a containerd sub-project, you will find the: |
|
| 15 |
+ * [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md), |
|
| 16 |
+ * [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS), |
|
| 17 |
+ * and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md) |
|
| 18 |
+ |
|
| 19 |
+information in our [`containerd/project`](https://github.com/containerd/project) repository. |
| 10 | 20 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,83 @@ |
| 0 |
+/* |
|
| 1 |
+ Copyright The containerd Authors. |
|
| 2 |
+ |
|
| 3 |
+ Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 4 |
+ you may not use this file except in compliance with the License. |
|
| 5 |
+ You may obtain a copy of the License at |
|
| 6 |
+ |
|
| 7 |
+ http://www.apache.org/licenses/LICENSE-2.0 |
|
| 8 |
+ |
|
| 9 |
+ Unless required by applicable law or agreed to in writing, software |
|
| 10 |
+ distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 |
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 |
+ See the License for the specific language governing permissions and |
|
| 13 |
+ limitations under the License. |
|
| 14 |
+*/ |
|
| 15 |
+ |
|
| 16 |
+package typeurl |
|
| 17 |
+ |
|
| 18 |
+// Package typeurl assists with managing the registration, marshaling, and |
|
| 19 |
+// unmarshaling of types encoded as protobuf.Any. |
|
| 20 |
+// |
|
| 21 |
+// A protobuf.Any is a proto message that can contain any arbitrary data. It |
|
| 22 |
+// consists of two components, a TypeUrl and a Value, and its proto definition |
|
| 23 |
+// looks like this: |
|
| 24 |
+// |
|
| 25 |
+// message Any {
|
|
| 26 |
+// string type_url = 1; |
|
| 27 |
+// bytes value = 2; |
|
| 28 |
+// } |
|
| 29 |
+// |
|
| 30 |
+// The TypeUrl is used to distinguish the contents from other proto.Any |
|
| 31 |
+// messages. This typeurl library manages these URLs to enable automagic |
|
| 32 |
+// marshaling and unmarshaling of the contents. |
|
| 33 |
+// |
|
| 34 |
+// For example, consider this go struct: |
|
| 35 |
+// |
|
| 36 |
+// type Foo struct {
|
|
| 37 |
+// Field1 string |
|
| 38 |
+// Field2 string |
|
| 39 |
+// } |
|
| 40 |
+// |
|
| 41 |
+// To use typeurl, types must first be registered. This is typically done in |
|
| 42 |
+// the init function |
|
| 43 |
+// |
|
| 44 |
+// func init() {
|
|
| 45 |
+// typeurl.Register(&Foo{}, "Foo")
|
|
| 46 |
+// } |
|
| 47 |
+// |
|
| 48 |
+// This will register the type Foo with the url path "Foo". The arguments to |
|
| 49 |
+// Register are variadic, and are used to construct a url path. Consider this |
|
| 50 |
+// example, from the github.com/containerd/containerd/client package: |
|
| 51 |
+// |
|
| 52 |
+// func init() {
|
|
| 53 |
+// const prefix = "types.containerd.io" |
|
| 54 |
+// // register TypeUrls for commonly marshaled external types |
|
| 55 |
+// major := strconv.Itoa(specs.VersionMajor) |
|
| 56 |
+// typeurl.Register(&specs.Spec{}, prefix, "opencontainers/runtime-spec", major, "Spec")
|
|
| 57 |
+// // this function has more Register calls, which are elided. |
|
| 58 |
+// } |
|
| 59 |
+// |
|
| 60 |
+// This registers several types under a more complex url, which ends up mapping |
|
| 61 |
+// to `types.containerd.io/opencontainers/runtime-spec/1/Spec` (or some other |
|
| 62 |
+// value for major). |
|
| 63 |
+// |
|
| 64 |
+// Once a type is registered, it can be marshaled to a proto.Any message simply |
|
| 65 |
+// by calling `MarshalAny`, like this: |
|
| 66 |
+// |
|
| 67 |
+// foo := &Foo{Field1: "value1", Field2: "value2"}
|
|
| 68 |
+// anyFoo, err := typeurl.MarshalAny(foo) |
|
| 69 |
+// |
|
| 70 |
+// MarshalAny will resolve the correct URL for the type. If the type in |
|
| 71 |
+// question implements the proto.Message interface, then it will be marshaled |
|
| 72 |
+// as a proto message. Otherwise, it will be marshaled as json. This means that |
|
| 73 |
+// typeurl will work on any arbitrary data, whether or not it has a proto |
|
| 74 |
+// definition, as long as it can be serialized to json. |
|
| 75 |
+// |
|
| 76 |
+// To unmarshal, the process is simply inverse: |
|
| 77 |
+// |
|
| 78 |
+// iface, err := typeurl.UnmarshalAny(anyFoo) |
|
| 79 |
+// foo := iface.(*Foo) |
|
| 80 |
+// |
|
| 81 |
+// The correct type is automatically chosen from the type registry, and the |
|
| 82 |
+// returned interface can be cast straight to that type. |
| ... | ... |
@@ -78,7 +78,10 @@ func Is(any *types.Any, v interface{}) bool {
|
| 78 | 78 |
return any.TypeUrl == url |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 |
-// MarshalAny marshals the value v into an any with the correct TypeUrl |
|
| 81 |
+// MarshalAny marshals the value v into an any with the correct TypeUrl. |
|
| 82 |
+// If the provided object is already a proto.Any message, then it will be |
|
| 83 |
+// returned verbatim. If it is of type proto.Message, it will be marshaled as a |
|
| 84 |
+// protocol buffer. Otherwise, the object will be marshaled to json. |
|
| 82 | 85 |
func MarshalAny(v interface{}) (*types.Any, error) {
|
| 83 | 86 |
var marshal func(v interface{}) ([]byte, error)
|
| 84 | 87 |
switch t := v.(type) {
|