blob: 12484ea45349ecd558bbe0923f361d866ebfadb0 [file] [log] [blame]
cparsons0c5c1c62018-05-24 10:37:03 -07001// 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
15package com.google.devtools.build.lib.packages;
16
17import com.google.common.collect.ImmutableMap;
18import com.google.devtools.build.lib.events.Location;
19import com.google.devtools.build.lib.skylarkbuildapi.StructApi;
20import com.google.devtools.build.lib.syntax.EvalException;
21import com.google.devtools.build.lib.syntax.SkylarkDict;
22import 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 */
cparsons4ebf6c02018-08-17 14:49:36 -070029public final class StructProvider extends BuiltinProvider<StructImpl>
cparsons0c5c1c62018-05-24 10:37:03 -070030 implements StructApi.StructProviderApi {
31
32 /** "struct" function. */
33 public static final StructProvider STRUCT = new StructProvider();
34
35 StructProvider() {
cparsons4ebf6c02018-08-17 14:49:36 -070036 super("struct", StructImpl.class);
cparsons0c5c1c62018-05-24 10:37:03 -070037 }
38
39 @Override
cparsons4ebf6c02018-08-17 14:49:36 -070040 public StructImpl createStruct(SkylarkDict<?, ?> kwargs, Location loc) throws EvalException {
cparsons07460fc2018-06-20 10:41:48 -070041 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);
cparsons0c5c1c62018-05-24 10:37:03 -070049 }
50
51 /**
hlopkoe1d9b582018-07-23 03:06:32 -070052 * Creates a struct with the given field values and message format for unknown fields.
cparsons0c5c1c62018-05-24 10:37:03 -070053 *
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}