upfluence-thrift 2.2.0 → 2.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb0c6edc7181a95e78d4f3d1da97bf55cff534a6c4f3784c998bbefa1fc0df1c
4
- data.tar.gz: c225382fb7a4bc1a0c4896f74a970c669eaf7cac96414b6c7e1be3d1dc7e66e5
3
+ metadata.gz: ea7054176b376c94f565f2a9404599502b35853c1fcba1735d7153a16ccb1fa7
4
+ data.tar.gz: d90c18cf190256af2d4a0240bb4867bcf7b6e53b2f29f021e90e9d8eb8976a4e
5
5
  SHA512:
6
- metadata.gz: 9bfbd290bda69a9ca7102791fd2291e35902691598be71d1a1429dd16329ef9dad76a38d363e76098ee2b88af7ce8df93927b0f0458f41b93b8e4ec133b227b7
7
- data.tar.gz: ffbb8d16a28268f9cb6cc82fa089e9bb06f96aefb3097077923478e4547fbc5cec43b2b640e0a790434e5ed41422236405c75bb835c5e2ac7735352e0dae182b
6
+ metadata.gz: 41c69f0f716a5f5be7d9ee0a8e06c5d66463dc5ba5a7714c3f6e8db51248933d06d78c0b32b7217112358a0dcfc9e973195efd412e6b2cac526134074af228d7
7
+ data.tar.gz: 0c6b50c4e0b91776991f82f4b80d053685a1f9c09e16ef5bb1efea0dbec866ebde4dc1c2a0dac26cd1c05d9f1d7c7afb3131078d47f2380e963767c4c4c7e78e
@@ -1,4 +1,4 @@
1
- #
1
+ #
2
2
  # Licensed to the Apache Software Foundation (ASF) under one
3
3
  # or more contributor license agreements. See the NOTICE file
4
4
  # distributed with this work for additional information
@@ -6,21 +6,21 @@
6
6
  # to you under the Apache License, Version 2.0 (the
7
7
  # "License"); you may not use this file except in compliance
8
8
  # with the License. You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing,
13
13
  # software distributed under the License is distributed on an
14
14
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
15
  # KIND, either express or implied. See the License for the
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
- #
18
+ #
19
19
 
20
20
  module Thrift
21
21
  class Exception < StandardError
22
22
  def initialize(message)
23
- super
23
+ super(message)
24
24
  @message = message
25
25
  end
26
26
 
data/lib/thrift/types.rb CHANGED
@@ -1,4 +1,4 @@
1
- #
1
+ #
2
2
  # Licensed to the Apache Software Foundation (ASF) under one
3
3
  # or more contributor license agreements. See the NOTICE file
4
4
  # distributed with this work for additional information
@@ -6,16 +6,16 @@
6
6
  # to you under the Apache License, Version 2.0 (the
7
7
  # "License"); you may not use this file except in compliance
8
8
  # with the License. You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing,
13
13
  # software distributed under the License is distributed on an
14
14
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
15
  # KIND, either express or implied. See the License for the
16
16
  # specific language governing permissions and limitations
17
17
  # under the License.
18
- #
18
+ #
19
19
 
20
20
  require 'set'
21
21
 
