github.com/jamescostian/go-swagger@v0.30.4-0.20221130163922-68364d6b567b/scan/operations.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  	"fmt"
    22  	"go/ast"
    23  
    24  	"github.com/go-openapi/spec"
    25  
    26  	"golang.org/x/tools/go/loader"
    27  )
    28  
    29  func newOperationsParser(prog *loader.Program) *operationsParser {
    30  	return &operationsParser{
    31  		program: prog,
    32  	}
    33  }
    34  
    35  type operationsParser struct {
    36  	program     *loader.Program
    37  	definitions map[string]spec.Schema
    38  	operations  map[string]*spec.Operation
    39  	responses   map[string]spec.Response
    40  }
    41  
    42  func (op *operationsParser) Parse(gofile *ast.File, target interface{}, includeTags map[string]bool, excludeTags map[string]bool) error {
    43  	tgt := target.(*spec.Paths)
    44  	for _, comsec := range gofile.Comments {
    45  		content := parsePathAnnotation(rxOperation, comsec.List)
    46  
    47  		if content.Method == "" {
    48  			continue // it's not, next!
    49  		}
    50  
    51  		if !shouldAcceptTag(content.Tags, includeTags, excludeTags) {
    52  			if Debug {
    53  				fmt.Printf("operation %s %s is ignored due to tag rules\n", content.Method, content.Path)
    54  			}
    55  			continue
    56  		}
    57  
    58  		pthObj := tgt.Paths[content.Path]
    59  
    60  		op := setPathOperation(
    61  			content.Method, content.ID,
    62  			&pthObj, op.operations[content.ID])
    63  
    64  		op.Tags = content.Tags
    65  
    66  		sp := new(yamlSpecScanner)
    67  		sp.setTitle = func(lines []string) { op.Summary = joinDropLast(lines) }
    68  		sp.setDescription = func(lines []string) { op.Description = joinDropLast(lines) }
    69  
    70  		if err := sp.Parse(content.Remaining); err != nil {
    71  			return fmt.Errorf("operation (%s): %v", op.ID, err)
    72  		}
    73  		if err := sp.UnmarshalSpec(op.UnmarshalJSON); err != nil {
    74  			return fmt.Errorf("operation (%s): %v", op.ID, err)
    75  		}
    76  
    77  		if tgt.Paths == nil {
    78  			tgt.Paths = make(map[string]spec.PathItem)
    79  		}
    80  
    81  		tgt.Paths[content.Path] = pthObj
    82  	}
    83  
    84  	return nil
    85  }