log4ever 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +33 -0
  2. data/.project +11 -0
  3. data/README.md +36 -0
  4. data/config/evernote.auth.test.yml +1 -0
  5. data/config/log4r.xml +19 -0
  6. data/lib/log4r/evernote/lib/Evernote/EDAM/errors_constants.rb +14 -0
  7. data/lib/log4r/evernote/lib/Evernote/EDAM/errors_types.rb +128 -0
  8. data/lib/log4r/evernote/lib/Evernote/EDAM/limits_constants.rb +240 -0
  9. data/lib/log4r/evernote/lib/Evernote/EDAM/limits_types.rb +13 -0
  10. data/lib/log4r/evernote/lib/Evernote/EDAM/note_store.rb +5487 -0
  11. data/lib/log4r/evernote/lib/Evernote/EDAM/note_store_constants.rb +14 -0
  12. data/lib/log4r/evernote/lib/Evernote/EDAM/note_store_types.rb +1012 -0
  13. data/lib/log4r/evernote/lib/Evernote/EDAM/types_constants.rb +20 -0
  14. data/lib/log4r/evernote/lib/Evernote/EDAM/types_types.rb +1792 -0
  15. data/lib/log4r/evernote/lib/Evernote/EDAM/user_store.rb +549 -0
  16. data/lib/log4r/evernote/lib/Evernote/EDAM/user_store_constants.rb +18 -0
  17. data/lib/log4r/evernote/lib/Evernote/EDAM/user_store_types.rb +415 -0
  18. data/lib/log4r/evernote/lib/thrift/client.rb +62 -0
  19. data/lib/log4r/evernote/lib/thrift/core_ext/fixnum.rb +29 -0
  20. data/lib/log4r/evernote/lib/thrift/core_ext.rb +23 -0
  21. data/lib/log4r/evernote/lib/thrift/exceptions.rb +84 -0
  22. data/lib/log4r/evernote/lib/thrift/processor.rb +57 -0
  23. data/lib/log4r/evernote/lib/thrift/protocol/base_protocol.rb +290 -0
  24. data/lib/log4r/evernote/lib/thrift/protocol/binary_protocol.rb +229 -0
  25. data/lib/log4r/evernote/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  26. data/lib/log4r/evernote/lib/thrift/protocol/compact_protocol.rb +426 -0
  27. data/lib/log4r/evernote/lib/thrift/serializer/deserializer.rb +33 -0
  28. data/lib/log4r/evernote/lib/thrift/serializer/serializer.rb +34 -0
  29. data/lib/log4r/evernote/lib/thrift/server/base_server.rb +31 -0
  30. data/lib/log4r/evernote/lib/thrift/server/mongrel_http_server.rb +58 -0
  31. data/lib/log4r/evernote/lib/thrift/server/nonblocking_server.rb +305 -0
  32. data/lib/log4r/evernote/lib/thrift/server/simple_server.rb +43 -0
  33. data/lib/log4r/evernote/lib/thrift/server/thread_pool_server.rb +75 -0
  34. data/lib/log4r/evernote/lib/thrift/server/threaded_server.rb +47 -0
  35. data/lib/log4r/evernote/lib/thrift/struct.rb +237 -0
  36. data/lib/log4r/evernote/lib/thrift/struct_union.rb +192 -0
  37. data/lib/log4r/evernote/lib/thrift/thrift_native.rb +24 -0
  38. data/lib/log4r/evernote/lib/thrift/transport/base_server_transport.rb +37 -0
  39. data/lib/log4r/evernote/lib/thrift/transport/base_transport.rb +107 -0
  40. data/lib/log4r/evernote/lib/thrift/transport/buffered_transport.rb +108 -0
  41. data/lib/log4r/evernote/lib/thrift/transport/framed_transport.rb +116 -0
  42. data/lib/log4r/evernote/lib/thrift/transport/http_client_transport.rb +53 -0
  43. data/lib/log4r/evernote/lib/thrift/transport/io_stream_transport.rb +39 -0
  44. data/lib/log4r/evernote/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  45. data/lib/log4r/evernote/lib/thrift/transport/server_socket.rb +63 -0
  46. data/lib/log4r/evernote/lib/thrift/transport/socket.rb +137 -0
  47. data/lib/log4r/evernote/lib/thrift/transport/unix_server_socket.rb +60 -0
  48. data/lib/log4r/evernote/lib/thrift/transport/unix_socket.rb +40 -0
  49. data/lib/log4r/evernote/lib/thrift/types.rb +101 -0
  50. data/lib/log4r/evernote/lib/thrift/union.rb +179 -0
  51. data/lib/log4r/evernote/lib/thrift.rb +64 -0
  52. data/lib/log4r/evernote.rb +206 -0
  53. data/lib/log4r/outputter/evernoteoutputter.rb +156 -0
  54. data/spec/log4ever_spec.rb +94 -0
  55. data/spec/spec_helper.rb +14 -0
  56. metadata +105 -0
