github.com/Zenithar/prototool@v1.3.0/internal/lint/check_file_options_required.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package lint
    22  
    23  import (
    24  	"text/scanner"
    25  
    26  	"github.com/emicklei/proto"
    27  	"github.com/uber/prototool/internal/text"
    28  )
    29  
    30  var fileOptionsRequireGoPackageLinter = NewLinter(
    31  	"FILE_OPTIONS_REQUIRE_GO_PACKAGE",
    32  	`Verifies that the file option "go_package" is set.`,
    33  	newCheckFileOptionsRequire("go_package"),
    34  )
    35  
    36  var fileOptionsRequireJavaMultipleFilesLinter = NewLinter(
    37  	"FILE_OPTIONS_REQUIRE_JAVA_MULTIPLE_FILES",
    38  	`Verifies that the file option "java_multiple_files" is set.`,
    39  	newCheckFileOptionsRequire("java_multiple_files"),
    40  )
    41  
    42  var fileOptionsRequireJavaOuterClassnameLinter = NewLinter(
    43  	"FILE_OPTIONS_REQUIRE_JAVA_OUTER_CLASSNAME",
    44  	`Verifies that the file option "java_outer_classname" is set.`,
    45  	newCheckFileOptionsRequire("java_outer_classname"),
    46  )
    47  
    48  var fileOptionsRequireJavaPackageLinter = NewLinter(
    49  	"FILE_OPTIONS_REQUIRE_JAVA_PACKAGE",
    50  	`Verifies that the file option "java_package" is set.`,
    51  	newCheckFileOptionsRequire("java_package"),
    52  )
    53  
    54  func newCheckFileOptionsRequire(fileOption string) func(func(*text.Failure), string, []*proto.Proto) error {
    55  	return func(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
    56  		return runVisitor(&fileOptionsRequireVisitor{
    57  			baseAddVisitor: newBaseAddVisitor(add),
    58  			fileOption:     fileOption,
    59  		}, descriptors)
    60  	}
    61  }
    62  
    63  type fileOptionsRequireVisitor struct {
    64  	baseAddVisitor
    65  
    66  	fileOption string
    67  
    68  	filename string
    69  	seen     bool
    70  }
    71  
    72  func (v *fileOptionsRequireVisitor) OnStart(descriptor *proto.Proto) error {
    73  	v.filename = descriptor.Filename
    74  	v.seen = false
    75  	return nil
    76  }
    77  
    78  func (v *fileOptionsRequireVisitor) VisitOption(element *proto.Option) {
    79  	// TODO: not validating this is a file option, or are we since we're not recursing on other elements?
    80  	if element.Name == v.fileOption {
    81  		v.seen = true
    82  	}
    83  }
    84  
    85  func (v *fileOptionsRequireVisitor) Finally() error {
    86  	if !v.seen {
    87  		v.AddFailuref(scanner.Position{Filename: v.filename}, "File option %q is required.", v.fileOption)
    88  	}
    89  	return nil
    90  }