Skip to content

Commit

Permalink
Use Protobuf definition for determining JSON field names (#218)
Browse files Browse the repository at this point in the history
This was previously relying on an auto-generated mapping layer, but since
the upstream patch was merged into protobuf 3.17.0 we can rely on the
upstream support instead.

See protocolbuffers/protobuf#8356
  • Loading branch information
lfittl committed Jul 4, 2021
1 parent d41ce43 commit 06dab39
Show file tree
Hide file tree
Showing 7 changed files with 1,393 additions and 2,810 deletions.
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

0 comments on commit 06dab39

Please sign in to comment.