extension-handler.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2017 Google Inc. 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. "errors"
  21. "github.com/golang/protobuf/proto"
  22. "github.com/golang/protobuf/ptypes/any"
  23. ext_plugin "github.com/googleapis/gnostic/extensions"
  24. yaml "gopkg.in/yaml.v2"
  25. )
  26. // ExtensionHandler describes a binary that is called by the compiler to handle specification extensions.
  27. type ExtensionHandler struct {
  28. Name string
  29. }
  30. // HandleExtension calls a binary extension handler.
  31. func HandleExtension(context *Context, in interface{}, extensionName string) (bool, *any.Any, error) {
  32. handled := false
  33. var errFromPlugin error
  34. var outFromPlugin *any.Any
  35. if context != nil && context.ExtensionHandlers != nil && len(*(context.ExtensionHandlers)) != 0 {
  36. for _, customAnyProtoGenerator := range *(context.ExtensionHandlers) {
  37. outFromPlugin, errFromPlugin = customAnyProtoGenerator.handle(in, extensionName)
  38. if outFromPlugin == nil {
  39. continue
  40. } else {
  41. handled = true
  42. break
  43. }
  44. }
  45. }
  46. return handled, outFromPlugin, errFromPlugin
  47. }
  48. func (extensionHandlers *ExtensionHandler) handle(in interface{}, extensionName string) (*any.Any, error) {
  49. if extensionHandlers.Name != "" {
  50. binary, _ := yaml.Marshal(in)
  51. request := &ext_plugin.ExtensionHandlerRequest{}
  52. version := &ext_plugin.Version{}
  53. version.Major = 0
  54. version.Minor = 1
  55. version.Patch = 0
  56. request.CompilerVersion = version
  57. request.Wrapper = &ext_plugin.Wrapper{}
  58. request.Wrapper.Version = "v2"
  59. request.Wrapper.Yaml = string(binary)
  60. request.Wrapper.ExtensionName = extensionName
  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. fmt.Printf("Error: %+v\n", err)
  67. return nil, err
  68. }
  69. response := &ext_plugin.ExtensionHandlerResponse{}
  70. err = proto.Unmarshal(output, response)
  71. if err != nil {
  72. fmt.Printf("Error: %+v\n", err)
  73. fmt.Printf("%s\n", string(output))
  74. return nil, err
  75. }
  76. if !response.Handled {
  77. return nil, nil
  78. }
  79. if len(response.Error) != 0 {
  80. message := fmt.Sprintf("Errors when parsing: %+v for field %s by vendor extension handler %s. Details %+v", in, extensionName, extensionHandlers.Name, strings.Join(response.Error, ","))
  81. return nil, errors.New(message)
  82. }
  83. return response.Value, nil
  84. }
  85. return nil, nil
  86. }