pb_json_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ed2211bc248280019756fcfd8f74c8f5abe7d472ef5eaa5e38b72c35e96a633
4
+ data.tar.gz: 3609f021a03be07a3bac89c0ca1a1ddd77c1aa4748ff8ef81a605fbe2983afe7
5
+ SHA512:
6
+ metadata.gz: 8ade920f219c29475036b0f60bc88cac02da438e9ddbd5ca95d8d4a14c830bb50ed9b9789365be5a692804274a967279e11d71430ecda5c0607a9e283fd7df77
7
+ data.tar.gz: 8a7b8d36cdf82c1fa895e3f44deba5d693f20ec07921a7144bb19aefe508c1a90ee9f85f50fa7373c299313f54a2cfe7616e26cc586e37848318192a7dec2a37
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in pb_json_parser.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pb_json_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.8.0)
17
+ rspec-core (~> 3.8.0)
18
+ rspec-expectations (~> 3.8.0)
19
+ rspec-mocks (~> 3.8.0)
20
+ rspec-core (3.8.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-expectations (3.8.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-mocks (3.8.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-support (3.8.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.16)
35
+ pb_json_parser!
36
+ pry (~> 0.11)
37
+ rake (~> 10.0)
38
+ rspec (~> 3.8)
39
+
40
+ BUNDLED WITH
41
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nao Minami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # PbJsonParser
2
+
3
+ PbJsonParser parses JSON dumps generated by [protoc-gen-json](https://github.com/sourcegraph/prototools/blob/master/README.json.md) and returns the information of fields and associations of each message types.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pb_json_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pb_json_parser
20
+
21
+ ## Usage
22
+ First, prepare .proto file.
23
+
24
+ ```protobuf
25
+ // resources.proto
26
+ syntax = "proto3";
27
+ package south37.users_prototype;
28
+
29
+ import "google/protobuf/timestamp.proto";
30
+
31
+ message User {
32
+ int32 id = 1;
33
+ Profile profile = 2;
34
+ }
35
+
36
+ message Profile {
37
+ int32 id = 1;
38
+ google.protobuf.Timestamp birthday = 2;
39
+ }
40
+ ```
41
+
42
+ Then, generate `.proto.json` by protoc with [protoc-gen-json](https://github.com/sourcegraph/prototools/blob/master/README.json.md) plugin.
43
+
44
+ ```bash
45
+ $ REPOLIST=( \
46
+ > sourcegraph.com/sourcegraph/prototools/cmd/protoc-gen-json \
47
+ > )
48
+ > for REPO in ${REPOLIST[@]}; do
49
+ > if [ ! -d $GOPATH/src/$REPO ]; then
50
+ > go get -u $REPO
51
+ > fi
52
+ > done
53
+
54
+ $ protoc \
55
+ > -I ./protos \
56
+ > --json_out=out=resources.proto.json:./protos \
57
+ > ./protos/resources.proto
58
+ ```
59
+
60
+ Finally you can parse the json by `PbJsonParser`.
61
+
62
+ ```ruby
63
+ [6] pry(main)> PbJsonParser.parse(json: File.read('./protos/resources.proto.json'), filename: 'resources.proto').map(&:to_h)
64
+ => [
65
+ {:name=>"User", :fields=>["id"], :assocs=>[{:name=>"profile", :type=>"has_one", :class_name=>"Profile"}]},
66
+ {:name=>"Profile", :fields=>["id", "birthday"], :assocs=>[]}
67
+ ]
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake true` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/south37/pb_json_parser.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pb_json_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ module PbJsonParser
2
+ module AST
3
+ class Association
4
+ Type = {
5
+ has_one: "has_one".freeze,
6
+ has_many: "has_many".freeze,
7
+ }.freeze
8
+
9
+ attr_reader :name, :type, :class_name
10
+
11
+ # @param [String] name
12
+ # @param [String] type
13
+ # @param [String] class_name
14
+ def initialize(name:, type:, class_name:)
15
+ @name = name
16
+ @type = type
17
+ @class_name = class_name
18
+ validate!
19
+ end
20
+
21
+ def to_h
22
+ {
23
+ name: @name,
24
+ type: @type,
25
+ class_name: @class_name,
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ def validate!
32
+ if !Type.values.include?(@type)
33
+ raise "Invalid type: #{@type}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ module PbJsonParser
2
+ module AST
3
+ class Field
4
+ attr_reader :name
5
+
6
+ # @param [String] name
7
+ def initialize(name:)
8
+ @name = name
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ module PbJsonParser
2
+ module AST
3
+ class Message
4
+ attr_reader :name, :fields, :assocs
5
+
6
+ # @param [String] name
7
+ def initialize(name:)
8
+ @name = name
9
+ @fields = []
10
+ @assocs = []
11
+ end
12
+
13
+ # @param [Field] f
14
+ def push_field(f)
15
+ @fields.push(f)
16
+ end
17
+
18
+ # @param [Association] a
19
+ def push_assoc(a)
20
+ @assocs.push(a)
21
+ end
22
+
23
+ def to_h
24
+ {
25
+ name: @name,
26
+ fields: @fields.map(&:name),
27
+ assocs: @assocs.map(&:to_h),
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,8 @@
1
+ require "pb_json_parser/ast/association"
2
+ require "pb_json_parser/ast/field"
3
+ require "pb_json_parser/ast/message"
4
+
5
+ module PbJsonParser
6
+ module AST
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module PbJsonParser
2
+ class Config
3
+ attr_reader :skip_fields
4
+
5
+ def initialize
6
+ @skip_fields = []
7
+ end
8
+
9
+ # @param [<String>] fields
10
+ def skip_fields=(fields)
11
+ if !fields.is_a?(Array)
12
+ raise "invalid skip_fields: #{fields}"
13
+ end
14
+ @skip_fields = fields
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+
3
+ module PbJsonParser
4
+ class Parser
5
+ # @param [String] json
6
+ # @param [String] filename
7
+ # @param [Config] config
8
+ def initialize(json:, filename:)
9
+ @data = read_data(json, filename)
10
+ @config = Config.new
11
+ @package = @data["package"]
12
+ end
13
+
14
+ # @return [<AST::Message>]
15
+ def parse
16
+ r = []
17
+ @data["message_type"].each do |m_type|
18
+ m = parse_message(m_type)
19
+ r.push m
20
+ end
21
+ r
22
+ end
23
+
24
+ def configure(&block)
25
+ yield @config
26
+ end
27
+
28
+ private
29
+
30
+ # @param [{ String => Any }] message_type
31
+ # @return [<AST::Message>]
32
+ def parse_message(message_type)
33
+ m = AST::Message.new(name: message_type["name"])
34
+
35
+ message_type["field"].each do |f|
36
+ # NOTE: skip field when configured
37
+ next if @config.skip_fields.include?(f["name"])
38
+
39
+ case f["type"]
40
+ when 11 # type: TYPE_MESSAGE
41
+ if is_assoc?(f)
42
+ m.push_assoc(parse_assoc(f))
43
+ else
44
+ m.push_field(AST::Field.new(name: f["name"]))
45
+ end
46
+ else
47
+ m.push_field(AST::Field.new(name: f["name"]))
48
+ end
49
+ end
50
+
51
+ m
52
+ end
53
+
54
+ # @param [{ String => Any }] field
55
+ # @return [AST::Association]
56
+ def parse_assoc(field)
57
+ case field["label"]
58
+ when 3 # label: LABEL_REPEATED
59
+ type = AST::Association::Type[:has_many]
60
+ else
61
+ type = AST::Association::Type[:has_one]
62
+ end
63
+ class_name = field_message_type(field)
64
+
65
+ assoc = AST::Association.new(
66
+ name: field["name"],
67
+ type: type,
68
+ class_name: class_name,
69
+ )
70
+ end
71
+
72
+ # @param [{ string => Any }] field
73
+ # @return [bool]
74
+ def is_assoc?(field)
75
+ field_package(field) == @package
76
+ end
77
+
78
+ # @param [Hash] field
79
+ def field_package(field)
80
+ # NOTE: field["type_name"] is `.<package>.<message_type>`.
81
+ field["type_name"][1..@package.size]
82
+ end
83
+
84
+ # @param [Hash] field
85
+ def field_message_type(field)
86
+ # NOTE: field["type_name"] is `.<package>.<message_type>`.
87
+ field["type_name"][(@package.size + 2)..-1]
88
+ end
89
+
90
+ # @param [String] json
91
+ # @return [Hash]
92
+ def read_data(json, filename)
93
+ h = JSON.parse(json)
94
+ data = h["proto_file"].find { |f| f["name"] == filename }
95
+ if !data
96
+ raise "'#{filename}' does not exist in the json!"
97
+ end
98
+ data
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module PbJsonParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "pb_json_parser/version"
2
+ require "pb_json_parser/ast"
3
+ require "pb_json_parser/config"
4
+ require "pb_json_parser/parser"
5
+
6
+ module PbJsonParser
7
+ class << self
8
+ # @param [String] json
9
+ # @param [String] filename
10
+ # @return [<AST::Message>]
11
+ def parse(json:, filename:, skip_fields: [])
12
+ parser = Parser.new(json: json, filename: filename)
13
+ parser.configure do |c|
14
+ c.skip_fields = skip_fields
15
+ end
16
+ parser.parse
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pb_json_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pb_json_parser"
8
+ spec.version = PbJsonParser::VERSION
9
+ spec.authors = ["Nao Minami"]
10
+ spec.email = ["south37777@gmail.com"]
11
+
12
+ spec.summary = %q{Parser of JSON dumps generated by protoc-gen-json.}
13
+ spec.description = %q{Parser of JSON dumps generated by protoc-gen-json.}
14
+ spec.homepage = "https://github.com/south37/pb_json_parser"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.8"
29
+ spec.add_development_dependency "pry", "~> 0.11"
30
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pb_json_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nao Minami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ description: Parser of JSON dumps generated by protoc-gen-json.
70
+ email:
71
+ - south37777@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/pb_json_parser.rb
87
+ - lib/pb_json_parser/ast.rb
88
+ - lib/pb_json_parser/ast/association.rb
89
+ - lib/pb_json_parser/ast/field.rb
90
+ - lib/pb_json_parser/ast/message.rb
91
+ - lib/pb_json_parser/config.rb
92
+ - lib/pb_json_parser/parser.rb
93
+ - lib/pb_json_parser/version.rb
94
+ - pb_json_parser.gemspec
95
+ homepage: https://github.com/south37/pb_json_parser
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.7.6
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Parser of JSON dumps generated by protoc-gen-json.
119
+ test_files: []