...

Source file src/github.com/bazelbuild/buildtools/edit/types.go

Documentation: github.com/bazelbuild/buildtools/edit

     1  /*
     2  Copyright 2016 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Type information for attributes.
    18  
    19  package edit
    20  
    21  import (
    22  	buildpb "github.com/bazelbuild/buildtools/build_proto"
    23  	"github.com/bazelbuild/buildtools/lang"
    24  	"github.com/bazelbuild/buildtools/tables"
    25  )
    26  
    27  var typeOf = lang.TypeOf
    28  
    29  // IsList returns true for all attributes whose type is a list.
    30  func IsList(attr string) bool {
    31  	overrideValue, isOverridden := tables.IsListArg[attr]
    32  	if isOverridden {
    33  		return overrideValue
    34  	}
    35  	// It stands to reason that a sortable list must be a list.
    36  	isSortableList := tables.IsSortableListArg[attr]
    37  	if isSortableList {
    38  		return true
    39  	}
    40  	ty := typeOf[attr]
    41  	return ty == buildpb.Attribute_STRING_LIST ||
    42  		ty == buildpb.Attribute_LABEL_LIST ||
    43  		ty == buildpb.Attribute_OUTPUT_LIST ||
    44  		ty == buildpb.Attribute_FILESET_ENTRY_LIST ||
    45  		ty == buildpb.Attribute_INTEGER_LIST ||
    46  		ty == buildpb.Attribute_LICENSE ||
    47  		ty == buildpb.Attribute_DISTRIBUTION_SET
    48  }
    49  
    50  // IsIntList returns true for all attributes whose type is an int list.
    51  func IsIntList(attr string) bool {
    52  	return typeOf[attr] == buildpb.Attribute_INTEGER_LIST
    53  }
    54  
    55  // IsString returns true for all attributes whose type is a string or a label.
    56  func IsString(attr string) bool {
    57  	ty := typeOf[attr]
    58  	return ty == buildpb.Attribute_LABEL ||
    59  		ty == buildpb.Attribute_STRING ||
    60  		ty == buildpb.Attribute_OUTPUT
    61  }
    62  
    63  // IsStringDict returns true for all attributes whose type is a string dictionary.
    64  func IsStringDict(attr string) bool {
    65  	return typeOf[attr] == buildpb.Attribute_STRING_DICT
    66  }
    67  
    68  // ContainsLabels returns true for all attributes whose type is a label or a label list.
    69  func ContainsLabels(kind, attr string) bool {
    70  	if kind == "package_group" && attr == "packages" {
    71  		// "package_group" is a special rule and its "packages" attribute is not a list of labels.
    72  		return false
    73  	}
    74  	ty := typeOf[attr]
    75  	return ty == buildpb.Attribute_LABEL_LIST ||
    76  		ty == buildpb.Attribute_LABEL
    77  }
    78  

View as plain text