Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Add TOC to Ruby version module #1388

Merged
merged 3 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.api.codegen.GeneratorVersionProvider;
import com.google.api.codegen.InterfaceView;
import com.google.api.codegen.TargetLanguage;
import com.google.api.codegen.config.GapicInterfaceConfig;
import com.google.api.codegen.config.GapicProductConfig;
import com.google.api.codegen.config.PackageMetadataConfig;
import com.google.api.codegen.config.ProductServiceConfig;
Expand All @@ -42,19 +43,23 @@
import com.google.api.codegen.viewmodel.PathTemplateGetterFunctionView;
import com.google.api.codegen.viewmodel.ViewModel;
import com.google.api.codegen.viewmodel.metadata.SimpleModuleView;
import com.google.api.codegen.viewmodel.metadata.TocContentView;
import com.google.api.codegen.viewmodel.metadata.TocModuleView;
import com.google.api.codegen.viewmodel.metadata.VersionIndexModuleView;
import com.google.api.codegen.viewmodel.metadata.VersionIndexRequireView;
import com.google.api.codegen.viewmodel.metadata.VersionIndexView;
import com.google.api.tools.framework.model.Interface;
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.Model;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import java.util.List;

/** The ModelToViewTransformer to transform a Model into the standard GAPIC surface in Ruby. */
public class RubyGapicSurfaceTransformer implements ModelToViewTransformer {
private static final String VERSION_INDEX_TEMPLATE_FILE = "ruby/version_index.snip";
private static final String XAPI_TEMPLATE_FILENAME = "ruby/main.snip";
private static final int VERSION_MODULE_RINDEX = 1;
private static final int SERVICE_MODULE_RINDEX = 2;

private final GapicCodePathMapper pathMapper;
Expand Down Expand Up @@ -172,37 +177,52 @@ private List<ApiMethodView> generateApiMethods(GapicInterfaceContext context) {

private ViewModel generateVersionIndexView(Model model, GapicProductConfig productConfig) {
SurfaceNamer namer = new RubySurfaceNamer(productConfig.getPackageName());

ImmutableList.Builder<VersionIndexModuleView> moduleViews = ImmutableList.builder();
RubyPackageMetadataTransformer metadataTransformer =
new RubyPackageMetadataTransformer(packageConfig);
RubyPackageMetadataNamer packageNamer =
new RubyPackageMetadataNamer(productConfig.getPackageName());
List<String> apiModules = namer.getApiModules();
int moduleCount = apiModules.size();
String version = apiModules.get(moduleCount - VERSION_MODULE_RINDEX).toLowerCase();

ImmutableList.Builder<VersionIndexRequireView> requireViews = ImmutableList.builder();
ImmutableList.Builder<TocContentView> tocContents = ImmutableList.builder();
Iterable<Interface> interfaces = new InterfaceView().getElementIterable(model);
for (Interface apiInterface : interfaces) {
GapicInterfaceConfig interfaceConfig = productConfig.getInterfaceConfig(apiInterface);
requireViews.add(
VersionIndexRequireView.newBuilder()
.clientName(namer.getFullyQualifiedApiWrapperClassName(interfaceConfig))
.fileName(namer.getServiceFileName(interfaceConfig))
.build());
tocContents.add(
metadataTransformer.generateTocContent(
model, packageNamer, version, namer.getApiWrapperClassName(interfaceConfig)));
}
tocContents.add(
metadataTransformer.generateDataTypeTocContent(
Joiner.on("::").join(apiModules), packageNamer, version));

ImmutableList.Builder<VersionIndexModuleView> moduleViews = ImmutableList.builder();
for (int i = 0; i < moduleCount; ++i) {
if (i == moduleCount - SERVICE_MODULE_RINDEX) {
RubyPackageMetadataTransformer metadataTransformer =
new RubyPackageMetadataTransformer(packageConfig);
if (i == moduleCount - VERSION_MODULE_RINDEX) {
moduleViews.add(
TocModuleView.newBuilder()
.moduleName(apiModules.get(i))
.fullName(model.getServiceConfig().getTitle())
.contents(tocContents.build())
.build());
} else if (i == moduleCount - SERVICE_MODULE_RINDEX) {
moduleViews.add(
metadataTransformer
.generateReadmeMetadataView(
model,
productConfig,
new RubyPackageMetadataNamer(productConfig.getPackageName()))
.generateReadmeMetadataView(model, productConfig, packageNamer)
.moduleName(apiModules.get(i))
.build());
} else {
moduleViews.add(SimpleModuleView.newBuilder().moduleName(apiModules.get(i)).build());
}
}

ImmutableList.Builder<VersionIndexRequireView> requireViews = ImmutableList.builder();
Iterable<Interface> interfaces = new InterfaceView().getElementIterable(model);
for (Interface apiInterface : interfaces) {
requireViews.add(
VersionIndexRequireView.newBuilder()
.clientName(namer.getNotImplementedString("VersionIndexRequireView.clientName"))
.fileName(namer.getServiceFileName(productConfig.getInterfaceConfig(apiInterface)))
.build());
}

return VersionIndexView.newBuilder()
.apiVersion(namer.getApiWrapperModuleVersion())
// The following assumes that all generated interfaces are generated to the same output path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.api.codegen.viewmodel.OptionalArrayMethodView;
import com.google.api.codegen.viewmodel.ViewModel;
import com.google.api.codegen.viewmodel.metadata.ReadmeMetadataView;
import com.google.api.codegen.viewmodel.metadata.TocContentView;
import com.google.api.tools.framework.model.Interface;
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.Model;
Expand Down Expand Up @@ -120,6 +121,33 @@ public ReadmeMetadataView.Builder generateReadmeMetadataView(
.exampleMethods(generateExampleMethods(model, productConfig));
}

public TocContentView generateTocContent(
Model model, RubyPackageMetadataNamer namer, String version, String clientName) {
return generateTocContent(
model.getServiceConfig().getDocumentation().getSummary(), namer, version, clientName);
}

public TocContentView generateDataTypeTocContent(
String apiModule, RubyPackageMetadataNamer namer, String version) {
return generateTocContent("Data types for " + apiModule, namer, version, "Data Types");
}

private TocContentView generateTocContent(
String description, RubyPackageMetadataNamer namer, String version, String clientName) {
return TocContentView.newBuilder()
.name(clientName)
.description(description)
.link(
GITHUB_DOC_HOST
+ String.format(
LIB_DOC_PATH, namer.getMetadataIdentifier(), packageConfig.protoPath())
+ '/'
+ version
+ '/'
+ clientName.replace(" ", "").toLowerCase())
.build();
}

private ViewModel generateGemspecView(Model model, RubyPackageMetadataNamer namer) {
return metadataTransformer
.generateMetadataView(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright 2017 Google Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.api.codegen.viewmodel.metadata;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class TocContentView {
public abstract String name();

public abstract String description();

public abstract String link();

public static Builder newBuilder() {
return new AutoValue_TocContentView.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder name(String val);

public abstract Builder description(String val);

public abstract Builder link(String val);

public abstract TocContentView build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright 2017 Google Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.api.codegen.viewmodel.metadata;

import com.google.auto.value.AutoValue;
import java.util.List;

@AutoValue
public abstract class TocModuleView implements VersionIndexModuleView {
public abstract String moduleName();

public abstract String fullName();

public abstract List<TocContentView> contents();

public static Builder newBuilder() {
return new AutoValue_TocModuleView.Builder();
}

public String type() {
return TocModuleView.class.getSimpleName();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder moduleName(String val);

public abstract Builder fullName(String val);

public abstract Builder contents(List<TocContentView> val);

public abstract TocModuleView build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@switch module.type
@case "ReadmeMetadataView"
{@readmeModule(module, iterator)}
@case "TocModuleView"
{@tocModule(module, iterator)}
@case "SimpleModuleView"
{@simpleModule(module, iterator)}
@end
Expand All @@ -33,6 +35,23 @@
{@simpleModule(module, iterator)}
@end

@private tocModule(module, iterator)
@##
@# # {@module.fullName} Contents
@#
@# | Class | Description |
@# | ----- | ----------- |
@join content : module.contents
@# | [{@content.name}][] | {@content.description} |
@end
@#
@join content : module.contents
@# [{@content.name}]: {@content.link}
@end
@#
{@simpleModule(module, iterator)}
@end

@private simpleModule(module, iterator)
module {@module.moduleName}
@if iterator.hasNext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
# [Product Documentation]: https://cloud.google.com/library
#
module Library
##
# # Google Example Library API Contents
#
# | Class | Description |
# | ----- | ----------- |
# | [LibraryServiceClient][] | A simple Google Example Library API. |
# | [Data Types][] | Data types for Library::V1 |
#
# [LibraryServiceClient]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/library/latest/google/library/v1/libraryserviceclient
# [Data Types]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/library/latest/google/library/v1/datatypes
#
module V1
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
# [Product Documentation]: https://cloud.google.com/library
#
module Library
##
# # Google Example Library API Contents
#
# | Class | Description |
# | ----- | ----------- |
# | [LibraryServiceClient][] | A simple Google Example Library API. |
# | [Data Types][] | Data types for Library::V1 |
#
# [LibraryServiceClient]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/library/latest/google/library/v1/libraryserviceclient
# [Data Types]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/library/latest/google/library/v1/datatypes
#
module V1
end
end
Expand Down