use of com.google.copybara.doc.DocBase.DocParam in project copybara by google.
the class ModuleLoader method processStarlarkMethod.
private DocFunction processStarlarkMethod(Method method, StarlarkMethod annotation, @Nullable String prefix) {
Type[] genericParameterTypes = method.getGenericParameterTypes();
Param[] starlarkParams = annotation.parameters();
if (genericParameterTypes.length < starlarkParams.length) {
throw new IllegalStateException(String.format("Missing java parameters for: %s\n" + "%s\n" + "%s", method, Arrays.toString(genericParameterTypes), Arrays.toString(starlarkParams)));
}
ImmutableList.Builder<DocParam> params = ImmutableList.builder();
Map<String, DocDefault> docDefaultsMap = stream(method.getAnnotationsByType(DocDefault.class)).collect(Collectors.toMap(DocDefault::field, identity(), (f, v) -> v));
for (int i = 0; i < starlarkParams.length; i++) {
Type parameterType = genericParameterTypes[i];
Param starlarkParam = starlarkParams[i];
// Compute the list of names of allowed types (e.g. string or bool or NoneType).
List<String> allowedTypeNames = new ArrayList<>();
if (starlarkParam.allowedTypes().length > 0) {
for (ParamType param : starlarkParam.allowedTypes()) {
allowedTypeNames.add(skylarkTypeName(param.type()) + (param.generic1() != Object.class ? " of " + skylarkTypeName(param.generic1()) : ""));
}
} else {
// Otherwise use the type of the parameter variable itself.
allowedTypeNames.add(skylarkTypeName(parameterType));
}
DocDefault fieldInfo = docDefaultsMap.get(starlarkParam.name());
if (fieldInfo != null && fieldInfo.allowedTypes().length > 0) {
allowedTypeNames = Arrays.asList(fieldInfo.allowedTypes());
}
params.add(new DocParam(starlarkParam.name(), fieldInfo != null ? fieldInfo.value() : emptyToNull(starlarkParam.defaultValue()), allowedTypeNames, starlarkParam.doc()));
}
String returnType = method.getGenericReturnType().equals(NoneType.class) || method.getGenericReturnType().equals(void.class) ? null : skylarkTypeName(method.getGenericReturnType());
return new DocFunction(prefix != null ? prefix + "." + annotation.name() : annotation.name(), annotation.doc(), returnType, params.build(), generateFlagsInfo(method), stream(method.getAnnotationsByType(Example.class)).map(DocExample::new).collect(toImmutableList()));
}
use of com.google.copybara.doc.DocBase.DocParam in project copybara by google.
the class MarkdownRenderer method module.
private CharSequence module(DocModule module, int level) {
StringBuilder sb = new StringBuilder();
sb.append(title(level, module.name));
sb.append(module.description).append("\n\n");
if (!module.fields.isEmpty()) {
sb.append(title(level + 2, "Fields:"));
sb.append(tableHeader("Name", "Description"));
for (DocField field : module.fields) {
sb.append(tableRow(field.name, String.format("%s<br><p>%s</p>", typeName(field.type), field.description)));
}
sb.append("\n");
}
sb.append(flags(module.flags));
for (DocFunction func : module.functions) {
sb.append("<a id=\"").append(func.name).append("\" aria-hidden=\"true\"></a>");
sb.append(title(level + 1, func.name));
sb.append(func.description);
sb.append("\n\n");
if (func.returnType != null) {
sb.append(typeName(func.returnType)).append(" ");
}
sb.append("`").append(func.name).append("(");
Joiner.on(", ").appendTo(sb, Lists.transform(func.params, p -> p.name + (p.defaultValue == null ? "" : "=" + p.defaultValue)));
sb.append(")`\n\n");
if (!func.params.isEmpty()) {
sb.append(title(level + 2, "Parameters:"));
sb.append(tableHeader("Parameter", "Description"));
for (DocParam param : func.params) {
sb.append(tableRow(param.name, String.format("%s<br><p>%s</p>", param.allowedTypes.stream().map(this::typeName).collect(joining(" or ")), param.description)));
}
sb.append("\n");
}
if (!func.examples.isEmpty()) {
sb.append(title(level + 2, func.examples.size() == 1 ? "Example:" : "Examples:"));
for (DocExample example : func.examples) {
sb.append(example(level + 3, example.example));
}
sb.append("\n");
}
sb.append(flags(func.flags));
}
return sb;
}
Aggregations