This repository was archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Escape Java keywords #40
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e9f1240
change book name
andreamlin 512cad3
fixed?
andreamlin 0279205
make symboltable a set
andreamlin 9b9509b
flake8 formatting
andreamlin 5d77512
use protoc 3.6.1
andreamlin faf7ec1
sudo?
andreamlin f0f8ece
avoid deprecated warning on yaml loader
andreamlin 45cc3ce
print out error
andreamlin 5e72054
ok actually print out error
andreamlin 91e83e5
remove sudo
andreamlin 5613d69
hhhh
andreamlin 9e37a78
revert somethings
andreamlin 2ec65a2
cleanup a little
andreamlin 2c9116a
put changes back in
andreamlin 76e3856
cleanup
andreamlin 272c154
made baseline as expected
andreamlin 013496d
use set literal
andreamlin 1e8edb8
using underscores instead of numbers
andreamlin 1f95c14
made baseline what is expected
andreamlin 682856f
formatting
andreamlin 84bd6d8
Merge branch 'master' into escape
andreamlin e902c2c
leave fieldmap as is
andreamlin a0dc7b7
make parameter_name_in_map more explicit
andreamlin d4564c2
formatting
andreamlin 10fba4c
update pyyaml and protobuf
andreamlin 2cbfbe4
use load() again
andreamlin 4aa12c7
fix symbol_table and add a test
andreamlin e593ac5
formatting
andreamlin 774bb92
revert the pyyaml stuff
andreamlin 00dfd0e
revert pyyaml change
andreamlin 8f238b5
removed extraneous line
andreamlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a test for this class.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"} | ||
This file contains hidden or 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 hidden or 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 hidden or 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,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_" |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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}"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this file, all forms of |
||
|
|
||
| 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() { | ||
|
|
@@ -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(); | ||
| } | ||
|
|
@@ -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) { | ||
|
|
@@ -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(); | ||
| } | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -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() { | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -182,7 +182,7 @@ public class ShelfBookName extends BookName { | |
| h *= 1000003; | ||
| h ^= shelfId.hashCode(); | ||
| h *= 1000003; | ||
| h ^= bookId.hashCode(); | ||
| h ^= return_.hashCode(); | ||
| return h; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 meancasing_utils.lower_underscore_to_lower_camel(lit), but sincelowerhas changed, we have to make a new variable that refers back to the old meaning.