fmparser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +121 -0
- data/Rakefile +2 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/fmparser.gemspec +31 -0
- data/lib/fmparser.rb +13 -0
- data/lib/fmparser/error.rb +8 -0
- data/lib/fmparser/node.rb +118 -0
- data/lib/fmparser/parser.rb +79 -0
- data/lib/fmparser/parser/deep_hash_node.rb +34 -0
- data/lib/fmparser/parser/deep_hash_parser.rb +55 -0
- data/lib/fmparser/version.rb +3 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb00fbdf6c704fdc22b51cf0fc27a0463d26f6b3a1a745de77891fe220e313f5
|
4
|
+
data.tar.gz: d760ccf65c0cdbac53ebbc2a2276b108290a16261f23d3a7423a069c4fb55c8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b4b51eaaf3b39b16d1f4bda9e8575173ef8b1745ac059d8fe8f9bd6b347fe712ed63cca75d599534fc535f1a4eeeb9f1316281437add83f9af4f1359a8ce8626
|
7
|
+
data.tar.gz: 02c1285491a06748426929d56f22d1562d59c7aa0aef5ea68883f58b3032e2ae652ccf6c1dc4d5d7c8c928a638695fe5026874791c319b69811dcedfd52dc24d
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fmparser (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
google-protobuf (3.8.0)
|
12
|
+
method_source (0.9.2)
|
13
|
+
pry (0.12.2)
|
14
|
+
coderay (~> 1.1.0)
|
15
|
+
method_source (~> 0.9.0)
|
16
|
+
rake (10.5.0)
|
17
|
+
rspec (3.8.0)
|
18
|
+
rspec-core (~> 3.8.0)
|
19
|
+
rspec-expectations (~> 3.8.0)
|
20
|
+
rspec-mocks (~> 3.8.0)
|
21
|
+
rspec-core (3.8.1)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-expectations (3.8.4)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-mocks (3.8.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-support (3.8.2)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
bundler (~> 1.17)
|
36
|
+
fmparser!
|
37
|
+
google-protobuf (~> 3.8)
|
38
|
+
pry (~> 0.12)
|
39
|
+
rake (~> 10.0)
|
40
|
+
rspec (~> 3.8)
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 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,121 @@
|
|
1
|
+
# FMParser
|
2
|
+
|
3
|
+
**F**ield**M**ask **Parser**. If you want to know more about FieldMask, please see https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask .
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'fmparser'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install fmparser
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`FMParser.parse` parses FieldMask parameter and returns a `FMParser::MessageNode` object.
|
24
|
+
|
25
|
+
For example, suppose you have the protobuf message classes shown below:
|
26
|
+
|
27
|
+
```proto
|
28
|
+
syntax = "proto3";
|
29
|
+
package msg;
|
30
|
+
|
31
|
+
message User {
|
32
|
+
Operation op = 1;
|
33
|
+
int64 id = 2;
|
34
|
+
repeated Name names =3;
|
35
|
+
Job job = 4;
|
36
|
+
}
|
37
|
+
|
38
|
+
message Name {
|
39
|
+
Operation op = 1;
|
40
|
+
int64 id = 2;
|
41
|
+
string first_name = 3;
|
42
|
+
string middle_name = 4;
|
43
|
+
string last_name = 5;
|
44
|
+
string language = 6;
|
45
|
+
}
|
46
|
+
|
47
|
+
message Job {
|
48
|
+
Operation op = 1;
|
49
|
+
int64 id = 2;
|
50
|
+
string company = 3;
|
51
|
+
}
|
52
|
+
|
53
|
+
enum Operation {
|
54
|
+
OPERATION_UNSPECIFIED = 0;
|
55
|
+
CREATE = 1;
|
56
|
+
UPDATE = 2;
|
57
|
+
DELETE = 3;
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
Then, you can parse `paths` array of strings with `root` protobuf message class.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
[1] pry(main)> f = FMParser.parse(paths: ["op", "id", "names.first_name", "names.last_name"], root: Msg::User)
|
65
|
+
=> #<FMParser::MessageNode:0x00007f7fc9848168
|
66
|
+
@enums=[#<FMParser::EnumNode:0x00007f7fc98489b0 @label=:optional, @name=:op, @type=Msg::Operation>],
|
67
|
+
@label=nil,
|
68
|
+
@messages=
|
69
|
+
[#<FMParser::MessageNode:0x00007f7fc9848208
|
70
|
+
@enums=[],
|
71
|
+
@label=:repeated,
|
72
|
+
@messages=[],
|
73
|
+
@name=:names,
|
74
|
+
@scalars=
|
75
|
+
[#<FMParser::ScalarNode:0x00007f7fc98484b0 @label=:optional, @name=:first_name, @type=:string>,
|
76
|
+
#<FMParser::ScalarNode:0x00007f7fc98482a8 @label=:optional, @name=:last_name, @type=:string>],
|
77
|
+
@type=Msg::Name>],
|
78
|
+
@name=nil,
|
79
|
+
@scalars=[#<FMParser::ScalarNode:0x00007f7fc9848820 @label=:optional, @name=:id, @type=:int64>],
|
80
|
+
@type=Msg::User>
|
81
|
+
|
82
|
+
[2] pry(main)> f.field_names
|
83
|
+
=> [:id, :op, :names]
|
84
|
+
|
85
|
+
[3] pry(main)> f.has?(:id)
|
86
|
+
=> true
|
87
|
+
|
88
|
+
[4] pry(main)> f.has?(:names)
|
89
|
+
=> true
|
90
|
+
|
91
|
+
[5] pry(main)> f.has?(:job)
|
92
|
+
=> false
|
93
|
+
|
94
|
+
[6] pry(main)> f.get_child(:names)
|
95
|
+
=> #<FMParser::MessageNode:0x00007f7fc9848208
|
96
|
+
@enums=[],
|
97
|
+
@label=:repeated,
|
98
|
+
@messages=[],
|
99
|
+
@name=:names,
|
100
|
+
@scalars=
|
101
|
+
[#<FMParser::ScalarNode:0x00007f7fc98484b0 @label=:optional, @name=:first_name, @type=:string>,
|
102
|
+
#<FMParser::ScalarNode:0x00007f7fc98482a8 @label=:optional, @name=:last_name, @type=:string>],
|
103
|
+
@type=Msg::Name>
|
104
|
+
|
105
|
+
[7] pry(main)> f.get_child(:names).field_names
|
106
|
+
=> [:first_name, :last_name]
|
107
|
+
```
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
112
|
+
|
113
|
+
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).
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/south37/fmparser.
|
118
|
+
|
119
|
+
## License
|
120
|
+
|
121
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/fmparser.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "fmparser/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fmparser"
|
8
|
+
spec.version = FMParser::VERSION
|
9
|
+
spec.authors = ["Nao Minami"]
|
10
|
+
spec.email = ["south37777@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{FieldMask parameter parser}
|
13
|
+
spec.description = %q{FieldMask parameter parser}
|
14
|
+
spec.homepage = "https://github.com/south37/fmparser"
|
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.17"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "pry", "~> 0.12"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.8"
|
30
|
+
spec.add_development_dependency "google-protobuf", "~> 3.8"
|
31
|
+
end
|
data/lib/fmparser.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "fmparser/parser"
|
2
|
+
require "fmparser/version"
|
3
|
+
|
4
|
+
module FMParser
|
5
|
+
class << self
|
6
|
+
# @param [<String>] paths
|
7
|
+
# @param [Class] root Google::Protobuf message class
|
8
|
+
def parse(paths:, root:)
|
9
|
+
parser = Parser.new
|
10
|
+
parser.parse(paths: paths, root: root)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module FMParser
|
2
|
+
class Node
|
3
|
+
end
|
4
|
+
|
5
|
+
class ScalarNode < Node
|
6
|
+
# @param [String | Symbol] name
|
7
|
+
# @param [String | Symbol] type represents the scalar type of protobuf
|
8
|
+
# @param [String | Symbol] label
|
9
|
+
def initialize(name:, type:, label:)
|
10
|
+
@name = name.to_sym
|
11
|
+
@type = type.to_sym
|
12
|
+
@label = label.to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :name, :type, :label
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
self.class == other.class &&
|
19
|
+
self.name == other.name &&
|
20
|
+
self.type == other.type &&
|
21
|
+
self.label == other.label
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class EnumNode < Node
|
26
|
+
# @param [String | Symbol] name
|
27
|
+
# @param [Module] type represents the enum type of protobuf
|
28
|
+
# @param [String | Symbol] label
|
29
|
+
def initialize(name:, type:, label:)
|
30
|
+
@name = name.to_sym
|
31
|
+
@type = type
|
32
|
+
@label = label.to_sym
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_reader :name, :type, :label
|
36
|
+
|
37
|
+
def ==(other)
|
38
|
+
self.class == other.class &&
|
39
|
+
self.name == other.name &&
|
40
|
+
self.type == other.type &&
|
41
|
+
self.label == other.label
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class MessageNode < Node
|
46
|
+
# @param [String | Symbol | nil] name
|
47
|
+
# @param [Class] type represents the message type of protobuf
|
48
|
+
# @param [String | Symbol | nil] label
|
49
|
+
def initialize(name:, type:, label:, scalars: [], enums: [], messages: [])
|
50
|
+
@name = name ? name.to_sym : name
|
51
|
+
@type = type
|
52
|
+
@label = label ? label.to_sym : label
|
53
|
+
|
54
|
+
@scalars = scalars
|
55
|
+
@enums = enums
|
56
|
+
@messages = messages
|
57
|
+
end
|
58
|
+
|
59
|
+
attr_reader :name, :type, :label, :scalars, :enums, :messages
|
60
|
+
|
61
|
+
# @return [<FMParser::Node>]
|
62
|
+
def fields
|
63
|
+
scalars + enums + messages
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [<Symbol>]
|
67
|
+
def field_names
|
68
|
+
fields.map(&:name)
|
69
|
+
end
|
70
|
+
|
71
|
+
# @param [String | Symbol] field
|
72
|
+
# @return [Boolean]
|
73
|
+
def has?(field)
|
74
|
+
f = field.to_sym
|
75
|
+
validate!(f)
|
76
|
+
field_names.include?(f)
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param [String | Symbol] field
|
80
|
+
# @return [FMParser::MessageNode | nil]
|
81
|
+
def get_child(field)
|
82
|
+
f = field.to_sym
|
83
|
+
validate!(f)
|
84
|
+
children[f]
|
85
|
+
end
|
86
|
+
|
87
|
+
def children
|
88
|
+
@children ||=
|
89
|
+
@messages.map { |m| [m.name, m] }.to_h
|
90
|
+
end
|
91
|
+
|
92
|
+
def ==(other)
|
93
|
+
self.class == other.class &&
|
94
|
+
self.name == other.name &&
|
95
|
+
self.type == other.type &&
|
96
|
+
self.label == other.label &&
|
97
|
+
self.scalars == other.scalars &&
|
98
|
+
self.enums == other.enums &&
|
99
|
+
self.messages == other.messages
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
# @param [Symbol]
|
105
|
+
# @raise [InvalidFieldError]
|
106
|
+
def validate!(field)
|
107
|
+
if !valid_fields.include?(field)
|
108
|
+
raise InvalidParameterError.new("There is no \"#{field}\" field in the #{@type} class")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# @return [<Symbol>]
|
113
|
+
def valid_fields
|
114
|
+
@valid_fields ||=
|
115
|
+
@type.descriptor.entries.map(&:name).map(&:to_sym)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "fmparser/error"
|
2
|
+
require "fmparser/node"
|
3
|
+
require "fmparser/parser/deep_hash_parser"
|
4
|
+
|
5
|
+
module FMParser
|
6
|
+
class Parser
|
7
|
+
# @param [<String>] paths
|
8
|
+
# @param [Class] root Google::Protobuf message class
|
9
|
+
def parse(paths:, root:)
|
10
|
+
deep_hash = DeepHashParser.parse(paths)
|
11
|
+
|
12
|
+
scalars, enums, messages = build_nodes(
|
13
|
+
descriptor: root.descriptor,
|
14
|
+
deep_hash: deep_hash,
|
15
|
+
)
|
16
|
+
|
17
|
+
MessageNode.new(
|
18
|
+
name: nil,
|
19
|
+
type: root,
|
20
|
+
label: nil,
|
21
|
+
scalars: scalars,
|
22
|
+
enums: enums,
|
23
|
+
messages: messages,
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# @param [Google::Protobuf::Descriptor] descriptor
|
30
|
+
# @param [FMParser::Parser::DeepHashNode] deep_hash
|
31
|
+
# @return [<<ScalarNode>, <EnumNode>, <MessageNode>>]
|
32
|
+
def build_nodes(descriptor:, deep_hash:)
|
33
|
+
scalars = []
|
34
|
+
enums = []
|
35
|
+
messages = []
|
36
|
+
|
37
|
+
deep_hash.children.each do |name, dh|
|
38
|
+
entry = descriptor.entries.find { |e| e.name == name }
|
39
|
+
|
40
|
+
if entry.nil?
|
41
|
+
raise InvalidPathError.new("\"#{name}\" does not exist in the fields of #{descriptor.msgclass}!")
|
42
|
+
end
|
43
|
+
|
44
|
+
case entry.type
|
45
|
+
when :message
|
46
|
+
d = entry.subtype # Google::Protobuf::Descriptor
|
47
|
+
s, e, m = build_nodes(
|
48
|
+
descriptor: d,
|
49
|
+
deep_hash: dh,
|
50
|
+
)
|
51
|
+
n = MessageNode.new(
|
52
|
+
name: name,
|
53
|
+
type: d.msgclass,
|
54
|
+
label: entry.label,
|
55
|
+
scalars: s,
|
56
|
+
enums: e,
|
57
|
+
messages: m,
|
58
|
+
)
|
59
|
+
messages << n
|
60
|
+
when :enum
|
61
|
+
# NOTE: If dh.is_leaf is false, it is invalid. But ignore it now.
|
62
|
+
d = entry.subtype # Google::Protobuf::EnumDescriptor
|
63
|
+
n = EnumNode.new(name: name, type: d.enummodule, label: entry.label)
|
64
|
+
enums << n
|
65
|
+
else # We treat this case as scalar
|
66
|
+
# NOTE: If dh.is_leaf is false, it is invalid. But ignore it now.
|
67
|
+
n = ScalarNode.new(name: name, type: entry.type, label: entry.label)
|
68
|
+
scalars << n
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
[
|
73
|
+
scalars,
|
74
|
+
enums,
|
75
|
+
messages,
|
76
|
+
]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module FMParser
|
2
|
+
class Parser
|
3
|
+
class DeepHashNode
|
4
|
+
attr_accessor :is_leaf
|
5
|
+
attr_reader :children
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@is_leaf = false
|
9
|
+
@children = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
# @param [String] field
|
13
|
+
# @param [DeepHashNode] n
|
14
|
+
def push_child(field, n)
|
15
|
+
if !field.is_a?(String)
|
16
|
+
raise "Invalid field type: #{field.class}"
|
17
|
+
end
|
18
|
+
@children[field] = n
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [DeepHashNode] other
|
22
|
+
# @reutrn [bool]
|
23
|
+
def ==(other)
|
24
|
+
is_leaf == other.is_leaf && children == other.children
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_h
|
28
|
+
@children.map do |field, child|
|
29
|
+
[field, child.to_h]
|
30
|
+
end.to_h
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "fmparser/parser/deep_hash_node"
|
2
|
+
|
3
|
+
module FMParser
|
4
|
+
class Parser
|
5
|
+
# DeepHashParser parses paths (string array) to a deep hash.
|
6
|
+
# ex.
|
7
|
+
# param: ["id", "facebook_uid", "detail", "detail.id", "profile.name"]
|
8
|
+
# return: <is_leaf: false, children: {
|
9
|
+
# "id" => <is_leaf: true, children: {}>,
|
10
|
+
# "facebook_uid" => <is_leaf: true, children: {}>,
|
11
|
+
# "detail" => <is_leaf: true, children: {
|
12
|
+
# "id" => <is_leaf: true, children: {}>
|
13
|
+
# }>,
|
14
|
+
# "profile" => <is_leaf: false, children: {
|
15
|
+
# "name" => <is_leaf: true, children: {}>
|
16
|
+
# }>
|
17
|
+
# }>
|
18
|
+
class DeepHashParser
|
19
|
+
class << self
|
20
|
+
# @param [<String>] paths
|
21
|
+
# @return [DeepHashNode]
|
22
|
+
def parse(paths)
|
23
|
+
node = DeepHashNode.new
|
24
|
+
paths.each do |path|
|
25
|
+
deep_push!(node.children, path.split('.').map(&:strip))
|
26
|
+
end
|
27
|
+
node
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# @param [Hash] h
|
33
|
+
# @param [<String>] field_list
|
34
|
+
def deep_push!(h, field_list)
|
35
|
+
if field_list.size < 1
|
36
|
+
# Do nothing
|
37
|
+
return
|
38
|
+
end
|
39
|
+
|
40
|
+
f = field_list.first
|
41
|
+
f_rest = field_list[1..-1]
|
42
|
+
|
43
|
+
if !h[f]
|
44
|
+
h[f] = DeepHashNode.new
|
45
|
+
end
|
46
|
+
if f_rest.size == 0
|
47
|
+
h[f].is_leaf = true
|
48
|
+
end
|
49
|
+
|
50
|
+
deep_push!(h[f].children, f_rest)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fmparser
|
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: 2019-06-17 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: google-protobuf
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.8'
|
83
|
+
description: FieldMask parameter parser
|
84
|
+
email:
|
85
|
+
- south37777@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- fmparser.gemspec
|
101
|
+
- lib/fmparser.rb
|
102
|
+
- lib/fmparser/error.rb
|
103
|
+
- lib/fmparser/node.rb
|
104
|
+
- lib/fmparser/parser.rb
|
105
|
+
- lib/fmparser/parser/deep_hash_node.rb
|
106
|
+
- lib/fmparser/parser/deep_hash_parser.rb
|
107
|
+
- lib/fmparser/version.rb
|
108
|
+
homepage: https://github.com/south37/fmparser
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubygems_version: 3.0.3
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: FieldMask parameter parser
|
131
|
+
test_files: []
|