Skip to content

Commit

Permalink
[ggj] fix: refactor parsed data into GapicContext (#154)
Browse files Browse the repository at this point in the history
* feat: add initial resource name def parsing

* feat: add early failure for missing fields

* feat: couple resnames and message parsing

* fix!: refactor method arg parsing away from class creation

* feat: add resource_reference parsing

* fix: dot name parsing
  • Loading branch information
miraleung authored Aug 7, 2020
1 parent ccaf11d commit a8cf057
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
9 changes: 3 additions & 6 deletions src/main/java/com/google/api/generator/gapic/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

import com.google.api.generator.gapic.composer.Composer;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.gapic.protowriter.Writer;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import java.util.List;
import java.util.Map;

public class Generator {
public static CodeGeneratorResponse generateGapic(
CodeGeneratorRequest request, String outputFilePath) {
Map<String, Message> messageTypes = Parser.parseMessages(request);
List<Service> services = Parser.parseServices(request, messageTypes);
List<GapicClass> clazzes = Composer.composeServiceClasses(services, messageTypes);
GapicContext context = Parser.parse(request);
List<GapicClass> clazzes = Composer.composeServiceClasses(context);
CodeGeneratorResponse response = Writer.writeCode(clazzes, outputFilePath);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Service;
import java.util.ArrayList;
Expand All @@ -26,11 +27,10 @@
import javax.annotation.Nonnull;

public class Composer {
public static List<GapicClass> composeServiceClasses(
@Nonnull List<Service> services, @Nonnull Map<String, Message> messageTypes) {
public static List<GapicClass> composeServiceClasses(GapicContext context) {
List<GapicClass> clazzes = new ArrayList<>();
for (Service service : services) {
clazzes.addAll(generateServiceClasses(service, messageTypes));
for (Service service : context.services()) {
clazzes.addAll(generateServiceClasses(service, context.messages()));
}
return clazzes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2020 Google LLC
//
// 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.generator.gapic.model;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;

@AutoValue
public abstract class GapicContext {
// Maps the message name (as it appears in the protobuf) to Messages.
public abstract ImmutableMap<String, Message> messages();

// Maps the resource type string to ResourceNames.
public abstract ImmutableMap<String, ResourceName> resourceNames();

public abstract ImmutableList<Service> services();

public static Builder builder() {
return new AutoValue_GapicContext.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setMessages(Map<String, Message> messages);

public abstract Builder setResourceNames(Map<String, ResourceName> resourceNames);

public abstract Builder setServices(List<Service> services);

public abstract GapicContext build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.gapic.model.Field;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.LongrunningOperation;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Method;
Expand Down Expand Up @@ -57,6 +58,21 @@ public GapicParserException(String errorMessage) {
}
}

public static GapicContext parse(CodeGeneratorRequest request) {
// Keep message and resource name parsing separate for cleaner logic.
// While this takes an extra pass through the protobufs, the extra time is relatively trivial
// and is worth the larger reduced maintenance cost.
Map<String, Message> messages = parseMessages(request);
Map<String, ResourceName> resourceNames = parseResourceNames(request);
messages = updateResourceNamesInMessages(messages, resourceNames.values());
List<Service> services = parseServices(request, messages);
return GapicContext.builder()
.setServices(services)
.setMessages(messages)
.setResourceNames(resourceNames)
.build();
}

public static List<Service> parseServices(
CodeGeneratorRequest request, Map<String, Message> messageTypes) {
Map<String, FileDescriptor> fileDescriptors = getFilesToGenerate(request);
Expand Down

0 comments on commit a8cf057

Please sign in to comment.