Skip to content

Commit

Permalink
Refactor ProviderInfo into a proto
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 252652040
  • Loading branch information
blossommojekwu authored and copybara-github committed Jun 11, 2019
1 parent 6595661 commit 25d2748
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 42 deletions.
35 changes: 21 additions & 14 deletions src/main/java/com/google/devtools/build/skydoc/SkydocMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@
import com.google.devtools.build.skydoc.rendering.DocstringParseException;
import com.google.devtools.build.skydoc.rendering.FunctionUtil;
import com.google.devtools.build.skydoc.rendering.MarkdownRenderer;
import com.google.devtools.build.skydoc.rendering.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.ProviderInfoWrapper;
import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.UserDefinedFunctionInfo;
import com.google.devtools.common.options.OptionsParser;
Expand Down Expand Up @@ -204,7 +205,7 @@ public static void main(String[] args)
Label targetFileLabel = Label.parseAbsolute(targetFileLabelString, ImmutableMap.of());

ImmutableMap.Builder<String, RuleInfo> ruleInfoMap = ImmutableMap.builder();
ImmutableMap.Builder<String, ProviderInfo> providerInfoMap = ImmutableMap.builder();
ImmutableMap.Builder<String, ProviderInfoWrapper> providerInfoMap = ImmutableMap.builder();
ImmutableMap.Builder<String, UserDefinedFunction> userDefinedFunctions = ImmutableMap.builder();

