Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e9f1240
change book name
andreamlin Mar 14, 2019
512cad3
fixed?
andreamlin Mar 15, 2019
0279205
make symboltable a set
andreamlin Mar 15, 2019
9b9509b
flake8 formatting
andreamlin Mar 15, 2019
5d77512
use protoc 3.6.1
andreamlin Mar 15, 2019
faf7ec1
sudo?
andreamlin Mar 15, 2019
f0f8ece
avoid deprecated warning on yaml loader
andreamlin Mar 15, 2019
45cc3ce
print out error
andreamlin Mar 15, 2019
5e72054
ok actually print out error
andreamlin Mar 15, 2019
91e83e5
remove sudo
andreamlin Mar 15, 2019
5613d69
hhhh
andreamlin Mar 15, 2019
9e37a78
revert somethings
andreamlin Mar 15, 2019
2ec65a2
cleanup a little
andreamlin Mar 15, 2019
2c9116a
put changes back in
andreamlin Mar 15, 2019
76e3856
cleanup
andreamlin Mar 15, 2019
272c154
made baseline as expected
andreamlin Mar 15, 2019
013496d
use set literal
andreamlin Mar 15, 2019
1e8edb8
using underscores instead of numbers
andreamlin Mar 15, 2019
1f95c14
made baseline what is expected
andreamlin Mar 15, 2019
682856f
formatting
andreamlin Mar 15, 2019
84bd6d8
Merge branch 'master' into escape
andreamlin Mar 15, 2019
e902c2c
leave fieldmap as is
andreamlin Mar 15, 2019
a0dc7b7
make parameter_name_in_map more explicit
andreamlin Mar 15, 2019
d4564c2
formatting
andreamlin Mar 15, 2019
10fba4c
update pyyaml and protobuf
andreamlin Mar 15, 2019
2cbfbe4
use load() again
andreamlin Mar 15, 2019
4aa12c7
fix symbol_table and add a test
andreamlin Mar 15, 2019
e593ac5
formatting
andreamlin Mar 15, 2019
774bb92
revert the pyyaml stuff
andreamlin Mar 15, 2019
00dfd0e
revert pyyaml change
andreamlin Mar 15, 2019
8f238b5
removed extraneous line
andreamlin Mar 18, 2019
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
2 changes: 1 addition & 1 deletion plugin/templates/resource_name.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class {{className}} {{extensionKeyword}} {{parentInterface}} {
if (fieldValuesMap == null) {
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
{{#formatFields}}
fieldMapBuilder.put("{{lower}}", {{lower}});
fieldMapBuilder.put("{{parameter_name_in_map}}", {{lower}});
{{/formatFields}}
fieldValuesMap = fieldMapBuilder.build();
}
Expand Down
11 changes: 9 additions & 2 deletions plugin/templates/resource_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import os
from plugin.utils import path_template
from plugin.utils import casing_utils
from plugin.utils.symbol_table import SymbolTable

RESOURCE_NAMES_GLOBAL_PACKAGE_JAVA = 'com.google.api.resourcenames'

Expand Down Expand Up @@ -59,6 +60,7 @@ def package(self):
class ResourceName(ResourceNameBase):

def __init__(self, collection_config, java_package, oneof):
symbol_table = SymbolTable()

entity_name = collection_config.java_entity_name
name_template = path_template.PathTemplate(
Expand All @@ -83,16 +85,21 @@ def __init__(self, collection_config, java_package, oneof):
self.parent_interface = 'ResourceName'
self.extension_keyword = 'implements'
self.parameter_list = [{
'parameter': casing_utils.lower_underscore_to_lower_camel(lit),
'parameter': symbol_table.getNewSymbol(
casing_utils.lower_underscore_to_lower_camel(lit)),
'parameter_name': lit,
'not_first': True,
'not_last': True,
} for lit in id_segments]
self.parameter_list[0]['not_first'] = False
self.parameter_list[-1]['not_last'] = False
self.format_fields = [{
'upper': casing_utils.lower_camel_to_upper_camel(f['parameter']),
'upper': casing_utils.lower_underscore_to_upper_camel(
f['parameter_name']),
'lower': f['parameter'],
'parameter_name_in_map':

@andreamlin andreamlin Mar 15, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed in the fieldMap, and the value used in the fieldMap used to just be lower, which used to mean casing_utils.lower_underscore_to_lower_camel(lit), but since lower has changed, we have to make a new variable that refers back to the old meaning.

casing_utils.lower_underscore_to_lower_camel(
f['parameter_name']),
} for f in self.parameter_list]
self.format_string = collection_config.name_pattern
self.package_name = java_package
Expand Down
110 changes: 110 additions & 0 deletions plugin/utils/symbol_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2019 Google LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
A utility class used to get and store unique symbols.
"""

import copy


class SymbolTable(object):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for this class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure


"""
Initialize a case-sensitive SymbolTable with a set of symbols.
"""
def __init__(self):
self.symbol_table = copy.deepcopy(java_reserved_symbols)

def getNewSymbol(self, desired_name):

if not (desired_name in self.symbol_table):
self.symbol_table.add(desired_name)
return desired_name

# Resolve collisions with one or more underscores.
while (desired_name + "_") in self.symbol_table:
desired_name = desired_name + "_"
self.symbol_table.add(desired_name + "_")
return desired_name + "_"


java_reserved_symbols = {
"abstract",

@andreamlin andreamlin Mar 15, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This set is ripped from gapic-generator's JavaNameFormatter

"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"false",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"void",
"volatile",
"while"}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

install_requires = [
'pystache >= 0.5.4',
'protobuf >= 3.3',
'protobuf >= 3.6',
'google-gax >= 0.12.3',
'pyyaml >= 3.12',
]
Expand Down
6 changes: 5 additions & 1 deletion test/test_plugin_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import subprocess
import shutil
import difflib
from sys import stderr

from plugin.utils import casing_utils

Expand Down Expand Up @@ -71,7 +72,10 @@ def format_output_arg(name, output_dir, extra_arg=None):
'--plugin=protoc-gen-gapic=gapic_plugin.py']
args += ['-I' + path for path in include_dirs]
args += proto_files
subprocess.check_call(args)
try:
subprocess.check_call(args)
except subprocess.CalledProcessError as exc:
stderr.write("Status : FAIL, message %s" % exc.output)


def clean_test_output():
Expand Down
42 changes: 42 additions & 0 deletions test/test_symbol_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2019 Google Inc. 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.
# 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.

from plugin.utils.symbol_table import SymbolTable


def test_symbol_table():
table = SymbolTable()

# Escape a key word once
keyword = "interface"
escaped_keyword = table.getNewSymbol(keyword)
assert escaped_keyword == "interface_"
assert keyword == "interface"

# Escape a key word twice
assert table.getNewSymbol("interface") == "interface__"

# Add a non-keyword
assert table.getNewSymbol("bridge") == "bridge"
# Escape the same non-keyword
assert table.getNewSymbol("bridge") == "bridge_"

# Escape a keyword that already has an underscore
assert table.getNewSymbol("class_") == "class_"
assert table.getNewSymbol("class") == "class__"
assert table.getNewSymbol("class_") == "class___"

# Make sure that a new instance of SymbolTable uses a different base set
new_table = SymbolTable()
assert new_table.getNewSymbol("interface") == "interface_"
40 changes: 20 additions & 20 deletions test/testdata/java_shelf_book_name.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import java.util.List;
public class ShelfBookName extends BookName {

private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}/books/{book_id}");
PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}/books/{return}");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, all forms of book_id are replaced with the corresponding form for return.


private volatile Map<String, String> fieldValuesMap;

private final String shelfId;
private final String bookId;
private final String return_;

public String getShelfId() {
return shelfId;
}

public String getBookId() {
return bookId;
public String getReturn() {
return return_;
}

public static Builder newBuilder() {
Expand All @@ -52,20 +52,20 @@ public class ShelfBookName extends BookName {

private ShelfBookName(Builder builder) {
shelfId = Preconditions.checkNotNull(builder.getShelfId());
bookId = Preconditions.checkNotNull(builder.getBookId());
return_ = Preconditions.checkNotNull(builder.getReturn());
}

public static ShelfBookName of(String shelfId, String bookId) {
public static ShelfBookName of(String shelfId, String return_) {
return newBuilder()
.setShelfId(shelfId)
.setBookId(bookId)
.setReturn(return_)
.build();
}

public static String format(String shelfId, String bookId) {
public static String format(String shelfId, String return_) {
return newBuilder()
.setShelfId(shelfId)
.setBookId(bookId)
.setReturn(return_)
.build()
.toString();
}
Expand All @@ -76,7 +76,7 @@ public class ShelfBookName extends BookName {
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ShelfBookName.parse: formattedString not in valid format");
return of(matchMap.get("shelf_id"), matchMap.get("book_id"));
return of(matchMap.get("shelf_id"), matchMap.get("return"));
}

public static List<ShelfBookName> parseList(List<String> formattedStrings) {
Expand Down Expand Up @@ -109,7 +109,7 @@ public class ShelfBookName extends BookName {
if (fieldValuesMap == null) {
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
fieldMapBuilder.put("shelfId", shelfId);
fieldMapBuilder.put("bookId", bookId);
fieldMapBuilder.put("return", return_);
fieldValuesMap = fieldMapBuilder.build();
}
}
Expand All @@ -123,30 +123,30 @@ public class ShelfBookName extends BookName {

@Override
public String toString() {
return PATH_TEMPLATE.instantiate("shelf_id", shelfId, "book_id", bookId);
return PATH_TEMPLATE.instantiate("shelf_id", shelfId, "return", return_);
}

/** Builder for ShelfBookName. */
public static class Builder {

private String shelfId;
private String bookId;
private String return_;

public String getShelfId() {
return shelfId;
}

public String getBookId() {
return bookId;
public String getReturn() {
return return_;
}

public Builder setShelfId(String shelfId) {
this.shelfId = shelfId;
return this;
}

public Builder setBookId(String bookId) {
this.bookId = bookId;
public Builder setReturn(String return_) {
this.return_ = return_;
return this;
}

Expand All @@ -155,7 +155,7 @@ public class ShelfBookName extends BookName {

private Builder(ShelfBookName shelfBookName) {
shelfId = shelfBookName.shelfId;
bookId = shelfBookName.bookId;
return_ = shelfBookName.return_;
}

public ShelfBookName build() {
Expand All @@ -171,7 +171,7 @@ public class ShelfBookName extends BookName {
if (o instanceof ShelfBookName) {
ShelfBookName that = (ShelfBookName) o;
return (this.shelfId.equals(that.shelfId))
&& (this.bookId.equals(that.bookId));
&& (this.return_.equals(that.return_));
}
return false;
}
Expand All @@ -182,7 +182,7 @@ public class ShelfBookName extends BookName {
h *= 1000003;
h ^= shelfId.hashCode();
h *= 1000003;
h ^= bookId.hashCode();
h ^= return_.hashCode();
return h;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/library_gapic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ collections:
common_resource_name: com.google.cloud.ProjectName
- name_pattern: shelves/{shelf_id}
entity_name: shelf
- name_pattern: shelves/{shelf_id}/books/{book_id}
- name_pattern: shelves/{shelf_id}/books/{return} # Test using a Java keyword ("return").
entity_name: book
language_overrides:
- language: java
Expand Down