Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | // http://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 com.google.devtools.build.lib.packages; |
| 16 | |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.packages.BuildType.DISTRIBUTIONS; |
| 18 | import static com.google.devtools.build.lib.packages.BuildType.FILESET_ENTRY_LIST; |
| 19 | import static com.google.devtools.build.lib.packages.BuildType.LABEL; |
| 20 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_DICT_UNARY; |
Michael Staib | 5e9e194 | 2017-02-23 19:06:31 +0000 | [diff] [blame] | 21 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_KEYED_STRING_DICT; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 22 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 23 | import static com.google.devtools.build.lib.packages.BuildType.LICENSE; |
| 24 | import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL; |
| 25 | import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL_LIST; |
| 26 | import static com.google.devtools.build.lib.packages.BuildType.OUTPUT; |
| 27 | import static com.google.devtools.build.lib.packages.BuildType.OUTPUT_LIST; |
| 28 | import static com.google.devtools.build.lib.packages.BuildType.TRISTATE; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 29 | import static com.google.devtools.build.lib.packages.Type.BOOLEAN; |
| 30 | import static com.google.devtools.build.lib.packages.Type.INTEGER; |
| 31 | import static com.google.devtools.build.lib.packages.Type.INTEGER_LIST; |
| 32 | import static com.google.devtools.build.lib.packages.Type.STRING; |
| 33 | import static com.google.devtools.build.lib.packages.Type.STRING_DICT; |
| 34 | import static com.google.devtools.build.lib.packages.Type.STRING_LIST; |
| 35 | import static com.google.devtools.build.lib.packages.Type.STRING_LIST_DICT; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 37 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.common.collect.ImmutableMap; |
| 39 | import com.google.devtools.build.lib.query2.proto.proto2api.Build.Attribute.Discriminator; |
| 40 | |
Michajlo Matijkiw | 937cb80 | 2016-07-14 22:31:34 +0000 | [diff] [blame] | 41 | /** Shared code used in proto buffer output for rules and rule classes. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | public class ProtoUtils { |
| 43 | /** |
| 44 | * This map contains all attribute types that are recognized by the protocol |
| 45 | * output formatter. |
| 46 | */ |
Mark Schaller | 4fa83ac | 2015-07-10 16:59:37 +0000 | [diff] [blame] | 47 | static final ImmutableMap<Type<?>, Discriminator> TYPE_MAP = |
| 48 | new ImmutableMap.Builder<Type<?>, Discriminator>() |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | .put(INTEGER, Discriminator.INTEGER) |
| 50 | .put(DISTRIBUTIONS, Discriminator.DISTRIBUTION_SET) |
| 51 | .put(LABEL, Discriminator.LABEL) |
| 52 | // NODEP_LABEL attributes are not really strings. This is implemented |
| 53 | // this way for the sake of backward compatibility. |
| 54 | .put(NODEP_LABEL, Discriminator.STRING) |
| 55 | .put(LABEL_LIST, Discriminator.LABEL_LIST) |
| 56 | .put(NODEP_LABEL_LIST, Discriminator.STRING_LIST) |
| 57 | .put(STRING, Discriminator.STRING) |
| 58 | .put(STRING_LIST, Discriminator.STRING_LIST) |
| 59 | .put(OUTPUT, Discriminator.OUTPUT) |
| 60 | .put(OUTPUT_LIST, Discriminator.OUTPUT_LIST) |
| 61 | .put(LICENSE, Discriminator.LICENSE) |
| 62 | .put(STRING_DICT, Discriminator.STRING_DICT) |
| 63 | .put(FILESET_ENTRY_LIST, Discriminator.FILESET_ENTRY_LIST) |
Lukacs Berki | f0cf38d | 2015-06-09 09:38:36 +0000 | [diff] [blame] | 64 | .put(LABEL_DICT_UNARY, Discriminator.LABEL_DICT_UNARY) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | .put(STRING_LIST_DICT, Discriminator.STRING_LIST_DICT) |
| 66 | .put(BOOLEAN, Discriminator.BOOLEAN) |
| 67 | .put(TRISTATE, Discriminator.TRISTATE) |
| 68 | .put(INTEGER_LIST, Discriminator.INTEGER_LIST) |
Michael Staib | 5e9e194 | 2017-02-23 19:06:31 +0000 | [diff] [blame] | 69 | .put(LABEL_KEYED_STRING_DICT, Discriminator.LABEL_KEYED_STRING_DICT) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | .build(); |
| 71 | |
Mark Schaller | 118aac7 | 2015-12-30 21:42:51 +0000 | [diff] [blame] | 72 | /** Returns the {@link Discriminator} value corresponding to the provided {@link Type}. */ |
| 73 | public static Discriminator getDiscriminatorFromType(Type<?> type) { |
Michajlo Matijkiw | 937cb80 | 2016-07-14 22:31:34 +0000 | [diff] [blame] | 74 | return Preconditions.checkNotNull(TYPE_MAP.get(type), "No discriminator found for %s", type); |
Mark Schaller | 4fa83ac | 2015-07-10 16:59:37 +0000 | [diff] [blame] | 75 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | } |