path_template.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2016, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. package gax
  30. import (
  31. "errors"
  32. "fmt"
  33. "strings"
  34. )
  35. type matcher interface {
  36. match([]string) (int, error)
  37. String() string
  38. }
  39. type segment struct {
  40. matcher
  41. name string
  42. }
  43. type labelMatcher string
  44. func (ls labelMatcher) match(segments []string) (int, error) {
  45. if len(segments) == 0 {
  46. return 0, fmt.Errorf("expected %s but no more segments found", ls)
  47. }
  48. if segments[0] != string(ls) {
  49. return 0, fmt.Errorf("expected %s but got %s", ls, segments[0])
  50. }
  51. return 1, nil
  52. }
  53. func (ls labelMatcher) String() string {
  54. return string(ls)
  55. }
  56. type wildcardMatcher int
  57. func (wm wildcardMatcher) match(segments []string) (int, error) {
  58. if len(segments) == 0 {
  59. return 0, errors.New("no more segments found")
  60. }
  61. return 1, nil
  62. }
  63. func (wm wildcardMatcher) String() string {
  64. return "*"
  65. }
  66. type pathWildcardMatcher int
  67. func (pwm pathWildcardMatcher) match(segments []string) (int, error) {
  68. length := len(segments) - int(pwm)
  69. if length <= 0 {
  70. return 0, errors.New("not sufficient segments are supplied for path wildcard")
  71. }
  72. return length, nil
  73. }
  74. func (pwm pathWildcardMatcher) String() string {
  75. return "**"
  76. }
  77. type ParseError struct {
  78. Pos int
  79. Template string
  80. Message string
  81. }
  82. func (pe ParseError) Error() string {
  83. return fmt.Sprintf("at %d of template '%s', %s", pe.Pos, pe.Template, pe.Message)
  84. }
  85. // PathTemplate manages the template to build and match with paths used
  86. // by API services. It holds a template and variable names in it, and
  87. // it can extract matched patterns from a path string or build a path
  88. // string from a binding.
  89. //
  90. // See http.proto in github.com/googleapis/googleapis/ for the details of
  91. // the template syntax.
  92. type PathTemplate struct {
  93. segments []segment
  94. }
  95. // NewPathTemplate parses a path template, and returns a PathTemplate
  96. // instance if successful.
  97. func NewPathTemplate(template string) (*PathTemplate, error) {
  98. return parsePathTemplate(template)
  99. }
  100. // MustCompilePathTemplate is like NewPathTemplate but panics if the
  101. // expression cannot be parsed. It simplifies safe initialization of
  102. // global variables holding compiled regular expressions.
  103. func MustCompilePathTemplate(template string) *PathTemplate {
  104. pt, err := NewPathTemplate(template)
  105. if err != nil {
  106. panic(err)
  107. }
  108. return pt
  109. }
  110. // Match attempts to match the given path with the template, and returns
  111. // the mapping of the variable name to the matched pattern string.
  112. func (pt *PathTemplate) Match(path string) (map[string]string, error) {
  113. paths := strings.Split(path, "/")
  114. values := map[string]string{}
  115. for _, segment := range pt.segments {
  116. length, err := segment.match(paths)
  117. if err != nil {
  118. return nil, err
  119. }
  120. if segment.name != "" {
  121. value := strings.Join(paths[:length], "/")
  122. if oldValue, ok := values[segment.name]; ok {
  123. values[segment.name] = oldValue + "/" + value
  124. } else {
  125. values[segment.name] = value
  126. }
  127. }
  128. paths = paths[length:]
  129. }
  130. if len(paths) != 0 {
  131. return nil, fmt.Errorf("Trailing path %s remains after the matching", strings.Join(paths, "/"))
  132. }
  133. return values, nil
  134. }
  135. // Render creates a path string from its template and the binding from
  136. // the variable name to the value.
  137. func (pt *PathTemplate) Render(binding map[string]string) (string, error) {
  138. result := make([]string, 0, len(pt.segments))
  139. var lastVariableName string
  140. for _, segment := range pt.segments {
  141. name := segment.name
  142. if lastVariableName != "" && name == lastVariableName {
  143. continue
  144. }
  145. lastVariableName = name
  146. if name == "" {
  147. result = append(result, segment.String())
  148. } else if value, ok := binding[name]; ok {
  149. result = append(result, value)
  150. } else {
  151. return "", fmt.Errorf("%s is not found", name)
  152. }
  153. }
  154. built := strings.Join(result, "/")
  155. return built, nil
  156. }