github.com/googleapis/api-linter@v1.65.2/rules/aip0191/java_outer_classname.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // 		https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0191
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"github.com/googleapis/api-linter/lint"
    23  	"github.com/googleapis/api-linter/locations"
    24  	"github.com/jhump/protoreflect/desc"
    25  	"github.com/stoewer/go-strcase"
    26  )
    27  
    28  var javaOuterClassname = &lint.FileRule{
    29  	Name: lint.NewRuleName(191, "java-outer-classname"),
    30  	OnlyIf: func(f *desc.FileDescriptor) bool {
    31  		return hasPackage(f) && !strings.HasSuffix(f.GetPackage(), ".master")
    32  	},
    33  	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
    34  		filename := filepath.Base(f.GetName())
    35  		want := strcase.UpperCamelCase(strings.ReplaceAll(filename, ".", "_"))
    36  
    37  		// We ignore case on the comparisons to not be too pedantic on compound
    38  		// word protos without underscores in the filename.
    39  		if !strings.EqualFold(f.GetFileOptions().GetJavaOuterClassname(), strings.ToUpper(want)) {
    40  			return []lint.Problem{{
    41  				Message: fmt.Sprintf(
    42  					"Proto files should set `option java_outer_classname = %q`.",
    43  					want,
    44  				),
    45  				Descriptor: f,
    46  				Location:   locations.FilePackage(f),
    47  			}}
    48  		}
    49  		return nil
    50  	},
    51  }