cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 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 | |
| 17 | import com.google.common.collect.ImmutableMap; |
| 18 | import com.google.devtools.build.lib.events.Location; |
| 19 | import com.google.devtools.build.lib.skylarkbuildapi.StructApi; |
| 20 | import com.google.devtools.build.lib.syntax.EvalException; |
| 21 | import com.google.devtools.build.lib.syntax.SkylarkDict; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | /** |
| 25 | * The provider for the built-in type {@code struct}. |
| 26 | * |
| 27 | * <p>Its singleton instance is {@link StructProvider#STRUCT}. |
| 28 | */ |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 29 | public final class StructProvider extends BuiltinProvider<StructImpl> |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 30 | implements StructApi.StructProviderApi { |
| 31 | |
| 32 | /** "struct" function. */ |
| 33 | public static final StructProvider STRUCT = new StructProvider(); |
| 34 | |
| 35 | StructProvider() { |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 36 | super("struct", StructImpl.class); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
cparsons | 4ebf6c0 | 2018-08-17 14:49:36 -0700 | [diff] [blame] | 40 | public StructImpl createStruct(SkylarkDict<?, ?> kwargs, Location loc) throws EvalException { |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 41 | Map<String, Object> kwargsMap = kwargs.getContents(String.class, Object.class, "kwargs"); |
| 42 | if (kwargsMap.containsKey("to_json")) { |
| 43 | throw new EvalException(loc, "cannot override built-in struct function 'to_json'"); |
| 44 | } |
| 45 | if (kwargsMap.containsKey("to_proto")) { |
| 46 | throw new EvalException(loc, "cannot override built-in struct function 'to_proto'"); |
| 47 | } |
| 48 | return SkylarkInfo.createSchemaless(this, kwargsMap, loc); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
hlopko | e1d9b58 | 2018-07-23 03:06:32 -0700 | [diff] [blame] | 52 | * Creates a struct with the given field values and message format for unknown fields. |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 53 | * |
| 54 | * <p>The custom message is useful for objects that have fields but aren't exactly used as |
| 55 | * providers, such as the {@code native} object, and the struct fields of {@code ctx} like |
| 56 | * {@code ctx.attr}. |
| 57 | * */ |
| 58 | public SkylarkInfo create( |
| 59 | Map<String, Object> values, String errorMessageFormatForUnknownField) { |
| 60 | return SkylarkInfo.createSchemalessWithCustomMessage( |
| 61 | this, values, errorMessageFormatForUnknownField); |
| 62 | } |
| 63 | |
| 64 | /** Creates an empty struct with the given location. */ |
| 65 | public SkylarkInfo createEmpty(Location loc) { |
| 66 | return SkylarkInfo.createSchemaless(this, ImmutableMap.of(), loc); |
| 67 | } |
| 68 | } |