pb_json_parser 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/pb_json_parser/ast/association.rb +8 -8
- data/lib/pb_json_parser/ast/field.rb +11 -2
- data/lib/pb_json_parser/ast/message.rb +1 -1
- data/lib/pb_json_parser/parser.rb +47 -5
- data/lib/pb_json_parser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b93dfc26dda0ff4068e3fc7b3d95c3cd1aff7eb74ff624ca891b8372546d488
|
4
|
+
data.tar.gz: 03a8b5f40491616094293620e4660de4dfd98939029a93944987443cdaebe96d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f15b0f9e178f081426f6d6d907fa17612ffed8e4485710b3220b4ea03353f3bbe3632cea3ee142bef2ff18feff4c719a340a76573c6fa6af97302d4fe084c160
|
7
|
+
data.tar.gz: 1e7b6f889c3fdb3787e9b0ede998ebed0834b26f9f00ff6e2ecd16dbc5fc2fa1bd0304b0363ebca85f886c1d5ae22d0306ee2d11407472d67b0ef4a67897f929
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -62,8 +62,8 @@ Finally you can parse the json by `PbJsonParser`.
|
|
62
62
|
```ruby
|
63
63
|
[6] pry(main)> PbJsonParser.parse(json: File.read('./protos/resources.proto.json'), filename: 'resources.proto').map(&:to_h)
|
64
64
|
=> [
|
65
|
-
{:name=>"User", :fields=>["id"], :assocs=>[{:name=>"profile", :
|
66
|
-
{:name=>"Profile", :fields=>["id", "birthday"], :assocs=>[]}
|
65
|
+
{:name=>"User", :fields=>[{:name=>"id", :type=>"int32"}], :assocs=>[{:name=>"profile", :kind=>"has_one", :class_name=>"Profile"}]},
|
66
|
+
{:name=>"Profile", :fields=>[{:name=>"id", :type=>"int32"}, {:name=>"birthday", :type=>".google.protobuf.Timestamp"}], :assocs=>[]}
|
67
67
|
]
|
68
68
|
```
|
69
69
|
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module PbJsonParser
|
2
2
|
module AST
|
3
3
|
class Association
|
4
|
-
|
4
|
+
Kind = {
|
5
5
|
has_one: "has_one".freeze,
|
6
6
|
has_many: "has_many".freeze,
|
7
7
|
}.freeze
|
8
8
|
|
9
|
-
attr_reader :name, :
|
9
|
+
attr_reader :name, :kind, :class_name
|
10
10
|
|
11
11
|
# @param [String] name
|
12
|
-
# @param [String]
|
12
|
+
# @param [String] kind
|
13
13
|
# @param [String] class_name
|
14
|
-
def initialize(name:,
|
14
|
+
def initialize(name:, kind:, class_name:)
|
15
15
|
@name = name
|
16
|
-
@
|
16
|
+
@kind = kind
|
17
17
|
@class_name = class_name
|
18
18
|
validate!
|
19
19
|
end
|
@@ -21,7 +21,7 @@ module PbJsonParser
|
|
21
21
|
def to_h
|
22
22
|
{
|
23
23
|
name: @name,
|
24
|
-
|
24
|
+
kind: @kind,
|
25
25
|
class_name: @class_name,
|
26
26
|
}
|
27
27
|
end
|
@@ -29,8 +29,8 @@ module PbJsonParser
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def validate!
|
32
|
-
if !
|
33
|
-
raise "Invalid
|
32
|
+
if !Kind.values.include?(@kind)
|
33
|
+
raise "Invalid kind: #{@kind}"
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -1,11 +1,20 @@
|
|
1
1
|
module PbJsonParser
|
2
2
|
module AST
|
3
3
|
class Field
|
4
|
-
attr_reader :name
|
4
|
+
attr_reader :name, :type
|
5
5
|
|
6
6
|
# @param [String] name
|
7
|
-
|
7
|
+
# @param [String] type
|
8
|
+
def initialize(name:, type:)
|
8
9
|
@name = name
|
10
|
+
@type = type
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_h
|
14
|
+
{
|
15
|
+
name: @name,
|
16
|
+
type: @type,
|
17
|
+
}
|
9
18
|
end
|
10
19
|
end
|
11
20
|
end
|
@@ -41,10 +41,12 @@ module PbJsonParser
|
|
41
41
|
if is_assoc?(f)
|
42
42
|
m.push_assoc(parse_assoc(f))
|
43
43
|
else
|
44
|
-
m.push_field(
|
44
|
+
m.push_field(parse_field(f, type: f["type_name"]))
|
45
45
|
end
|
46
|
+
when 14 # type: TYPE_ENUM
|
47
|
+
m.push_field(parse_field(f, type: f["type_name"]))
|
46
48
|
else
|
47
|
-
m.push_field(
|
49
|
+
m.push_field(parse_field(f, type: to_type(f["type"])))
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
@@ -56,15 +58,15 @@ module PbJsonParser
|
|
56
58
|
def parse_assoc(field)
|
57
59
|
case field["label"]
|
58
60
|
when 3 # label: LABEL_REPEATED
|
59
|
-
|
61
|
+
kind = AST::Association::Kind[:has_many]
|
60
62
|
else
|
61
|
-
|
63
|
+
kind = AST::Association::Kind[:has_one]
|
62
64
|
end
|
63
65
|
class_name = field_message_type(field)
|
64
66
|
|
65
67
|
assoc = AST::Association.new(
|
66
68
|
name: field["name"],
|
67
|
-
|
69
|
+
kind: kind,
|
68
70
|
class_name: class_name,
|
69
71
|
)
|
70
72
|
end
|
@@ -87,6 +89,46 @@ module PbJsonParser
|
|
87
89
|
field["type_name"][(@package.size + 2)..-1]
|
88
90
|
end
|
89
91
|
|
92
|
+
# @param [{ String => Any }] field
|
93
|
+
# @param [String] type
|
94
|
+
# @return [AST::Field]
|
95
|
+
def parse_field(field, type:)
|
96
|
+
AST::Field.new(name: field["name"], type: type)
|
97
|
+
end
|
98
|
+
|
99
|
+
# @param [Integer] type
|
100
|
+
# @return [String]
|
101
|
+
def to_type(type)
|
102
|
+
case type
|
103
|
+
when 1
|
104
|
+
'double'
|
105
|
+
when 2
|
106
|
+
'float'
|
107
|
+
when 3
|
108
|
+
'int64'
|
109
|
+
when 4
|
110
|
+
'uint64'
|
111
|
+
when 5
|
112
|
+
'int32'
|
113
|
+
when 8
|
114
|
+
'bool'
|
115
|
+
when 9
|
116
|
+
'string'
|
117
|
+
when 12
|
118
|
+
'bytes'
|
119
|
+
when 13
|
120
|
+
'uint32'
|
121
|
+
when 17
|
122
|
+
'sint32'
|
123
|
+
when 18
|
124
|
+
'sint64'
|
125
|
+
else
|
126
|
+
# TODO(south37) Handle other scalar value types
|
127
|
+
# cf. https://developers.google.com/protocol-buffers/docs/proto#scalar
|
128
|
+
"type[#{type}]"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
90
132
|
# @param [String] json
|
91
133
|
# @return [Hash]
|
92
134
|
def read_data(json, filename)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pb_json_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nao Minami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|