extensions.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 Google LLC. All Rights Reserved.
  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. package compiler
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os/exec"
  19. "strings"
  20. "github.com/golang/protobuf/proto"
  21. "github.com/golang/protobuf/ptypes/any"
  22. yaml "gopkg.in/yaml.v3"
  23. extensions "github.com/google/gnostic-models/extensions"
  24. )
  25. // ExtensionHandler describes a binary that is called by the compiler to handle specification extensions.
  26. type ExtensionHandler struct {
  27. Name string
  28. }
  29. // CallExtension calls a binary extension handler.
  30. func CallExtension(context *Context, in *yaml.Node, extensionName string) (handled bool, response *any.Any, err error) {
  31. if context == nil || context.ExtensionHandlers == nil {
  32. return false, nil, nil
  33. }
  34. handled = false
  35. for _, handler := range *(context.ExtensionHandlers) {
  36. response, err = handler.handle(in, extensionName)
  37. if response == nil {
  38. continue
  39. } else {
  40. handled = true
  41. break
  42. }
  43. }
  44. return handled, response, err
  45. }
  46. func (extensionHandlers *ExtensionHandler) handle(in *yaml.Node, extensionName string) (*any.Any, error) {
  47. if extensionHandlers.Name != "" {
  48. yamlData, _ := yaml.Marshal(in)
  49. request := &extensions.ExtensionHandlerRequest{
  50. CompilerVersion: &extensions.Version{
  51. Major: 0,
  52. Minor: 1,
  53. Patch: 0,
  54. },
  55. Wrapper: &extensions.Wrapper{
  56. Version: "unknown", // TODO: set this to the type/version of spec being parsed.
  57. Yaml: string(yamlData),
  58. ExtensionName: extensionName,
  59. },
  60. }
  61. requestBytes, _ := proto.Marshal(request)
  62. cmd := exec.Command(extensionHandlers.Name)
  63. cmd.Stdin = bytes.NewReader(requestBytes)
  64. output, err := cmd.Output()
  65. if err != nil {
  66. return nil, err
  67. }
  68. response := &extensions.ExtensionHandlerResponse{}
  69. err = proto.Unmarshal(output, response)
  70. if err != nil || !response.Handled {
  71. return nil, err
  72. }
  73. if len(response.Errors) != 0 {
  74. return nil, fmt.Errorf("Errors when parsing: %+v for field %s by vendor extension handler %s. Details %+v", in, extensionName, extensionHandlers.Name, strings.Join(response.Errors, ","))
  75. }
  76. return response.Value, nil
  77. }
  78. return nil, nil
  79. }