@@ -0,0 +1,179 @@
1
+ require 'thrift/types/known/any_types'
2
+ require 'json'
3
+ require 'yaml'
4
+
5
+ module Thrift
6
+ module Types
7
+ module Known
8
+ module Any
9
+ class CustomYAML
10
+ class << self
11
+ def dump(obj)
12
+ YAML.dump(obj)[4..-1]
13
+ end
14
+
15
+ def load(val)
16
+ YAML.safe_load(val)
17
+ end
18
+ end
19
+ end
20
+
21
+ class MetaCodec
22
+ def initialize(klass)
23
+ @klass = klass
24
+ end
25
+
26
+ def encode(obj)
27
+ @klass.dump(to_hash(obj))
28
+ end
29
+
30
+ def decode(buf, obj)
31
+ from_hash(obj, @klass.load(buf))
32
+ end
33
+
34
+ private
35
+
36
+ def to_hash(obj)
37
+ to_hash_field(obj, type: Thrift::Types::STRUCT)
38
+ end
39
+
40
+ def to_hash_field(value, field)
41
+ case field[:type]
42
+ when Thrift::Types::STRUCT
43
+ value.struct_fields.values.reduce({}) do |acc, sfield|
44
+ name = sfield[:name]
45
+ vv = value.send("#{name}?") ? to_hash_field(value.send(name), sfield) : nil
46
+ vv.nil? ? acc : acc.merge(name => vv)
47
+ end
48
+ when Thrift::Types::LIST
49
+ value.map { |vv| to_hash_field(vv, field[:element]) }
50
+ when Thrift::Types::MAP
51
+ value.reduce({}) do |acc, (k, v)|
52
+ kv = to_hash_field(k, field[:key])
53
+ vv = to_hash_field(v, field[:value])
54
+
55
+ acc.merge(kv => vv)
56
+ end
57
+ else
58
+ value
59
+ end
60
+ end
61
+
62
+ def from_hash(obj, hash)
63
+ return nil if hash.nil?
64
+
65
+ obj.struct_fields.values.each do |field|
66
+ name = field[:name]
67
+ value = hash[name]
68
+
69
+ obj.send("#{name}=", from_value_field(value, field)) if value
70
+ end
71
+
72
+ obj
73
+ end
74
+
75
+ def from_value_field(value, field)
76
+ case field[:type]
77
+ when Thrift::Types::STRUCT
78
+ from_hash(field[:class].new, value)
79
+ when Thrift::Types::LIST
80
+ value.map { |vv| from_value_field(vv, field[:element]) }
81
+ when Thrift::Types::MAP
82
+ value.reduce({}) do |acc, (k, v)|
83
+ kv = from_value_field(k, field[:key])
84
+ vv = from_value_field(v, field[:value])
85
+ acc.merge(kv => vv)
86
+ end
87
+ else
88
+ value
89
+ end
90
+ end
91
+ end
92
+
93
+ class ProtocolCodec
94
+ def initialize(protocol_factory)
95
+ @protocol_factory = protocol_factory
96
+ end
97
+
98
+ def encode(obj)
99
+ Serializer.new(@protocol_factory).serialize(obj)
100
+ end
101
+
102
+ def decode(buf, obj)
103
+ obj.read(
104
+ @protocol_factory.get_protocol(MemoryBufferTransport.new(buf))
105
+ )
106
+ end
107
+ end
108
+
109
+ DEFAULT_CODEC = ProtocolCodec.new(JsonProtocolFactory.new)
110
+ JSON_CODEC = MetaCodec.new(JSON)
111
+ YAML_CODEC = MetaCodec.new(CustomYAML)
112
+
113
+ CODECS = {
114
+ '' => DEFAULT_CODEC,
115
+ 'json' => JSON_CODEC,
116
+ 'yaml' => YAML_CODEC,
117
+ 'yml' => YAML_CODEC
118
+ }
119
+
120
+
121
+ class << self
122
+ def from_object(obj, codec_key = '')
123
+ Any.from_object(obj, codec_key)
124
+ end
125
+ end
126
+
127
+ class Any
128
+ class TypeNotHandled < StandardError; end
129
+ class CodecNotHandled < StandardError; end
130
+
131
+ class << self
132
+ def from_object(obj, codec_key = '')
133
+ struct_def = Thrift::STRUCT_DEFINITIONS.values.find do |v|
134
+ obj.is_a? v.klass
135
+ end
136
+
137
+ raise TypeNotHandled unless struct_def
138
+ raise CodecNotHandled unless CODECS[codec_key]
139
+
140
+ key = codec_key.eql?('') ? '' : "-#{codec_key}"
141
+
142
+ Any.new(
143
+ type: "thrift#{key}/#{struct_def.struct_type}",
144
+ value: CODECS[codec_key].encode(obj)
145
+ )
146
+ end
147
+ end
148
+
149
+ def to_object
150
+ codec_key, struct_type = parse_type
151
+
152
+ raise TypeNotHandled unless Thrift::STRUCT_DEFINITIONS[struct_type]
153
+ raise CodecNotHandled unless CODECS[codec_key]
154
+
155
+ res = Thrift::STRUCT_DEFINITIONS[struct_type].klass.new
156
+ CODECS[codec_key].decode(value, res)
157
+
158
+ res
159
+ end
160
+
161
+ private
162
+
163
+ def parse_type
164
+ ts = type.split('/')
165
+
166
+ if ts.length != 2 || (ts[0] =~ /^thrift-?/) != 0
167
+ raise TypeNotHandled.new
168
+ end
169
+
170
+ codec = ts[0][6..-1]
171
+ codec = codec[1..-1] if codec[0].eql?('-')
172
+
173
+ [codec, ts[1]]
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (2.3.0-upfluence)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'any_types'
9
+
10
+ module Thrift
11
+ module Types
12
+ module Known
13
+ module Any
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (2.3.0-upfluence)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ module Thrift
10
+ module Types
11
+ module Known
12
+ module Any
13
+ class Any; end
14
+
15
+ class Any
16
+ include ::Thrift::Struct, ::Thrift::Struct_Union
17
+
18
+ NAME = 'Any'.freeze
19
+ NAMESPACE = 'types.known.any'.freeze
20
+
21
+ THRIFT_FIELD_INDEX_TYPE = 1
22
+ THRIFT_FIELD_INDEX_VALUE = 2
23
+
24
+ FIELDS = {
25
+ THRIFT_FIELD_INDEX_TYPE => {type: ::Thrift::Types::STRING, name: 'type'},
26
+ THRIFT_FIELD_INDEX_VALUE => {type: ::Thrift::Types::STRING, name: 'value', binary: true}
27
+ }
28
+
29
+ def struct_fields; FIELDS; end
30
+
31
+ def validate
32
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field type is unset!') unless @type
33
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field value is unset!') unless @value
34
+ end
35
+
36
+ ::Thrift::Struct.generate_accessors self
37
+ ::Thrift.register_struct_type self
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'thrift/types/known/duration_types'
2
+
3
+ module Thrift
4
+ module Types
5
+ module Known
6
+ module Duration
7
+ class << self
8
+ def from_number(v)
9
+ Duration.from_number(v)
10
+ end
11
+ end
12
+
13
+ class Duration
14
+ class << self
15
+ def from_number(v)
16
+ Duration.new(seconds: v.to_i, nanos: ((v % 1) * 1e9).to_i)
17
+ end
18
+ end
19
+
20
+ def to_number
21
+ seconds + nanos / 1e9
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (2.3.0-upfluence)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'duration_types'
9
+
10
+ module Thrift
11
+ module Types
12
+ module Known
13
+ module Duration
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (2.3.0-upfluence)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ module Thrift
10
+ module Types
11
+ module Known
12
+ module Duration
13
+ class Duration; end
14
+
15
+ class Duration
16
+ include ::Thrift::Struct, ::Thrift::Struct_Union
17
+
18
+ NAME = 'Duration'.freeze
19
+ NAMESPACE = 'types.known.duration'.freeze
20
+
21
+ THRIFT_FIELD_INDEX_SECONDS = 1
22
+ THRIFT_FIELD_INDEX_NANOS = 2
23
+
24
+ FIELDS = {
25
+ THRIFT_FIELD_INDEX_SECONDS => {type: ::Thrift::Types::I64, name: 'seconds'},
26
+ THRIFT_FIELD_INDEX_NANOS => {type: ::Thrift::Types::I32, name: 'nanos'}
27
+ }
28
+
29
+ def struct_fields; FIELDS; end
30
+
31
+ def validate
32
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field seconds is unset!') unless @seconds
33
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field nanos is unset!') unless @nanos
34
+ end
35
+
36
+ ::Thrift::Struct.generate_accessors self
37
+ ::Thrift.register_struct_type self
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,31 @@
1
+ require 'thrift/types/known/timestamp_types'
2
+
3
+ module Thrift
4
+ module Types
5
+ module Known
6
+ module Timestamp
7
+ class << self
8
+ def from_time(v)
9
+ Timestamp.from_time(v)
10
+ end
11
+
12
+ def now
13
+ from_time(Time.now)
14
+ end
15
+ end
16
+
17
+ class Timestamp
18
+ class << self
19
+ def from_time(v)
20
+ Timestamp.new(seconds: v.to_i, nanos: v.nsec)
21
+ end
22
+ end
23
+
24
+ def to_time
25
+ Time.at(seconds, nanos, :nsec)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (2.3.0-upfluence)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'timestamp_types'
9
+
10
+ module Thrift
11
+ module Types
12
+ module Known
13
+ module Timestamp
14
+ end
15
+ end
16
+ end
17
+ end