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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ed2211bc248280019756fcfd8f74c8f5abe7d472ef5eaa5e38b72c35e96a633
4
- data.tar.gz: 3609f021a03be07a3bac89c0ca1a1ddd77c1aa4748ff8ef81a605fbe2983afe7
3
+ metadata.gz: 5b93dfc26dda0ff4068e3fc7b3d95c3cd1aff7eb74ff624ca891b8372546d488
4
+ data.tar.gz: 03a8b5f40491616094293620e4660de4dfd98939029a93944987443cdaebe96d
5
5
  SHA512:
6
- metadata.gz: 8ade920f219c29475036b0f60bc88cac02da438e9ddbd5ca95d8d4a14c830bb50ed9b9789365be5a692804274a967279e11d71430ecda5c0607a9e283fd7df77
7
- data.tar.gz: 8a7b8d36cdf82c1fa895e3f44deba5d693f20ec07921a7144bb19aefe508c1a90ee9f85f50fa7373c299313f54a2cfe7616e26cc586e37848318192a7dec2a37
6
+ metadata.gz: f15b0f9e178f081426f6d6d907fa17612ffed8e4485710b3220b4ea03353f3bbe3632cea3ee142bef2ff18feff4c719a340a76573c6fa6af97302d4fe084c160
7
+ data.tar.gz: 1e7b6f889c3fdb3787e9b0ede998ebed0834b26f9f00ff6e2ecd16dbc5fc2fa1bd0304b0363ebca85f886c1d5ae22d0306ee2d11407472d67b0ef4a67897f929
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pb_json_parser (0.1.0)
4
+ pb_json_parser (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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", :type=>"has_one", :class_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
- Type = {
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, :type, :class_name
9
+ attr_reader :name, :kind, :class_name
10
10
 
11
11
  # @param [String] name
12
- # @param [String] type
12
+ # @param [String] kind
13
13
  # @param [String] class_name
14
- def initialize(name:, type:, class_name:)
14
+ def initialize(name:, kind:, class_name:)
15
15
  @name = name
16
- @type = type
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
- type: @type,
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 !Type.values.include?(@type)
33
- raise "Invalid type: #{@type}"
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
- def initialize(name:)
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
@@ -23,7 +23,7 @@ module PbJsonParser
23
23
  def to_h
24
24
  {
25
25
  name: @name,
26
- fields: @fields.map(&:name),
26
+ fields: @fields.map(&:to_h),
27
27
  assocs: @assocs.map(&:to_h),
28
28
  }
29
29
  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(AST::Field.new(name: f["name"]))
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(AST::Field.new(name: f["name"]))
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
- type = AST::Association::Type[:has_many]
61
+ kind = AST::Association::Kind[:has_many]
60
62
  else
61
- type = AST::Association::Type[:has_one]
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
- type: type,
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)
@@ -1,3 +1,3 @@
1
1
  module PbJsonParser
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-09-11 00:00:00.000000000 Z
11
+ date: 2018-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler