-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ggj][codegen] fix!: wrap protobuf location and process comments (#336)
* feat: add protobuf comment parser util * fix: add basic proto build rules * feat: add header comments to ServiceClient * fix: build protoc at test time * fix!: wrap protobuf location and process comments * fix: merge
- Loading branch information
Showing
3 changed files
with
107 additions
and
43 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/com/google/api/generator/gapic/model/SourceCodeInfoLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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.common.escape.Escaper; | ||
import com.google.common.escape.Escapers; | ||
import com.google.protobuf.DescriptorProtos.SourceCodeInfo.Location; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* A light wrapper around SourceCodeInfo.Location to provide cleaner protobuf comments. Please see | ||
* additional documentation on descriptor.proto. | ||
*/ | ||
public class SourceCodeInfoLocation { | ||
// Not a singleton because of nested-class instantiation mechanics. | ||
private final NewlineEscaper ESCAPER = new NewlineEscaper(); | ||
|
||
@Nonnull private final Location location; | ||
|
||
private SourceCodeInfoLocation(Location location) { | ||
this.location = location; | ||
} | ||
|
||
public static SourceCodeInfoLocation create(@Nonnull Location location) { | ||
return new SourceCodeInfoLocation(location); | ||
} | ||
|
||
public String getLeadingComments() { | ||
return processProtobufComment(location.getLeadingComments()); | ||
} | ||
|
||
public String getTrailingComments() { | ||
return processProtobufComment(location.getTrailingComments()); | ||
} | ||
|
||
public String getLeadingDetachedComments(int index) { | ||
return processProtobufComment(location.getLeadingDetachedComments(index)); | ||
} | ||
|
||
private String processProtobufComment(String s) { | ||
return ESCAPER.escape(s).trim(); | ||
} | ||
|
||
private class NewlineEscaper extends Escaper { | ||
private final Escaper charEscaper = Escapers.builder().addEscape('\n', "").build(); | ||
|
||
@Override | ||
public String escape(String sourceString) { | ||
return charEscaper.escape(sourceString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters