github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/edit/types.go (about)

     1  /*
     2  Copyright 2016 Google Inc. All Rights Reserved.
     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      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  // Type information for attributes.
    14  
    15  package edit
    16  
    17  import (
    18  	buildpb "github.com/cheshirekow/buildtools/build_proto"
    19  	"github.com/cheshirekow/buildtools/lang"
    20  	"github.com/cheshirekow/buildtools/tables"
    21  )
    22  
    23  var typeOf = lang.TypeOf
    24  
    25  // IsList returns true for all attributes whose type is a list.
    26  func IsList(attr string) bool {
    27  	overrideValue, isOverridden := tables.IsListArg[attr]
    28  	if isOverridden {
    29  		return overrideValue
    30  	}
    31  	// It stands to reason that a sortable list must be a list.
    32  	isSortableList := tables.IsSortableListArg[attr]
    33  	if isSortableList {
    34  		return true
    35  	}
    36  	ty := typeOf[attr]
    37  	return ty == buildpb.Attribute_STRING_LIST ||
    38  		ty == buildpb.Attribute_LABEL_LIST ||
    39  		ty == buildpb.Attribute_OUTPUT_LIST ||
    40  		ty == buildpb.Attribute_FILESET_ENTRY_LIST ||
    41  		ty == buildpb.Attribute_INTEGER_LIST ||
    42  		ty == buildpb.Attribute_LICENSE ||
    43  		ty == buildpb.Attribute_DISTRIBUTION_SET
    44  }
    45  
    46  // IsIntList returns true for all attributes whose type is an int list.
    47  func IsIntList(attr string) bool {
    48  	return typeOf[attr] == buildpb.Attribute_INTEGER_LIST
    49  }
    50  
    51  // IsString returns true for all attributes whose type is a string or a label.
    52  func IsString(attr string) bool {
    53  	ty := typeOf[attr]
    54  	return ty == buildpb.Attribute_LABEL ||
    55  		ty == buildpb.Attribute_STRING ||
    56  		ty == buildpb.Attribute_OUTPUT
    57  }
    58  
    59  // IsStringDict returns true for all attributes whose type is a string dictionary.
    60  func IsStringDict(attr string) bool {
    61  	return typeOf[attr] == buildpb.Attribute_STRING_DICT
    62  }
    63  
    64  // ContainsLabels returns true for all attributes whose type is a label or a label list.
    65  func ContainsLabels(attr string) bool {
    66  	ty := typeOf[attr]
    67  	return ty == buildpb.Attribute_LABEL_LIST ||
    68  		ty == buildpb.Attribute_LABEL
    69  }