capn_proto 0.0.1.alpha.1

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.
Files changed (57) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +19 -0
  4. data/README.md +42 -0
  5. data/Rakefile +25 -0
  6. data/capn_proto.gemspec +33 -0
  7. data/examples/addressbook.bin +0 -0
  8. data/examples/addressbook.capnp +31 -0
  9. data/examples/create_test_data.py +39 -0
  10. data/examples/example.rb +38 -0
  11. data/ext/capn_proto/.ycm_extra_conf.py +65 -0
  12. data/ext/capn_proto/.ycm_extra_conf.pyc +0 -0
  13. data/ext/capn_proto/class_builder.cc +80 -0
  14. data/ext/capn_proto/class_builder.h +72 -0
  15. data/ext/capn_proto/dynamic_list_reader.cc +52 -0
  16. data/ext/capn_proto/dynamic_list_reader.h +23 -0
  17. data/ext/capn_proto/dynamic_struct_reader.cc +59 -0
  18. data/ext/capn_proto/dynamic_struct_reader.h +23 -0
  19. data/ext/capn_proto/dynamic_value_reader.cc +50 -0
  20. data/ext/capn_proto/dynamic_value_reader.h +13 -0
  21. data/ext/capn_proto/exception.cc +24 -0
  22. data/ext/capn_proto/exception.h +18 -0
  23. data/ext/capn_proto/extconf.rb +29 -0
  24. data/ext/capn_proto/field_list.cc +51 -0
  25. data/ext/capn_proto/field_list.h +23 -0
  26. data/ext/capn_proto/init.cc +34 -0
  27. data/ext/capn_proto/list_nested_node_reader.cc +50 -0
  28. data/ext/capn_proto/list_nested_node_reader.h +24 -0
  29. data/ext/capn_proto/message_reader.cc +30 -0
  30. data/ext/capn_proto/message_reader.h +17 -0
  31. data/ext/capn_proto/nested_node_reader.cc +42 -0
  32. data/ext/capn_proto/nested_node_reader.h +21 -0
  33. data/ext/capn_proto/parsed_schema.cc +58 -0
  34. data/ext/capn_proto/parsed_schema.h +23 -0
  35. data/ext/capn_proto/rb.cc +0 -0
  36. data/ext/capn_proto/rb.h +0 -0
  37. data/ext/capn_proto/ruby_capn_proto.cc +4 -0
  38. data/ext/capn_proto/ruby_capn_proto.h +38 -0
  39. data/ext/capn_proto/schema_node_reader.cc +51 -0
  40. data/ext/capn_proto/schema_node_reader.h +22 -0
  41. data/ext/capn_proto/schema_parser.cc +53 -0
  42. data/ext/capn_proto/schema_parser.h +20 -0
  43. data/ext/capn_proto/stream_fd_message_reader.cc +53 -0
  44. data/ext/capn_proto/stream_fd_message_reader.h +21 -0
  45. data/ext/capn_proto/struct_schema.cc +57 -0
  46. data/ext/capn_proto/struct_schema.h +23 -0
  47. data/ext/capn_proto/util.cc +25 -0
  48. data/ext/capn_proto/util.h +16 -0
  49. data/lib/capn_proto/version.rb +3 -0
  50. data/lib/capn_proto.rb +88 -0
  51. data/media/captain_proto.png +0 -0
  52. data/media/captain_proto_small.png +0 -0
  53. data/spec/addressbook.bin +0 -0
  54. data/spec/addressbook.capnp +31 -0
  55. data/spec/capn_proto_spec.rb +7 -0
  56. data/spec/create_test_data.py +38 -0
  57. metadata +188 -0
