Class: Stark::Ruby
- Inherits:
-
Object
- Object
- Stark::Ruby
- Defined in:
- lib/stark/ruby.rb
Constant Summary collapse
- CoreTypes =
{ 'bool' => "::Thrift::Types::BOOL", 'byte' => "::Thrift::Types::BYTE", 'double' => "::Thrift::Types::DOUBLE", 'i16' => "::Thrift::Types::I16", 'i32' => "::Thrift::Types::I32", 'i64' => "::Thrift::Types::I64", 'string' => '::Thrift::Types::STRING', 'binary' => '::Thrift::Types::STRING', 'struct' => '::Thrift::Types::STRUCT', 'map' => '::Thrift::Types::MAP', 'set' => '::Thrift::Types::SET', 'list' => '::Thrift::Types::LIST' }
- ReadFunc =
{ 'bool' => 'read_bool', 'byte' => 'read_byte', 'double' => 'read_double', 'i16' => "read_i16", 'i32' => "read_i32", 'i64' => "read_i64", 'string' => 'read_string', 'binary' => 'read_string', }
- WriteFunc =
{ 'bool' => 'write_bool', 'byte' => 'write_byte', 'double' => 'write_double', 'i16' => "write_i16", 'i32' => "write_i32", 'i64' => "write_i64", 'string' => 'write_string', 'binary' => 'write_string', }
Instance Method Summary collapse
- #close ⇒ Object
- #const_to_ruby(val) ⇒ Object
- #indent ⇒ Object
-
#initialize(stream = STDOUT) ⇒ Ruby
constructor
A new instance of Ruby.
- #o(str = nil) ⇒ Object
- #object_type(t) ⇒ Object
- #outdent ⇒ Object
- #process_enum(enum) ⇒ Object
- #process_exception(str) ⇒ Object
- #process_include(inc) ⇒ Object
- #process_namespace(ns) ⇒ Object
- #process_service(serv) ⇒ Object
- #process_struct(str) ⇒ Object
- #read_func(t) ⇒ Object
- #read_type(t, lhs, found_type = 'rtype') ⇒ Object
- #run(ast) ⇒ Object
- #type(t) ⇒ Object
- #wire_type(t) ⇒ Object
- #wire_type_and_ruby_type(t) ⇒ Object
- #write_client(serv) ⇒ Object
- #write_field(ft, name, idx) ⇒ Object
- #write_field_declarations(fields) ⇒ Object
- #write_func(t) ⇒ Object
- #write_processor(serv) ⇒ Object
- #write_protocol ⇒ Object
- #write_type(ft, name) ⇒ Object
Constructor Details
#initialize(stream = STDOUT) ⇒ Ruby
Returns a new instance of Ruby.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/stark/ruby.rb', line 5 def initialize(stream=STDOUT) @namespace = nil @indent = 0 @structs = {} @enums = {} @exceptions = {} @stream = stream o "require 'set'" o "require 'stark/client'" o "require 'stark/processor'" o "require 'stark/struct'" o "require 'stark/exception'" end |
Instance Method Details
#close ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/stark/ruby.rb', line 27 def close write_protocol if @namespace outdent o "end" end end |
#const_to_ruby(val) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/stark/ruby.rb', line 65 def const_to_ruby(val) case val when Stark::Parser::AST::ConstString return %Q!"#{val.value}"! when Stark::Parser::AST::ConstInt return val.value when Stark::Parser::AST::ConstDouble return val.value when Stark::Parser::AST::ConstIdentifier return val.value.to_sym when Stark::Parser::AST::ConstList parts = val.values.map { |x| const_to_ruby(x) } return "[#{parts.join(', ')}]" when Stark::Parser::AST::ConstMap parts = val.values.map { |(k,v)| const_to_ruby(k) + " => " + const_to_ruby(v) } return "{#{parts.join(', ')}}" else raise "Unsupported default type: #{val.class}" end end |
#indent ⇒ Object
137 138 139 |
# File 'lib/stark/ruby.rb', line 137 def indent @indent += 2 end |
#o(str = nil) ⇒ Object
132 133 134 135 |
# File 'lib/stark/ruby.rb', line 132 def o(str = nil) @stream.print(" " * @indent) if str @stream.puts str end |
#object_type(t) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/stark/ruby.rb', line 208 def object_type(t) case t when Stark::Parser::AST::Map "map<#{object_type(t.key)},#{object_type(t.value)}>" when Stark::Parser::AST::List "list<#{object_type(t.value)}>" when Stark::Parser::AST::Set "set<#{object_type(t.value)}>" else t end end |
#outdent ⇒ Object
141 142 143 |
# File 'lib/stark/ruby.rb', line 141 def outdent @indent -= 2 end |
#process_enum(enum) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/stark/ruby.rb', line 53 def process_enum(enum) @enums[enum.name] = enum e = "Enum_#{enum.name}" o "#{e} = Hash.new { |h,k| p [:bad, k]; h[k] = -1 }" idx = 0 enum.values.each do |f| o "#{e}[#{idx}] = :'#{f}'" o "#{e}[:'#{f}'] = #{idx}" idx += 1 end end |
#process_exception(str) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/stark/ruby.rb', line 121 def process_exception(str) @exceptions[str.name] = str o o "class #{str.name} < Stark::Exception" indent write_field_declarations str.fields outdent o "end" end |
#process_include(inc) ⇒ Object
49 50 51 |
# File 'lib/stark/ruby.rb', line 49 def process_include(inc) raise NotImplementedError, "include not implemented" end |
#process_namespace(ns) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/stark/ruby.rb', line 35 def process_namespace(ns) return unless [nil, 'rb'].include?(ns.lang) @namespace = ns.namespace.gsub('.', '::') parts = @namespace.split('::') if parts.length > 1 0.upto(parts.length - 2) do |i| o "module #{parts[0..i].join('::')}; end unless defined?(#{parts[0..i].join('::')})" end end o o "module #{@namespace}" indent end |
#process_service(serv) ⇒ Object
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/stark/ruby.rb', line 561 def process_service(serv) unless @protocol_declared o o "module Protocol; end" @protocol_declared = true end o o "module #{serv.name}" indent write_client serv o write_processor serv outdent o "end" end |
#process_struct(str) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/stark/ruby.rb', line 110 def process_struct(str) @structs[str.name] = str o o "class #{str.name} < Stark::Struct" indent write_field_declarations str.fields outdent o "end" end |
#read_func(t) ⇒ Object
221 222 223 |
# File 'lib/stark/ruby.rb', line 221 def read_func(t) ReadFunc[t] || raise("unknown type - #{t}") end |
#read_type(t, lhs, found_type = 'rtype') ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/stark/ruby.rb', line 229 def read_type(t, lhs, found_type = 'rtype') o "#{lhs} = expect ip, #{wire_type(t)}, #{found_type} do" indent if desc = @structs[t] o "read_#{desc.name}(ip)" elsif desc = @enums[t] o "Enum_#{desc.name}[ip.read_i32]" elsif t.kind_of? Stark::Parser::AST::Map o "expect_map ip, #{wire_type(t.key)}, #{wire_type(t.value)} do |kt,vt,size|" indent o "{}.tap do |_hash|" indent o "size.times do" indent read_type(t.key, "k", "kt") read_type(t.value, "v", "vt") o "_hash[k] = v" outdent o "end" outdent o "end" outdent o "end" elsif t.kind_of? Stark::Parser::AST::List o "expect_list ip, #{wire_type(t.value)} do |vt,size|" indent o "Array.new(size) do" indent read_type t.value, "_elem", "vt" outdent o "end" outdent o "end" elsif t.kind_of? Stark::Parser::AST::Set o "expect_set ip, #{wire_type(t.value)} do |vt,size|" indent o "_arr = Array.new(size) do" indent read_type t.value, "element", "vt" o "element" outdent o "end" o "::Set.new(_arr)" outdent o "end" else o "ip.#{read_func(t)}" end outdent o "end" end |
#run(ast) ⇒ Object
22 23 24 25 |
# File 'lib/stark/ruby.rb', line 22 def run(ast) ast.each { |a| a.accept self } close end |
#type(t) ⇒ Object
182 183 184 |
# File 'lib/stark/ruby.rb', line 182 def type(t) CoreTypes[t] || raise("unknown type - #{t}") end |
#wire_type(t) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/stark/ruby.rb', line 186 def wire_type(t) return "::Thrift::Types::STRUCT" if @structs[t] || @exceptions[t] return "::Thrift::Types::I32" if @enums[t] case t when Stark::Parser::AST::Map "::Thrift::Types::MAP" when Stark::Parser::AST::List "::Thrift::Types::LIST" when Stark::Parser::AST::Set "::Thrift::Types::SET" else type t end end |
#wire_type_and_ruby_type(t) ⇒ Object
202 203 204 205 206 |
# File 'lib/stark/ruby.rb', line 202 def wire_type_and_ruby_type(t) return "::Thrift::Types::STRUCT, #{t}" if @structs[t] || @exceptions[t] return "::Thrift::Types::I32, Enum_#{t}" if @enums[t] wire_type(t) end |
#write_client(serv) ⇒ Object
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/stark/ruby.rb', line 476 def write_client(serv) o "class Client < Stark::Client" indent o "include Protocol" serv.functions.each do |func| names = Array(func.arguments).map { |f| f.name }.join(", ") o o "def #{func.name}(#{names})" indent o "op = @oprot" o "op.write_message_begin '#{func.name}', ::Thrift::MessageTypes::CALL, 0" o "op.write_struct_begin \"#{func.name}_args\"" Array(func.arguments).each do |arg| o "#{arg.name} = value_for_write #{arg.name}, #{wire_type_and_ruby_type(arg.type)}" write_field arg.type, arg.name, arg.index end o "op.write_field_stop" o "op.write_struct_end" o "op.write_message_end" o "op.trans.flush" if func. == :oneway o "return" outdent o "end" next end o "ip = @iprot" o "_, mtype, _ = ip.read_message_begin" o "handle_exception mtype" o "ip.read_struct_begin" o "_, rtype, rid = ip.read_field_begin" if t = func.throws o "case rid" t.each do |ex| o "when #{ex.index}" o " _ex = read_#{ex.type}(ip)" o " ip.read_field_end" o " _, rtype, _ = ip.read_field_begin" o " fail if rtype != ::Thrift::Types::STOP" o " ip.read_struct_end" o " ip.read_message_end" o " raise _ex" end o "end" end o "result = nil" o "fail unless rid == 0" if func.return_type != "void" o "if rtype != ::Thrift::Types::STOP" indent read_type func.return_type, "result" o "ip.read_field_end" o "_, rtype, rid = ip.read_field_begin" outdent o "end" end o "fail if rtype != ::Thrift::Types::STOP" o "ip.read_struct_end" o "ip.read_message_end" o "return result" outdent o "end" end outdent o "end" end |
#write_field(ft, name, idx) ⇒ Object
319 320 321 322 323 |
# File 'lib/stark/ruby.rb', line 319 def write_field(ft, name, idx) o "op.write_field_begin '#{name}', #{wire_type(ft)}, #{idx}" write_type ft, name o "op.write_field_end" end |
#write_field_declarations(fields) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/stark/ruby.rb', line 88 def write_field_declarations(fields) max_field_len = fields.inject(0) {|max,f| f.name.length > max ? f.name.length : max } max_index_len = fields.inject(0) {|max,f| f.index.to_s.length > max ? f.index.to_s.length : max } current_index = 1 fields.each do |f| if f.index != current_index o "field_number #{f.index}" current_index = f.index end current_index += 1 if f.value o("attr_writer :%-*s # %*s: %s" % [max_field_len, f.name, max_index_len, f.index, object_type(f.type)]) o("def %s; @%s || %s; end" % [f.name, f.name, const_to_ruby(f.value)]) else o("attr_accessor :%-*s # %*s: %s" % [max_field_len, f.name, max_index_len, f.index, object_type(f.type)]) end end end |
#write_func(t) ⇒ Object
225 226 227 |
# File 'lib/stark/ruby.rb', line 225 def write_func(t) WriteFunc[t] || raise("unknown type - #{t}") end |
#write_processor(serv) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/stark/ruby.rb', line 325 def write_processor(serv) o "class Processor < Stark::Processor" indent o "include Protocol" serv.functions.each do |func| o o "def process_#{func.name}(seqid, ip, op)" indent o "ip.read_struct_begin" args = Array(func.arguments) o "fields = {}" o "while true" o " _, ftype, fid = ip.read_field_begin" o " break if ftype == ::Thrift::Types::STOP" if args.empty? o " ip.skip ftype" else o " case fid" args.each do |arg| o " when #{arg.index}" indent; indent read_type arg.type, "fields[#{arg.index}]", "ftype" outdent; outdent end o " else" o " ip.skip ftype" o " end" end o " ip.read_field_end" o "end" o "ip.read_struct_end" o "ip.read_message_end" o "args = #{args.map(&:index).inspect}.inject([]) {|arr,i| arr << fields[i] }" if t = func.throws o "begin" indent o "result = @handler.#{func.name}(*args)" outdent t.each do |ex| o "rescue #{ex.type} => ex#{ex.index}" indent o "op.write_message_begin '#{func.name}', ::Thrift::MessageTypes::REPLY, seqid" o "op.write_struct_begin '#{func.name}_result'" write_field ex.type, "ex#{ex.index}", ex.index o "op.write_field_stop" o "op.write_struct_end" o "op.write_message_end" o "op.trans.flush" o "return" outdent end o "end" else o "result = @handler.#{func.name}(*args)" end if func. == :oneway o "return result" outdent o "end" next end ft = func.return_type o "result = value_for_write(result, #{wire_type_and_ruby_type(ft)})" if ft != "void" o "op.write_message_begin '#{func.name}', ::Thrift::MessageTypes::REPLY, seqid" o "op.write_struct_begin '#{func.name}_result'" if ft != "void" o "if result" indent write_field ft, 'result', 0 outdent o "end" end o "op.write_field_stop" o "op.write_struct_end" o "op.write_message_end" o "op.trans.flush" o "return result" outdent o "end" end outdent o "end" end |
#write_protocol ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/stark/ruby.rb', line 422 def write_protocol o o "module Protocol" indent @structs.merge(@exceptions).each do |name, struct| o o "def read_#{name}(ip)" indent o "obj = #{name}.new" o "ip.read_struct_begin" o "while true" o " _, ftype, fid = ip.read_field_begin" o " break if ftype == ::Thrift::Types::STOP" o " case fid" struct.fields.each do |f| o " when #{f.index}" indent; indent read_type f.type, "obj.#{f.name}", 'ftype' outdent; outdent end o " else" o " ip.skip ftype" o " end" o " ip.read_field_end" o "end" o "ip.read_struct_end" o "obj" outdent o "end" o o "def write_#{name}(op, str)" indent o "op.write_struct_begin '#{name}'" struct.fields.each do |f| o "if #{f.name} = value_for_write(str.#{f.name}, #{wire_type_and_ruby_type(f.type)})" indent write_field f.type, f.name, f.index outdent o "end" end o "op.write_field_stop" o "op.write_struct_end" outdent o "end" end outdent o "end" end |
#write_type(ft, name) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/stark/ruby.rb', line 281 def write_type(ft, name) if desc = (@structs[ft] || @exceptions[ft]) o "write_#{desc.name} op, #{name}" elsif desc = @enums[ft] o "op.write_i32 Enum_#{desc.name}[#{name}.to_sym]" elsif ft.kind_of? Stark::Parser::AST::Map o "#{name} = hash_cast #{name}" o "op.write_map_begin(#{wire_type(ft.key)}, #{wire_type(ft.value)}, #{name}.size)" o "#{name}.each do |k,v|" indent write_type ft.key, "k" write_type ft.value, "v" outdent o "end" o "op.write_map_end" elsif ft.kind_of? Stark::Parser::AST::List o "#{name} = Array(#{name})" o "op.write_list_begin(#{wire_type(ft.value)}, #{name}.size)" o "#{name}.each do |v|" indent write_type ft.value, "v" outdent o "end" o "op.write_list_end" elsif ft.kind_of? Stark::Parser::AST::Set o "#{name} = Set.new(#{name})" o "op.write_list_begin(#{wire_type(ft.value)}, #{name}.size)" o "#{name}.each do |v|" indent write_type ft.value, "v" outdent o "end" o "op.write_set_end" elsif ft != "void" o "op.#{write_func(ft)} #{name}" end end |