try {
Expand All @@ -226,7 +227,7 @@ public static void main(String[] args)
ruleInfoMap.build().entrySet().stream()
.filter(entry -> validSymbolName(symbolNames, entry.getKey()))
.collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
Map<String, ProviderInfo> filteredProviderInfos =
Map<String, ProviderInfoWrapper> filteredProviderInfos =
providerInfoMap.build().entrySet().stream()
.filter(entry -> validSymbolName(symbolNames, entry.getKey()))
.collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
Expand Down Expand Up @@ -273,10 +274,13 @@ private static void printRuleInfos(
}

private static void printProviderInfos(
PrintWriter printWriter, MarkdownRenderer renderer, Map<String, ProviderInfo> providerInfos)
PrintWriter printWriter,
MarkdownRenderer renderer,
Map<String, ProviderInfoWrapper> providerInfos)
throws IOException {
for (Entry<String, ProviderInfo> entry : providerInfos.entrySet()) {
printProviderInfo(printWriter, renderer, entry.getKey(), entry.getValue());
for (Entry<String, ProviderInfoWrapper> entry : providerInfos.entrySet()) {
ProviderInfoWrapper infoWrapper = entry.getValue();
printProviderInfo(printWriter, renderer, entry.getKey(), infoWrapper.getProviderInfo());
printWriter.println();
}
}
Expand All @@ -286,6 +290,7 @@ private static void printUserDefinedFunctions(
MarkdownRenderer renderer,
Map<String, UserDefinedFunction> userDefinedFunctions)
throws IOException {

for (Entry<String, UserDefinedFunction> entry : userDefinedFunctions.entrySet()) {
try {
UserDefinedFunctionInfo functionInfo =
Expand Down Expand Up @@ -342,22 +347,24 @@ public Environment eval(
StarlarkSemantics semantics,
Label label,
ImmutableMap.Builder<String, RuleInfo> ruleInfoMap,
ImmutableMap.Builder<String, ProviderInfo> providerInfoMap,
ImmutableMap.Builder<String, ProviderInfoWrapper> providerInfoMap,
ImmutableMap.Builder<String, UserDefinedFunction> userDefinedFunctionMap)
throws InterruptedException, IOException, LabelSyntaxException, EvalException,
StarlarkEvaluationException {

List<RuleInfoWrapper> ruleInfoList = new ArrayList<>();
List<ProviderInfo> providerInfoList = new ArrayList<>();

List<ProviderInfoWrapper> providerInfoList = new ArrayList<>();
Environment env = recursiveEval(semantics, label, ruleInfoList, providerInfoList);

Map<BaseFunction, RuleInfoWrapper> ruleFunctions =
ruleInfoList.stream()
.collect(
Collectors.toMap(RuleInfoWrapper::getIdentifierFunction, Functions.identity()));
Map<BaseFunction, ProviderInfo> providerInfos =

Map<BaseFunction, ProviderInfoWrapper> providerInfos =
providerInfoList.stream()
.collect(Collectors.toMap(ProviderInfo::getIdentifier, Functions.identity()));
.collect(Collectors.toMap(ProviderInfoWrapper::getIdentifier, Functions.identity()));

// Sort the bindings so their ordering is deterministic.
TreeMap<String, Object> sortedBindings = new TreeMap<>(env.getGlobals().getExportedBindings());
Expand All @@ -368,7 +375,7 @@ public Environment eval(
ruleInfoMap.put(envEntry.getKey(), ruleInfo);
}
if (providerInfos.containsKey(envEntry.getValue())) {
ProviderInfo providerInfo = providerInfos.get(envEntry.getValue());
ProviderInfoWrapper providerInfo = providerInfos.get(envEntry.getValue());
providerInfoMap.put(envEntry.getKey(), providerInfo);
}
if (envEntry.getValue() instanceof UserDefinedFunction) {
Expand Down Expand Up @@ -403,7 +410,7 @@ private Environment recursiveEval(
StarlarkSemantics semantics,
Label label,
List<RuleInfoWrapper> ruleInfoList,
List<ProviderInfo> providerInfoList)
List<ProviderInfoWrapper> providerInfoList)
throws InterruptedException, IOException, LabelSyntaxException, StarlarkEvaluationException {
Path path = pathOfLabel(label);

Expand Down Expand Up @@ -471,7 +478,7 @@ private Environment evalSkylarkBody(
BuildFileAST buildFileAST,
Map<String, Extension> imports,
List<RuleInfoWrapper> ruleInfoList,
List<ProviderInfo> providerInfoList)
List<ProviderInfoWrapper> providerInfoList)
throws InterruptedException, StarlarkEvaluationException {

Environment env =
Expand All @@ -496,7 +503,7 @@ private Environment evalSkylarkBody(
* invocation information will be added
*/
private static GlobalFrame globalFrame(
List<RuleInfoWrapper> ruleInfoList, List<ProviderInfo> providerInfoList) {
List<RuleInfoWrapper> ruleInfoList, List<ProviderInfoWrapper> providerInfoList) {
TopLevelBootstrap topLevelBootstrap =
new TopLevelBootstrap(
new FakeBuildApiGlobals(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkType;
import com.google.devtools.build.skydoc.rendering.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.ProviderInfoWrapper;
import com.google.devtools.build.skydoc.rendering.RuleInfoWrapper;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -56,7 +58,8 @@ public class FakeSkylarkRuleFunctionsApi implements SkylarkRuleFunctionsApi<File
private static final FakeDescriptor IMPLICIT_NAME_ATTRIBUTE_DESCRIPTOR =
new FakeDescriptor(AttributeType.NAME, "A unique name for this target.", true);
private final List<RuleInfoWrapper> ruleInfoList;
private final List<ProviderInfo> providerInfoList;

private final List<ProviderInfoWrapper> providerInfoList;

/**
* Constructor.
Expand All @@ -67,7 +70,7 @@ public class FakeSkylarkRuleFunctionsApi implements SkylarkRuleFunctionsApi<File
* information will be added
*/
public FakeSkylarkRuleFunctionsApi(
List<RuleInfoWrapper> ruleInfoList, List<ProviderInfo> providerInfoList) {
List<RuleInfoWrapper> ruleInfoList, List<ProviderInfoWrapper> providerInfoList) {
this.ruleInfoList = ruleInfoList;
this.providerInfoList = providerInfoList;
}
Expand Down Expand Up @@ -98,7 +101,7 @@ public ProviderApi provider(String doc, Object fields, Location location) throws
} else {
// fields is NONE, so there is no field information to add.
}
providerInfoList.add(new ProviderInfo(fakeProvider, doc, providerFieldInfos.build()));
providerInfoList.add(forProviderInfo(fakeProvider, doc, providerFieldInfos.build()));
return fakeProvider;
}

Expand All @@ -107,6 +110,12 @@ public ProviderFieldInfo asProviderFieldInfo(String name, String docString) {
return ProviderFieldInfo.newBuilder().setName(name).setDocString(docString).build();
}

/** Constructor for ProviderInfoWrapper. */
public ProviderInfoWrapper forProviderInfo(
BaseFunction identifier, String docString, Collection<ProviderFieldInfo> fieldInfos) {
return new ProviderInfoWrapper(identifier, docString, fieldInfos);
}

@Override
public BaseFunction rule(
BaseFunction implementation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.skydoc.rendering;

import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.UserDefinedFunctionInfo;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.AttributeType;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.FunctionParamInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.UserDefinedFunctionInfo;
import java.util.List;
Expand Down Expand Up @@ -51,9 +52,10 @@ public String ruleSummary(String ruleName, RuleInfo ruleInfo) {
*/
@SuppressWarnings("unused") // Used by markdown template.
public String providerSummary(String providerName, ProviderInfo providerInfo) {
List<String> fieldNames = providerInfo.getFields().stream()
.map(field -> field.getName())
.collect(Collectors.toList());
List<String> fieldNames =
providerInfo.getFieldInfosList().stream()
.map(field -> field.getName())
.collect(Collectors.toList());
return summary(providerName, fieldNames);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,38 +16,37 @@

import com.google.devtools.build.lib.syntax.BaseFunction;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderFieldInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import java.util.Collection;

/**
* Stores information about a starlark provider definition.
* Stores information about a starlark provider definition, comprised of BaseFunction identifier and
* a {@link ProviderInfo} proto.
*
* For example, in <pre>FooInfo = provider(doc = 'My provider', fields = {'bar' : 'a bar'})</pre>,
* this contains all information about the definition of FooInfo for purposes of generating its
* documentation.
* <p>For example, in
*
* <pre>FooInfo = provider(doc = 'My provider', fields = {'bar' : 'a bar'})</pre>
*
* , this contains all information about the definition of FooInfo for purposes of generating its
* documentation, as well as a unique BaseFunction identifier.
*/
public class ProviderInfo {
public class ProviderInfoWrapper {

private final BaseFunction identifier;
private final String docString;
private final Collection<ProviderFieldInfo> fieldInfos;
private final ProviderInfo providerInfo;

public ProviderInfo(BaseFunction identifier,
String docString,
Collection<ProviderFieldInfo> fieldInfos) {
public ProviderInfoWrapper(
BaseFunction identifier, String docString, Collection<ProviderFieldInfo> fieldInfos) {
this.identifier = identifier;
this.docString = docString;
this.fieldInfos = fieldInfos;
this.providerInfo =
ProviderInfo.newBuilder().setDocString(docString).addAllFieldInfos(fieldInfos).build();
}

public BaseFunction getIdentifier() {
return identifier;
}

public String getDocString() {
return docString;
}

public Collection<ProviderFieldInfo> getFields() {
return fieldInfos;
public ProviderInfo getProviderInfo() {
return providerInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ message ProviderFieldInfo {
// The description of the provider.
string doc_string = 2;
}

// Representation of a Starlark provider definition.
message ProviderInfo {
// The description of the provider.
string doc_string = 1;

// The fields of the provider.
repeated ProviderFieldInfo field_infos = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ${util.providerSummary($providerName, $providerInfo)}

${providerInfo.docString}

#if (!$providerInfo.fields.isEmpty())
#if (!$providerInfo.fieldInfosList.isEmpty())
#[[###]]# Fields

<table class="params-table">
Expand All @@ -17,7 +17,7 @@ ${providerInfo.docString}
<col class="col-description" />
</colgroup>
<tbody>
#foreach ($field in $providerInfo.fields)
#foreach ($field in $providerInfo.fieldInfosList)
<tr id="${providerName}-${field.name}">
<td><code>${field.name}</code></td>
<td>
Expand Down

0 comments on commit 25d2748

Please sign in to comment.