Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checkpoint commit, can roundtrip simple structs
- Loading branch information
Showing
27 changed files
with
347 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ begin | |
RSpec::Core::RakeTask.new(:spec) | ||
task :default => :spec | ||
rescue LoadError | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,70 @@ | ||
```xdr | ||
|
||
enum LedgerType { | ||
ACCOUNT, | ||
TRUSTLINE, | ||
OFFER | ||
enum MyUnionType { | ||
TYPE_1, | ||
TYPE_2, | ||
TYPE_3, | ||
TYPE_4 | ||
}; | ||
|
||
union LedgerEntry switch (LedgerType type) | ||
union MyUnion switch (MyUnionType type) | ||
{ | ||
case ACCOUNT: | ||
AccountEntry account; | ||
case TYPE_1: | ||
hyper typeOneVal; | ||
|
||
case TRUSTLINE: | ||
TrustLineEntry trustLine; | ||
case TYPE_2: | ||
case TYPE_3: | ||
int typeTwoVal; | ||
|
||
case OFFER: | ||
OfferEntry offer; | ||
case TYPE_4: | ||
void; | ||
|
||
default: | ||
CustomType typeThreeVal; | ||
}; | ||
|
||
``` | ||
|
||
becomes: | ||
|
||
```ruby | ||
module LedgerType | ||
ACCOUNT = 0 | ||
TRUSTLINE = 1 | ||
OFFER = 2 | ||
module MyUnionType | ||
TYPE_1 = 0 | ||
TYPE_2 = 1 | ||
TYPE_3 = 2 | ||
TYPE_4 = 3 | ||
end | ||
|
||
class LedgerEntry | ||
include XDR::Union | ||
class MyUnion < XDR::Union | ||
|
||
switch_on :type, MyUnionType | ||
|
||
switch MyUnionType::TYPE_1, :type_one_val | ||
switch MyUnionType::TYPE_2, :type_two_val | ||
switch MyUnionType::TYPE_3, :type_two_val | ||
switch MyUnionType::TYPE_4 # void | ||
switch :type_three_val # default | ||
|
||
arm :account, LedgerType::ACCOUNT, AccountEntry | ||
arm :trust_line, LedgerType::TRUSTLINE, TrustLineEntry | ||
arm :offer, LedgerType::OFFER, OfferEntry | ||
attribute :type_one_val, XDR::Hyper | ||
attribute :type_two_val, XDR::Int | ||
attribute :type_three_val, CustomType | ||
end | ||
``` | ||
|
||
usage: | ||
|
||
```ruby | ||
LedgerEntry.new # => raise | ||
|
||
entry = LedgerEntry.new(:account, AccountEntry.new) | ||
entry.arm # => 0 | ||
entry.get # => #<AccountEntry:...> | ||
entry.account! # => #<AccountEntry:...> | ||
entry.trust_line! # => raise ArmNotSetError | ||
|
||
entry.set :trust_line, TrustLineEntry.new | ||
entry.arm # => 1 | ||
entry.get # => #<TrustLineEntry:...> | ||
entry.trust_line! # => #<TrustLineEntry:...> | ||
MyUnion.new # => raise | ||
|
||
val = MyUnion.new(:type_one_val, 100) | ||
val.arm # => 0 | ||
val.get # => 100 | ||
val.type_one_val! # => 100 | ||
val.type_two_val! # => raise ArmNotSetError | ||
|
||
val.set :default, CustomType.new | ||
val.arm # => :default | ||
val.get # => #<CustomType:...> | ||
val.type_three_val! # => #<CustomType:...> | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module XDR::Bool | ||
include XDR::Concerns::ConvertsToXDR | ||
|
||
def converter | ||
XDR::Primitives::BOOL | ||
end | ||
|
||
extend self | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module XDR::Concerns::ConvertsToXDR | ||
def to_xdr(val) | ||
xdr_serializer.to_xdr(val) | ||
end | ||
|
||
def from_xdr(io) | ||
io = io.is_a?(String) ? StringIO.new(io) : io | ||
|
||
xdr_serializer.from_xdr(io) | ||
end | ||
|
||
def xdr_serializer | ||
raise NotImplementedError, "implement in including class" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module XDR::DSL | ||
extend ActiveSupport::Autoload | ||
|
||
autoload :Struct | ||
autoload :Union | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module XDR::DSL::Struct | ||
def attribute(name, type) | ||
|
||
raise ArgumentError, "#{type} does not convert to xdr" unless type.is_a?(XDR::Concerns::ConvertsToXDR) | ||
|
||
self.fields = self.fields.merge(name => type) | ||
|
||
attr_accessor name | ||
define_attribute_methods name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module XDR::DSL::Union | ||
def switch_on(type) | ||
end | ||
|
||
def switch(discriminant, arm) | ||
puts "switch #{discriminant} => #{arm}" | ||
end | ||
|
||
def arm(name, type) | ||
puts "arm #{name} => #{type}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module XDR::Int | ||
extend XDR::Concerns::ConvertsToXDR | ||
|
||
def self.xdr_serializer | ||
XDR::Primitives::INT32 | ||
end | ||
|
||
extend self | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class XDR::Opaque | ||
def self.[] (length) | ||
new(length) | ||
end | ||
|
||
def initialize(length) | ||
@length = length | ||
end | ||
|
||
def valid?(input) | ||
input.is_a?(String) && input.length == @length | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class XDR::Option | ||
def self.[] (child_type) | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class XDR::String | ||
include XDR::Concerns::ConvertsToXDR | ||
|
||
def self.[] (max_length) | ||
new(max_length) | ||
end | ||
|
||
def initialize(max_length) | ||
@max_length = max_length | ||
end | ||
|
||
def xdr_serializer | ||
XDR::Primitives::String.new(@max_length) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class XDR::Struct | ||
include ActiveModel::Model | ||
include ActiveModel::AttributeMethods | ||
|
||
extend XDR::Concerns::ConvertsToXDR | ||
extend XDR::DSL::Struct | ||
|
||
class_attribute :fields | ||
self.fields = ActiveSupport::OrderedHash.new | ||
|
||
validates_with XDR::StructValidator | ||
|
||
def self.xdr_serializer | ||
# TODO: raise if `struct` aint an `XDR::Struct` | ||
return @xdr_serializer if defined? @xdr_serializer | ||
|
||
field_converters = self.fields.values.map(&:xdr_serializer) | ||
|
||
@xdr_serializer = XDR::Primitives::Struct.new(*field_converters) | ||
end | ||
|
||
def to_xdr | ||
self.class.to_xdr(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class XDR::StructValidator < ActiveModel::Validator | ||
def validate(struct) | ||
# validate each field | ||
# TODO | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class XDR::Union | ||
include ActiveModel::Model | ||
include ActiveModel::AttributeMethods | ||
extend XDR::DSL::Union | ||
|
||
validates_with XDR::UnionValidator | ||
end |
Oops, something went wrong.