github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/scan/path.go (about)

     1  //go:build !go1.11
     2  // +build !go1.11
     3  
     4  // Copyright 2015 go-swagger maintainers
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //    http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package scan
    19  
    20  import (
    21  	"go/ast"
    22  	"regexp"
    23  	"strings"
    24  
    25  	"github.com/go-openapi/spec"
    26  )
    27  
    28  type parsedPathContent struct {
    29  	Method, Path, ID string
    30  	Tags             []string
    31  	Remaining        *ast.CommentGroup
    32  }
    33  
    34  func parsePathAnnotation(annotation *regexp.Regexp, lines []*ast.Comment) (cnt parsedPathContent) {
    35  	var justMatched bool
    36  
    37  	for _, cmt := range lines {
    38  		for _, line := range strings.Split(cmt.Text, "\n") {
    39  			matches := annotation.FindStringSubmatch(line)
    40  			if len(matches) > 3 {
    41  				cnt.Method, cnt.Path, cnt.ID = matches[1], matches[2], matches[len(matches)-1]
    42  				cnt.Tags = rxSpace.Split(matches[3], -1)
    43  				if len(matches[3]) == 0 {
    44  					cnt.Tags = nil
    45  				}
    46  				justMatched = true
    47  			} else if cnt.Method != "" {
    48  				if cnt.Remaining == nil {
    49  					cnt.Remaining = new(ast.CommentGroup)
    50  				}
    51  				if !justMatched || strings.TrimSpace(rxStripComments.ReplaceAllString(line, "")) != "" {
    52  					cc := new(ast.Comment)
    53  					cc.Slash = cmt.Slash
    54  					cc.Text = line
    55  					cnt.Remaining.List = append(cnt.Remaining.List, cc)
    56  					justMatched = false
    57  				}
    58  			}
    59  		}
    60  	}
    61  
    62  	return
    63  }
    64  
    65  func setPathOperation(method, id string, pthObj *spec.PathItem, op *spec.Operation) *spec.Operation {
    66  	if op == nil {
    67  		op = new(spec.Operation)
    68  		op.ID = id
    69  	}
    70  
    71  	switch strings.ToUpper(method) {
    72  	case "GET":
    73  		if pthObj.Get != nil {
    74  			if id == pthObj.Get.ID {
    75  				op = pthObj.Get
    76  			} else {
    77  				pthObj.Get = op
    78  			}
    79  		} else {
    80  			pthObj.Get = op
    81  		}
    82  
    83  	case "POST":
    84  		if pthObj.Post != nil {
    85  			if id == pthObj.Post.ID {
    86  				op = pthObj.Post
    87  			} else {
    88  				pthObj.Post = op
    89  			}
    90  		} else {
    91  			pthObj.Post = op
    92  		}
    93  
    94  	case "PUT":
    95  		if pthObj.Put != nil {
    96  			if id == pthObj.Put.ID {
    97  				op = pthObj.Put
    98  			} else {
    99  				pthObj.Put = op
   100  			}
   101  		} else {
   102  			pthObj.Put = op
   103  		}
   104  
   105  	case "PATCH":
   106  		if pthObj.Patch != nil {
   107  			if id == pthObj.Patch.ID {
   108  				op = pthObj.Patch
   109  			} else {
   110  				pthObj.Patch = op
   111  			}
   112  		} else {
   113  			pthObj.Patch = op
   114  		}
   115  
   116  	case "HEAD":
   117  		if pthObj.Head != nil {
   118  			if id == pthObj.Head.ID {
   119  				op = pthObj.Head
   120  			} else {
   121  				pthObj.Head = op
   122  			}
   123  		} else {
   124  			pthObj.Head = op
   125  		}
   126  
   127  	case "DELETE":
   128  		if pthObj.Delete != nil {
   129  			if id == pthObj.Delete.ID {
   130  				op = pthObj.Delete
   131  			} else {
   132  				pthObj.Delete = op
   133  			}
   134  		} else {
   135  			pthObj.Delete = op
   136  		}
   137  
   138  	case "OPTIONS":
   139  		if pthObj.Options != nil {
   140  			if id == pthObj.Options.ID {
   141  				op = pthObj.Options
   142  			} else {
   143  				pthObj.Options = op
   144  			}
   145  		} else {
   146  			pthObj.Options = op
   147  		}
   148  	}
   149  
   150  	return op
   151  }