@@ -0,0 +1,53 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "message_reader.h"
3
+ #include "stream_fd_message_reader.h"
4
+ #include "struct_schema.h"
5
+ #include "dynamic_struct_reader.h"
6
+ #include "class_builder.h"
7
+
8
+ namespace ruby_capn_proto {
9
+ using WrappedType = capnp::StreamFdMessageReader;
10
+ VALUE StreamFdMessageReader::Class;
11
+
12
+ void StreamFdMessageReader::Init() {
13
+ ClassBuilder("StreamFdMessageReader", MessageReader::Class).
14
+ defineAlloc(&alloc).
15
+ defineMethod("initialize", &initialize).
16
+ defineMethod("get_root", &get_root).
17
+ store(&Class);
18
+ }
19
+
20
+ VALUE StreamFdMessageReader::alloc(VALUE klass) {
21
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
22
+ }
23
+
24
+ VALUE StreamFdMessageReader::initialize(VALUE self, VALUE rb_io) {
25
+ VALUE rb_fileno = rb_funcall(rb_io, rb_intern("fileno"), 0);
26
+ auto fileno = FIX2INT(rb_fileno);
27
+ WrappedType* p = unwrap(self);
28
+ new (p) WrappedType(fileno);
29
+
30
+ return Qnil;
31
+ }
32
+
33
+ void StreamFdMessageReader::free(WrappedType* p) {
34
+ p->~StreamFdMessageReader();
35
+ ruby_xfree(p);
36
+ }
37
+
38
+ WrappedType* StreamFdMessageReader::unwrap(VALUE self) {
39
+ WrappedType* p;
40
+ Data_Get_Struct(self, WrappedType, p);
41
+ return p;
42
+ }
43
+
44
+ VALUE StreamFdMessageReader::get_root(VALUE self, VALUE rb_schema) {
45
+ if (rb_respond_to(rb_schema, rb_intern("schema"))) {
46
+ rb_schema = rb_funcall(rb_schema, rb_intern("schema"), 0);
47
+ }
48
+
49
+ auto schema = *StructSchema::unwrap(rb_schema);
50
+ auto reader = unwrap(self)->getRoot<capnp::DynamicStruct>(schema);
51
+ return DynamicStructReader::create(reader);
52
+ }
53
+ }
@@ -0,0 +1,21 @@
1
+ #ifndef STREAM_FD_MESSAGE_READER_H
2
+ #define STREAM_FD_MESSAGE_READER_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class StreamFdMessageReader {
8
+ public:
9
+ using WrappedType = capnp::StreamFdMessageReader;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE initialize(VALUE klass, VALUE io);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE get_root(VALUE name, VALUE schema);
16
+
17
+ static VALUE Class;
18
+ };
19
+ }
20
+
21
+ #endif /* STREAM_FD_MESSAGE_READER_H */
@@ -0,0 +1,57 @@
1
+ #include "ruby_capn_proto.h"
2
+ #include "struct_schema.h"
3
+ #include "field_list.h"
4
+ #include "class_builder.h"
5
+ #include "util.h"
6
+
7
+ namespace ruby_capn_proto {
8
+ using WrappedType = capnp::StructSchema;
9
+ VALUE StructSchema::Class;
10
+
11
+ void StructSchema::Init() {
12
+ ClassBuilder("StructSchema", rb_cObject).
13
+ defineAlloc(&alloc).
14
+ defineMethod("field_names", &field_names).
15
+ defineMethod("fields", &fields).
16
+ store(&Class);
17
+ }
18
+
19
+ void StructSchema::free(WrappedType* p) {
20
+ p->~StructSchema();
21
+ ruby_xfree(p);
22
+ }
23
+
24
+ VALUE StructSchema::alloc(VALUE klass) {
25
+ return Data_Wrap_Struct(klass, NULL, free, ruby_xmalloc(sizeof(WrappedType)));
26
+ }
27
+
28
+ WrappedType* StructSchema::unwrap(VALUE self) {
29
+ WrappedType* p;
30
+ Data_Get_Struct(self, WrappedType, p);
31
+ return p;
32
+ }
33
+
34
+ VALUE StructSchema::create(WrappedType schema) {
35
+ VALUE rb_obj = alloc(Class);
36
+ WrappedType* wrapped_schema = unwrap(rb_obj);
37
+ *wrapped_schema = kj::mv(schema);
38
+
39
+ return rb_obj;
40
+ }
41
+
42
+ VALUE StructSchema::field_names(VALUE self) {
43
+ auto field_list = unwrap(self)->getFields();
44
+ auto size = field_list.size();
45
+ auto rb_array = rb_ary_new2(size);
46
+ for (int i=0; i<size; i++) {
47
+ auto name = field_list[i].getProto().getName();
48
+ rb_ary_push(rb_array, rb_str_new(name.begin(), name.size()));
49
+ }
50
+
51
+ return rb_array;
52
+ }
53
+
54
+ VALUE StructSchema::fields(VALUE self) {
55
+ return FieldList::create(unwrap(self)->getFields());
56
+ }
57
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef STRUCT_SCHEMA_H
2
+ #define STRUCT_SCHEMA_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class StructSchema {
8
+ public:
9
+ using WrappedType = capnp::StructSchema;
10
+ static void Init();
11
+ static VALUE alloc(VALUE klass);
12
+ static VALUE create(WrappedType schema);
13
+ static void free(WrappedType* p);
14
+ static WrappedType* unwrap(VALUE self);
15
+ static VALUE field_names(VALUE self);
16
+ static VALUE fields(VALUE self);
17
+
18
+ static VALUE Class;
19
+ };
20
+ }
21
+
22
+
23
+ #endif /* STRUCT_SCHEMA_H */
@@ -0,0 +1,25 @@
1
+ #include "util.h"
2
+
3
+ namespace ruby_capn_proto {
4
+ kj::Array<kj::String> Util::toStringArray(VALUE rb_array) {
5
+ auto len = RARRAY_LEN(rb_array);
6
+ auto array = kj::heapArray<kj::String>(len);
7
+ for (int i=0; i < len; i++) {
8
+ auto rb_str = rb_ary_entry(rb_array, i);
9
+ array[i] = toString(rb_str);
10
+ }
11
+
12
+ return array;
13
+ }
14
+
15
+ kj::String Util::toString(VALUE rb_string) {
16
+ auto rb_val = StringValue(rb_string);
17
+ auto str = RSTRING_PTR(rb_val);
18
+ auto len = RSTRING_LEN(rb_val);
19
+ return kj::heapString(str, len);
20
+ }
21
+
22
+ VALUE Util::toRubyString(kj::StringPtr string) {
23
+ return rb_str_new(string.begin(), string.size());
24
+ }
25
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef UTIL_H
2
+ #define UTIL_H
3
+
4
+ #include "ruby_capn_proto.h"
5
+
6
+ namespace ruby_capn_proto {
7
+ class Util {
8
+ public:
9
+ static kj::Array<kj::String> toStringArray(VALUE array_of_strings);
10
+ static kj::String toString(VALUE string);
11
+
12
+ static VALUE toRubyString(kj::StringPtr string);
13
+ };
14
+ }
15
+
16
+ #endif /* UTIL_H */
@@ -0,0 +1,3 @@
1
+ module CapnProto
2
+ VERSION = "0.0.1.alpha.1"
3
+ end
data/lib/capn_proto.rb ADDED
@@ -0,0 +1,88 @@
1
+ require 'capn_proto/init'
2
+
3
+ module CapnProto
4
+ ListNestedNodeReader.class_eval do
5
+ include Enumerable
6
+ def each
7
+ (0...size).each do |n|
8
+ yield self[n]
9
+ end
10
+ end
11
+ end
12
+
13
+ DynamicListReader.class_eval do
14
+ include Enumerable
15
+ def each
16
+ (0...size).each do |n|
17
+ yield self[n]
18
+ end
19
+ end
20
+ end
21
+
22
+ DynamicStructReader.class_eval do
23
+ def method_missing(name, *args, &block)
24
+ self[name.to_s, *args, &block]
25
+ end
26
+
27
+ def each
28
+ (0...size).each do |n|
29
+ yield self[n]
30
+ end
31
+ end
32
+ end
33
+
34
+ module SchemaLoader
35
+ def schema_parser
36
+ @schema_parser
37
+ end
38
+
39
+ def load_schema(file_name, imports=[])
40
+ display_name = self.name
41
+
42
+ @schema_parser ||= CapnProto::SchemaParser.new
43
+
44
+ schema = @schema_parser.parse_disk_file(
45
+ display_name,
46
+ file_name,
47
+ imports);
48
+
49
+ _load_schema_rec(schema, self)
50
+ end
51
+
52
+ def _load_schema_rec(schema, mod)
53
+ node = schema.get_proto
54
+ nested_nodes = node.nested_nodes
55
+
56
+ if node.struct?
57
+ struct_schema = schema.as_struct
58
+ mod.instance_variable_set(:@schema, struct_schema)
59
+ mod.extend(Struct)
60
+ end
61
+
62
+ nested_nodes.each do |nested_node|
63
+ nested_mod = mod.const_set(nested_node.name, Module.new)
64
+ nested_schema = schema.get_nested(nested_node.name)
65
+ _load_schema_rec(nested_schema, nested_mod)
66
+ end
67
+ end
68
+
69
+ module Struct
70
+ def schema
71
+ @schema
72
+ end
73
+
74
+ def read_from(io)
75
+ reader = StreamFdMessageReader.new(io)
76
+ reader.get_root(self)
77
+ end
78
+
79
+ def read_packed_from(io)
80
+ raise 'not implemented'
81
+ end
82
+
83
+ def new_message(file)
84
+ raise 'not implemented'
85
+ end
86
+ end
87
+ end
88
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,31 @@
1
+ @0x9eb32e19f86ee174;
2
+
3
+ struct Person {
4
+ id @0 :UInt32;
5
+ name @1 :Text;
6
+ email @2 :Text;
7
+ phones @3 :List(PhoneNumber);
8
+
9
+ struct PhoneNumber {
10
+ number @0 :Text;
11
+ type @1 :Type;
12
+
13
+ enum Type {
14
+ mobile @0;
15
+ home @1;
16
+ work @2;
17
+ }
18
+ }
19
+
20
+ employment :union {
21
+ unemployed @4 :Void;
22
+ employer @5 :Text;
23
+ school @6 :Text;
24
+ selfEmployed @7 :Void;
25
+ # We assume that a person is only one of these.
26
+ }
27
+ }
28
+
29
+ struct AddressBook {
30
+ people @0 :List(Person);
31
+ }
@@ -0,0 +1,7 @@
1
+ require 'capn_proto'
2
+
3
+ ADDRESSBOOK_SCHEMA = File.expand_path("../addressbook.capnp", __FILE__)
4
+
5
+ module AddressBook extend CapnProto::SchemaLoader
6
+ load_schema(ADDRESSBOOK_SCHEMA)
7
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env python
2
+ from __future__ import print_function
3
+ import os
4
+ import capnp
5
+
6
+ this_dir = os.path.dirname(__file__)
7
+ addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
8
+
9
+ def writeAddressBook(file):
10
+ addresses = addressbook.AddressBook.newMessage()
11
+ people = addresses.init('people', 2)
12
+
13
+ alice = people[0]
14
+ alice.id = 123
15
+ alice.name = 'Alice'
16
+ alice.email = 'alice@example.com'
17
+ alicePhones = alice.init('phones', 1)
18
+ alicePhones[0].number = "555-1212"
19
+ alicePhones[0].type = 'mobile'
20
+ alice.employment.school = "MIT"
21
+
22
+ bob = people[1]
23
+ bob.id = 456
24
+ bob.name = 'Bob'
25
+ bob.email = 'bob@example.com'
26
+ bobPhones = bob.init('phones', 2)
27
+ bobPhones[0].number = "555-4567"
28
+ bobPhones[0].type = 'home'
29
+ bobPhones[1].number = "555-7654"
30
+ bobPhones[1].type = 'work'
31
+ bob.employment.unemployed = None
32
+
33
+ addresses.writeTo(file)
34
+
35
+
36
+ if __name__ == '__main__':
37
+ f = open('addressbook.bin', 'w')
38
+ writeAddressBook(f)
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capn_proto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha.1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Charles Strahan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.14.1
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.14.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake-compiler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.7.6
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.6
62
+ - !ruby/object:Gem::Dependency
63
+ name: awesome_print
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: interactive_editor
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! 'This gem wraps the official C++ implementation of Cap''n Proto (libcapnp).
95
+ From the Cap''n Proto documentation: "Cap''n Proto is an insanely fast data interchange
96
+ format and capability-based RPC system. Think JSON, except binary. Or think Protocol
97
+ Buffers, except faster."'
98
+ email:
99
+ - charles.c.strahan@gmail.com
100
+ executables: []
101
+ extensions:
102
+ - ext/capn_proto/extconf.rb
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - capn_proto.gemspec
111
+ - examples/addressbook.bin
112
+ - examples/addressbook.capnp
113
+ - examples/create_test_data.py
114
+ - examples/example.rb
115
+ - ext/capn_proto/.ycm_extra_conf.py
116
+ - ext/capn_proto/.ycm_extra_conf.pyc
117
+ - ext/capn_proto/class_builder.cc
118
+ - ext/capn_proto/class_builder.h
119
+ - ext/capn_proto/dynamic_list_reader.cc
120
+ - ext/capn_proto/dynamic_list_reader.h
121
+ - ext/capn_proto/dynamic_struct_reader.cc
122
+ - ext/capn_proto/dynamic_struct_reader.h
123
+ - ext/capn_proto/dynamic_value_reader.cc
124
+ - ext/capn_proto/dynamic_value_reader.h
125
+ - ext/capn_proto/exception.cc
126
+ - ext/capn_proto/exception.h
127
+ - ext/capn_proto/extconf.rb
128
+ - ext/capn_proto/field_list.cc
129
+ - ext/capn_proto/field_list.h
130
+ - ext/capn_proto/init.cc
131
+ - ext/capn_proto/list_nested_node_reader.cc
132
+ - ext/capn_proto/list_nested_node_reader.h
133
+ - ext/capn_proto/message_reader.cc
134
+ - ext/capn_proto/message_reader.h
135
+ - ext/capn_proto/nested_node_reader.cc
136
+ - ext/capn_proto/nested_node_reader.h
137
+ - ext/capn_proto/parsed_schema.cc
138
+ - ext/capn_proto/parsed_schema.h
139
+ - ext/capn_proto/rb.cc
140
+ - ext/capn_proto/rb.h
141
+ - ext/capn_proto/ruby_capn_proto.cc
142
+ - ext/capn_proto/ruby_capn_proto.h
143
+ - ext/capn_proto/schema_node_reader.cc
144
+ - ext/capn_proto/schema_node_reader.h
145
+ - ext/capn_proto/schema_parser.cc
146
+ - ext/capn_proto/schema_parser.h
147
+ - ext/capn_proto/stream_fd_message_reader.cc
148
+ - ext/capn_proto/stream_fd_message_reader.h
149
+ - ext/capn_proto/struct_schema.cc
150
+ - ext/capn_proto/struct_schema.h
151
+ - ext/capn_proto/util.cc
152
+ - ext/capn_proto/util.h
153
+ - lib/capn_proto.rb
154
+ - lib/capn_proto/version.rb
155
+ - media/captain_proto.png
156
+ - media/captain_proto_small.png
157
+ - spec/addressbook.bin
158
+ - spec/addressbook.capnp
159
+ - spec/capn_proto_spec.rb
160
+ - spec/create_test_data.py
161
+ homepage: https://github.com/cstrahan/capnp-ruby
162
+ licenses: []
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ none: false
169
+ requirements:
170
+ - - ! '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ segments:
174
+ - 0
175
+ hash: -2889828387253718151
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>'
180
+ - !ruby/object:Gem::Version
181
+ version: 1.3.1
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 1.8.23
185
+ signing_key:
186
+ specification_version: 3
187
+ summary: Cap'n Proto (libcapnp) bindings for Ruby.
188
+ test_files: []