github.com/Zenithar/prototool@v1.3.0/internal/lint/check_file_options_equal.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  	"fmt"
    25  
    26  	"github.com/emicklei/proto"
    27  	"github.com/uber/prototool/internal/protostrs"
    28  	"github.com/uber/prototool/internal/text"
    29  )
    30  
    31  var fileOptionsEqualGoPackagePbSuffixLinter = NewLinter(
    32  	"FILE_OPTIONS_EQUAL_GO_PACKAGE_PB_SUFFIX",
    33  	`Verifies that the file option "go_package" is equal to $(basename PACKAGE)pb.`,
    34  	newCheckFileOptionsEqual("go_package", func(_ *proto.Proto, pkg *proto.Package) string {
    35  		return protostrs.GoPackage(pkg.Name)
    36  	}),
    37  )
    38  
    39  var fileOptionsEqualJavaMultipleFilesTrueLinter = NewLinter(
    40  	"FILE_OPTIONS_EQUAL_JAVA_MULTIPLE_FILES_TRUE",
    41  	`Verifies that the file option "java_multiple_files" is equal to true.`,
    42  	newCheckFileOptionsEqual("java_multiple_files", func(*proto.Proto, *proto.Package) string {
    43  		return "true"
    44  	}),
    45  )
    46  
    47  var fileOptionsEqualJavaOuterClassnameProtoSuffixLinter = NewLinter(
    48  	"FILE_OPTIONS_EQUAL_JAVA_OUTER_CLASSNAME_PROTO_SUFFIX",
    49  	`Verifies that the file option "java_outer_classname" is equal to $(upperCamelCase $(basename FILE))Proto.`,
    50  	newCheckFileOptionsEqual("java_outer_classname", func(descriptor *proto.Proto, _ *proto.Package) string {
    51  		return protostrs.JavaOuterClassname(descriptor.Filename)
    52  	}),
    53  )
    54  
    55  var fileOptionsEqualJavaPackageComPrefixLinter = NewLinter(
    56  	"FILE_OPTIONS_EQUAL_JAVA_PACKAGE_COM_PREFIX",
    57  	`Verifies that the file option "java_package" is equal to com.PACKAGE.`,
    58  	newCheckFileOptionsEqual("java_package", func(_ *proto.Proto, pkg *proto.Package) string {
    59  		return protostrs.JavaPackage(pkg.Name)
    60  	}),
    61  )
    62  
    63  func newCheckFileOptionsEqual(fileOption string, expectedValueFunc func(*proto.Proto, *proto.Package) string) func(func(*text.Failure), string, []*proto.Proto) error {
    64  	return func(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
    65  		return runVisitor(&fileOptionsEqualVisitor{
    66  			baseAddVisitor:    newBaseAddVisitor(add),
    67  			fileOption:        fileOption,
    68  			expectedValueFunc: expectedValueFunc,
    69  		}, descriptors)
    70  	}
    71  }
    72  
    73  type fileOptionsEqualVisitor struct {
    74  	baseAddVisitor
    75  
    76  	fileOption        string
    77  	expectedValueFunc func(*proto.Proto, *proto.Package) string
    78  
    79  	descriptor *proto.Proto
    80  	pkg        *proto.Package
    81  	option     *proto.Option
    82  }
    83  
    84  func (v *fileOptionsEqualVisitor) OnStart(descriptor *proto.Proto) error {
    85  	v.descriptor = descriptor
    86  	v.pkg = nil
    87  	v.option = nil
    88  	return nil
    89  }
    90  
    91  func (v *fileOptionsEqualVisitor) VisitPackage(element *proto.Package) {
    92  	v.pkg = element
    93  }
    94  
    95  func (v *fileOptionsEqualVisitor) VisitOption(element *proto.Option) {
    96  	// TODO: not validating this is a file option, or are we since we're not recursing on other elements?
    97  	if element.Name == v.fileOption {
    98  		v.option = element
    99  	}
   100  }
   101  
   102  func (v *fileOptionsEqualVisitor) Finally() error {
   103  	if v.descriptor == nil || v.pkg == nil || v.option == nil {
   104  		// do not do anything, other linters should verify that the file option exists
   105  		// this makes it possible to be optional if a required file option linter is suppressed
   106  		// TODO make sure this is consistent across all linters
   107  		return nil
   108  	}
   109  	if v.descriptor.Filename == "" {
   110  		// if this isn't set, we made a mistake setting this up, return a system error
   111  		return fmt.Errorf("expected filename to be set for descriptor %v in checkFileOptionsEqual linter", v.descriptor)
   112  	}
   113  	// TODO: handle AggregatedConstants
   114  	value := v.option.Constant.Source
   115  	expectedValue := v.expectedValueFunc(v.descriptor, v.pkg)
   116  	if expectedValue != value {
   117  		v.AddFailuref(v.option.Position, "Expected %q for option %q but was %q.", expectedValue, v.option.Name, value)
   118  	}
   119  	return nil
   120  }