Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Protobuf definition for determining JSON field names #218

Merged
merged 1 commit into from Jul 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,10 @@
- google-protobuf 3.15.x has a bug that causes a seg fault in Ruby under
certain conditions (https://github.com/protocolbuffers/protobuf/pull/8639). Use
google-protobuf 3.17.1 instead.
* Use Protobuf definition for determining JSON field names
- Note you may see a breaking change if you were using `PgQuery::ParseResult.encode_json`
to map the protobuf result to JSON, since this now respects the intended JSON names
from the Proto3 definition (instead of the differently formatted Protobuf field names)


## 2.0.3 2021-04-05
Expand Down
14 changes: 0 additions & 14 deletions Rakefile
Expand Up @@ -86,18 +86,4 @@ task :update_source do
system("cp -a #{libdir}/testdata/* #{testfilesdir}")
# Copy back the custom ext files
system("cp -a #{extbakdir}/pg_query_ruby.{c,sym} #{extbakdir}/extconf.rb #{extdir}")

# Generate JSON field name helper (workaround until https://github.com/protocolbuffers/protobuf/pull/8356 is merged)
str = "module PgQuery\n INTERNAL_PROTO_FIELD_NAME_TO_JSON_NAME = {\n"
cur_type = nil
File.read(File.join(libdir, 'protobuf/pg_query.proto')).each_line do |line|
if line[/^message (\w+)/]
cur_type = $1
next
end
next unless line[/(repeated )?\w+ (\w+) = \d+( \[json_name="(\w+)"\])?;/]
str += format(" [%s, :%s] => '%s',\n", cur_type, $2, $4 || $2)
end
str += " }\nend\n"
File.write(File.join(__dir__, 'lib/pg_query/json_field_names.rb'), str)
end
1 change: 0 additions & 1 deletion lib/pg_query.rb
Expand Up @@ -10,7 +10,6 @@
require 'pg_query/treewalker'

require 'pg_query/filter_columns'
require 'pg_query/json_field_names'
require 'pg_query/fingerprint'
require 'pg_query/param_refs'
require 'pg_query/deparse'
Expand Down
4 changes: 1 addition & 3 deletions lib/pg_query/fingerprint.rb
Expand Up @@ -60,9 +60,7 @@ def ignored_node_type?(node)
end

def node_protobuf_field_name_to_json_name(node_class, field)
# Use protobuf descriptor once json_name support is fixed: https://github.com/protocolbuffers/protobuf/pull/8356
# node_class.descriptor.find { |d| d.name == field.to_s }.json_name
INTERNAL_PROTO_FIELD_NAME_TO_JSON_NAME.fetch([node_class, field])
node_class.descriptor.find { |d| d.name == field.to_s }.json_name
end

def fingerprint_node(node, hash, parent_node_name = nil, parent_field_name = nil) # rubocop:disable Metrics/CyclomaticComplexity
Expand Down