@@ -0,0 +1,125 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ module Thrift
22
+ class MemoryBufferTransport < BaseTransport
23
+ GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
24
+
25
+ # If you pass a string to this, you should #dup that string
26
+ # unless you want it to be modified by #read and #write
27
+ #--
28
+ # this behavior is no longer required. If you wish to change it
29
+ # go ahead, just make sure the specs pass
30
+ def initialize(buffer = nil)
31
+ @buf = buffer || ''
32
+ @index = 0
33
+ end
34
+
35
+ def open?
36
+ return true
37
+ end
38
+
39
+ def open
40
+ end
41
+
42
+ def close
43
+ end
44
+
45
+ def peek
46
+ @index < @buf.size
47
+ end
48
+
49
+ # this method does not use the passed object directly but copies it
50
+ def reset_buffer(new_buf = '')
51
+ @buf.replace new_buf
52
+ @index = 0
53
+ end
54
+
55
+ def available
56
+ @buf.length - @index
57
+ end
58
+
59
+ def read(len)
60
+ data = @buf.slice(@index, len)
61
+ @index += len
62
+ @index = @buf.size if @index > @buf.size
63
+ if @index >= GARBAGE_BUFFER_SIZE
64
+ @buf = @buf.slice(@index..-1)
65
+ @index = 0
66
+ end
67
+ if data.size < len
68
+ raise EOFError, "Not enough bytes remain in buffer"
69
+ end
70
+ data
71
+ end
72
+
73
+ def read_byte
74
+ raise EOFError.new("Not enough bytes remain in buffer") if @index >= @buf.size
75
+ val = ::Thrift::TransportUtils.get_string_byte(@buf, @index)
76
+ @index += 1
77
+ if @index >= GARBAGE_BUFFER_SIZE
78
+ @buf = @buf.slice(@index..-1)
79
+ @index = 0
80
+ end
81
+ val
82
+ end
83
+
84
+ def read_into_buffer(buffer, size)
85
+ i = 0
86
+ while i < size
87
+ raise EOFError.new("Not enough bytes remain in buffer") if @index >= @buf.size
88
+
89
+ # The read buffer has some data now, so copy bytes over to the output buffer.
90
+ byte = ::Thrift::TransportUtils.get_string_byte(@buf, @index)
91
+ ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
92
+ @index += 1
93
+ i += 1
94
+ end
95
+ if @index >= GARBAGE_BUFFER_SIZE
96
+ @buf = @buf.slice(@index..-1)
97
+ @index = 0
98
+ end
99
+ i
100
+ end
101
+
102
+ def write(wbuf)
103
+ @buf << wbuf
104
+ end
105
+
106
+ def flush
107
+ end
108
+
109
+ def inspect_buffer
110
+ out = []
111
+ for idx in 0...(@buf.size)
112
+ # if idx != 0
113
+ # out << " "
114
+ # end
115
+
116
+ if idx == @index
117
+ out << ">"
118
+ end
119
+
120
+ out << @buf[idx].ord.to_s(16)
121
+ end
122
+ out.join(" ")
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'socket'
22
+
23
+ module Thrift
24
+ class ServerSocket < BaseServerTransport
25
+ # call-seq: initialize(host = nil, port)
26
+ def initialize(host_or_port, port = nil)
27
+ if port
28
+ @host = host_or_port
29
+ @port = port
30
+ else
31
+ @host = nil
32
+ @port = host_or_port
33
+ end
34
+ @handle = nil
35
+ end
36
+
37
+ attr_reader :handle
38
+
39
+ def listen
40
+ @handle = TCPServer.new(@host, @port)
41
+ end
42
+
43
+ def accept
44
+ unless @handle.nil?
45
+ sock = @handle.accept
46
+ trans = Socket.new
47
+ trans.handle = sock
48
+ trans
49
+ end
50
+ end
51
+
52
+ def close
53
+ @handle.close unless @handle.nil? or @handle.closed?
54
+ @handle = nil
55
+ end
56
+
57
+ def closed?
58
+ @handle.nil? or @handle.closed?
59
+ end
60
+
61
+ alias to_io handle
62
+ end
63
+ end
@@ -0,0 +1,137 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'socket'
22
+
23
+ module Thrift
24
+ class Socket < BaseTransport
25
+ def initialize(host='localhost', port=9090, timeout=nil)
26
+ @host = host
27
+ @port = port
28
+ @timeout = timeout
29
+ @desc = "#{host}:#{port}"
30
+ @handle = nil
31
+ end
32
+
33
+ attr_accessor :handle, :timeout
34
+
35
+ def open
36
+ begin
37
+ addrinfo = ::Socket::getaddrinfo(@host, @port).first
38
+ @handle = ::Socket.new(addrinfo[4], ::Socket::SOCK_STREAM, 0)
39
+ sockaddr = ::Socket.sockaddr_in(addrinfo[1], addrinfo[3])
40
+ begin
41
+ @handle.connect_nonblock(sockaddr)
42
+ rescue Errno::EINPROGRESS
43
+ unless IO.select(nil, [ @handle ], nil, @timeout)
44
+ raise TransportException.new(TransportException::NOT_OPEN, "Connection timeout to #{@desc}")
45
+ end
46
+ begin
47
+ @handle.connect_nonblock(sockaddr)
48
+ rescue Errno::EISCONN
49
+ end
50
+ end
51
+ @handle
52
+ rescue StandardError => e
53
+ raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: #{e}")
54
+ end
55
+ end
56
+
57
+ def open?
58
+ !@handle.nil? and !@handle.closed?
59
+ end
60
+
61
+ def write(str)
62
+ raise IOError, "closed stream" unless open?
63
+ begin
64
+ if @timeout.nil? or @timeout == 0
65
+ @handle.write(str)
66
+ else
67
+ len = 0
68
+ start = Time.now
69
+ while Time.now - start < @timeout
70
+ rd, wr, = IO.select(nil, [@handle], nil, @timeout)
71
+ if wr and not wr.empty?
72
+ len += @handle.write_nonblock(str[len..-1])
73
+ break if len >= str.length
74
+ end
75
+ end
76
+ if len < str.length
77
+ raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out writing #{str.length} bytes to #{@desc}")
78
+ else
79
+ len
80
+ end
81
+ end
82
+ rescue TransportException => e
83
+ # pass this on
84
+ raise e
85
+ rescue StandardError => e
86
+ @handle.close
87
+ @handle = nil
88
+ raise TransportException.new(TransportException::NOT_OPEN, e.message)
89
+ end
90
+ end
91
+
92
+ def read(sz)
93
+ raise IOError, "closed stream" unless open?
94
+
95
+ begin
96
+ if @timeout.nil? or @timeout == 0
97
+ data = @handle.readpartial(sz)
98
+ else
99
+ # it's possible to interrupt select for something other than the timeout
100
+ # so we need to ensure we've waited long enough, but not too long
101
+ start = Time.now
102
+ timespent = 0
103
+ rd = loop do
104
+ rd, = IO.select([@handle], nil, nil, @timeout - timespent)
105
+ timespent = Time.now - start
106
+ break rd if (rd and not rd.empty?) or timespent >= @timeout
107
+ end
108
+ if rd.nil? or rd.empty?
109
+ raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out reading #{sz} bytes from #{@desc}")
110
+ else
111
+ data = @handle.readpartial(sz)
112
+ end
113
+ end
114
+ rescue TransportException => e
115
+ # don't let this get caught by the StandardError handler
116
+ raise e
117
+ rescue StandardError => e
118
+ @handle.close unless @handle.closed?
119
+ @handle = nil
120
+ raise TransportException.new(TransportException::NOT_OPEN, e.message)
121
+ end
122
+ if (data.nil? or data.length == 0)
123
+ raise TransportException.new(TransportException::UNKNOWN, "Socket: Could not read #{sz} bytes from #{@desc}")
124
+ end
125
+ data
126
+ end
127
+
128
+ def close
129
+ @handle.close unless @handle.nil? or @handle.closed?
130
+ @handle = nil
131
+ end
132
+
133
+ def to_io
134
+ @handle
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'socket'
22
+
23
+ module Thrift
24
+ class UNIXServerSocket < BaseServerTransport
25
+ def initialize(path)
26
+ @path = path
27
+ @handle = nil
28
+ end
29
+
30
+ attr_accessor :handle
31
+
32
+ def listen
33
+ @handle = ::UNIXServer.new(@path)
34
+ end
35
+
36
+ def accept
37
+ unless @handle.nil?
38
+ sock = @handle.accept
39
+ trans = UNIXSocket.new(nil)
40
+ trans.handle = sock
41
+ trans
42
+ end
43
+ end
44
+
45
+ def close
46
+ if @handle
47
+ @handle.close unless @handle.closed?
48
+ @handle = nil
49
+ # UNIXServer doesn't delete the socket file, so we have to do it ourselves
50
+ File.delete(@path)
51
+ end
52
+ end
53
+
54
+ def closed?
55
+ @handle.nil? or @handle.closed?
56
+ end
57
+
58
+ alias to_io handle
59
+ end
60
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ require 'socket'
22
+
23
+ module Thrift
24
+ class UNIXSocket < Socket
25
+ def initialize(path, timeout=nil)
26
+ @path = path
27
+ @timeout = timeout
28
+ @desc = @path # for read()'s error
29
+ @handle = nil
30
+ end
31
+
32
+ def open
33
+ begin
34
+ @handle = ::UNIXSocket.new(@path)
35
+ rescue StandardError
36
+ raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,101 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'set'
21
+
22
+ module Thrift
23
+ module Types
24
+ STOP = 0
25
+ VOID = 1
26
+ BOOL = 2
27
+ BYTE = 3
28
+ DOUBLE = 4
29
+ I16 = 6
30
+ I32 = 8
31
+ I64 = 10
32
+ STRING = 11
33
+ STRUCT = 12
34
+ MAP = 13
35
+ SET = 14
36
+ LIST = 15
37
+ end
38
+
39
+ class << self
40
+ attr_accessor :type_checking
41
+ end
42
+
43
+ class TypeError < Exception
44
+ end
45
+
46
+ def self.check_type(value, field, name, skip_nil=true)
47
+ return if value.nil? and skip_nil
48
+ klasses = case field[:type]
49
+ when Types::VOID
50
+ NilClass
51
+ when Types::BOOL
52
+ [TrueClass, FalseClass]
53
+ when Types::BYTE, Types::I16, Types::I32, Types::I64
54
+ Integer
55
+ when Types::DOUBLE
56
+ Float
57
+ when Types::STRING
58
+ String
59
+ when Types::STRUCT
60
+ [Struct, Union]
61
+ when Types::MAP
62
+ Hash
63
+ when Types::SET
64
+ Set
65
+ when Types::LIST
66
+ Array
67
+ end
68
+ valid = klasses && [*klasses].any? { |klass| klass === value }
69
+ raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
70
+ # check elements now
71
+ case field[:type]
72
+ when Types::MAP
73
+ value.each_pair do |k,v|
74
+ check_type(k, field[:key], "#{name}.key", false)
75
+ check_type(v, field[:value], "#{name}.value", false)
76
+ end
77
+ when Types::SET, Types::LIST
78
+ value.each do |el|
79
+ check_type(el, field[:element], "#{name}.element", false)
80
+ end
81
+ when Types::STRUCT
82
+ raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
83
+ end
84
+ end
85
+
86
+ def self.type_name(type)
87
+ Types.constants.each do |const|
88
+ return "Types::#{const}" if Types.const_get(const) == type
89
+ end
90
+ nil
91
+ end
92
+
93
+ module MessageTypes
94
+ CALL = 1
95
+ REPLY = 2
96
+ EXCEPTION = 3
97
+ ONEWAY = 4
98
+ end
99
+ end
100
+
101
+ Thrift.type_checking = false if Thrift.type_checking.nil?
@@ -0,0 +1,179 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ module Thrift
21
+ class Union
22
+ def initialize(name=nil, value=nil)
23
+ if name
24
+ if name.is_a? Hash
25
+ if name.size > 1
26
+ raise "#{self.class} cannot be instantiated with more than one field!"
27
+ end
28
+
29
+ name, value = name.keys.first, name.values.first
30
+ end
31
+
32
+ if Thrift.type_checking
33
+ raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s)
34
+ end
35
+
36
+ if value.nil?
37
+ raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!"
38
+ end
39
+
40
+ Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking
41
+ elsif !value.nil?
42
+ raise Exception, "Value provided, but no name!"
43
+ end
44
+ @setfield = name
45
+ @value = value
46
+ end
47
+
48
+ def inspect
49
+ if get_set_field
50
+ "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
51
+ else
52
+ "<#{self.class} >"
53
+ end
54
+ end
55
+
56
+ def read(iprot)
57
+ iprot.read_struct_begin
58
+ fname, ftype, fid = iprot.read_field_begin
59
+ handle_message(iprot, fid, ftype)
60
+ iprot.read_field_end
61
+
62
+ fname, ftype, fid = iprot.read_field_begin
63
+ raise "Too many fields for union" unless (ftype == Types::STOP)
64
+
65
+ iprot.read_struct_end
66
+ validate
67
+ end
68
+
69
+ def write(oprot)
70
+ validate
71
+ oprot.write_struct_begin(self.class.name)
72
+
73
+ fid = self.name_to_id(@setfield.to_s)
74
+
75
+ field_info = struct_fields[fid]
76
+ type = field_info[:type]
77
+ if is_container? type
78
+ oprot.write_field_begin(@setfield, type, fid)
79
+ write_container(oprot, @value, field_info)
80
+ oprot.write_field_end
81
+ else
82
+ oprot.write_field(@setfield, type, fid, @value)
83
+ end
84
+
85
+ oprot.write_field_stop
86
+ oprot.write_struct_end
87
+ end
88
+
89
+ def ==(other)
90
+ other != nil && @setfield == other.get_set_field && @value == other.get_value
91
+ end
92
+
93
+ def eql?(other)
94
+ self.class == other.class && self == other
95
+ end
96
+
97
+ def hash
98
+ [self.class.name, @setfield, @value].hash
99
+ end
100
+
101
+ def self.field_accessor(klass, field_info)
102
+ klass.send :define_method, field_info[:name] do
103
+ if field_info[:name].to_sym == @setfield
104
+ @value
105
+ else
106
+ raise RuntimeError, "#{field_info[:name]} is not union's set field."
107
+ end
108
+ end
109
+
110
+ klass.send :define_method, "#{field_info[:name]}=" do |value|
111
+ Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
112
+ @setfield = field_info[:name].to_sym
113
+ @value = value
114
+ end
115
+ end
116
+
117
+ def self.qmark_isset_method(klass, field_info)
118
+ klass.send :define_method, "#{field_info[:name]}?" do
119
+ get_set_field == field_info[:name].to_sym && !get_value.nil?
120
+ end
121
+ end
122
+
123
+ def self.generate_accessors(klass)
124
+ klass::FIELDS.values.each do |field_info|
125
+ field_accessor(klass, field_info)
126
+ qmark_isset_method(klass, field_info)
127
+ end
128
+ end
129
+
130
+ # get the symbol that indicates what the currently set field type is.
131
+ def get_set_field
132
+ @setfield
133
+ end
134
+
135
+ # get the current value of this union, regardless of what the set field is.
136
+ # generally, you should only use this method when you don't know in advance
137
+ # what field to expect.
138
+ def get_value
139
+ @value
140
+ end
141
+
142
+ def <=>(other)
143
+ if self.class == other.class
144
+ if get_set_field == other.get_set_field
145
+ if get_set_field.nil?
146
+ 0
147
+ else
148
+ get_value <=> other.get_value
149
+ end
150
+ else
151
+ if get_set_field && other.get_set_field.nil?
152
+ -1
153
+ elsif get_set_field.nil? && other.get_set_field
154
+ 1
155
+ elsif get_set_field.nil? && other.get_set_field.nil?
156
+ 0
157
+ else
158
+ name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s)
159
+ end
160
+ end
161
+ else
162
+ self.class <=> other.class
163
+ end
164
+ end
165
+
166
+ protected
167
+
168
+ def handle_message(iprot, fid, ftype)
169
+ field = struct_fields[fid]
170
+ if field and field[:type] == ftype
171
+ @value = read_field(iprot, field)
172
+ name = field[:name].to_sym
173
+ @setfield = name
174
+ else
175
+ iprot.skip(ftype)
176
+ end
177
+ end
178
+ end
179
+ end