1 // Copyright 2017 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package openapiextension_v1
22 "github.com/golang/protobuf/proto"
23 "github.com/golang/protobuf/ptypes"
26 type documentHandler func(version string, extensionName string, document string)
27 type extensionHandler func(name string, yamlInput string) (bool, proto.Message, error)
29 func forInputYamlFromOpenapic(handler documentHandler) {
30 data, err := ioutil.ReadAll(os.Stdin)
32 fmt.Println("File error:", err.Error())
36 fmt.Println("No input data.")
39 request := &ExtensionHandlerRequest{}
40 err = proto.Unmarshal(data, request)
42 fmt.Println("Input error:", err.Error())
45 handler(request.Wrapper.Version, request.Wrapper.ExtensionName, request.Wrapper.Yaml)
48 // ProcessExtension calles the handler for a specified extension.
49 func ProcessExtension(handleExtension extensionHandler) {
50 response := &ExtensionHandlerResponse{}
51 forInputYamlFromOpenapic(
52 func(version string, extensionName string, yamlInput string) {
53 var newObject proto.Message
56 handled, newObject, err := handleExtension(extensionName, yamlInput)
58 responseBytes, _ := proto.Marshal(response)
59 os.Stdout.Write(responseBytes)
63 // If we reach here, then the extension is handled
64 response.Handled = true
66 response.Error = append(response.Error, err.Error())
67 responseBytes, _ := proto.Marshal(response)
68 os.Stdout.Write(responseBytes)
71 response.Value, err = ptypes.MarshalAny(newObject)
73 response.Error = append(response.Error, err.Error())
74 responseBytes, _ := proto.Marshal(response)
75 os.Stdout.Write(responseBytes)
80 responseBytes, _ := proto.Marshal(response)
81 os.Stdout.Write(responseBytes)