diff --git a/README.md b/README.md index 8901ec72e..1cd3266b8 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,10 @@ are not maintained, not tested, and are preserved for legacy usage. - ruby: complete support - javascript: complete support -- java: complete support - golang: currently using a fork of go-xdr, but has complete support - elixir: support is experimental as the SDK is in early development. Generated code requires [:exdr](https://github.com/revelrylabs/exdr) in your deps - C#: complete support -- Python: complete support ## Contributing new generators / languages diff --git a/lib/xdrgen/generators.rb b/lib/xdrgen/generators.rb index dacfa7f3c..53ef67c24 100644 --- a/lib/xdrgen/generators.rb +++ b/lib/xdrgen/generators.rb @@ -5,10 +5,8 @@ module Xdrgen::Generators autoload :Ruby autoload :Go autoload :Javascript - autoload :Java autoload :Elixir autoload :Csharp - autoload :Python def self.for_language(language) const_get language.to_s.classify diff --git a/lib/xdrgen/generators/java.rb b/lib/xdrgen/generators/java.rb deleted file mode 100644 index 40ecd0cec..000000000 --- a/lib/xdrgen/generators/java.rb +++ /dev/null @@ -1,657 +0,0 @@ -require 'set' - -module Xdrgen - module Generators - class Java < Xdrgen::Generators::Base - - def generate - constants_container = Set[] - render_lib - render_definitions(@top, constants_container) - render_constants constants_container - end - - def render_lib - template = IO.read(__dir__ + "/java/XdrDataInputStream.erb") - result = ERB.new(template).result binding - @output.write "XdrDataInputStream.java", result - - template = IO.read(__dir__ + "/java/XdrDataOutputStream.erb") - result = ERB.new(template).result binding - @output.write "XdrDataOutputStream.java", result - - template = IO.read(__dir__ + "/java/XdrElement.erb") - result = ERB.new(template).result binding - @output.write "XdrElement.java", result - - template = IO.read(__dir__ + "/java/XdrString.erb") - result = ERB.new(template).result binding - @output.write "XdrString.java", result - - template = IO.read(__dir__ + "/java/XdrUnsignedHyperInteger.erb") - result = ERB.new(template).result binding - @output.write "XdrUnsignedHyperInteger.java", result - - template = IO.read(__dir__ + "/java/XdrUnsignedInteger.erb") - result = ERB.new(template).result binding - @output.write "XdrUnsignedInteger.java", result - end - - def render_definitions(node, constants_container) - node.namespaces.each{|n| render_definitions n, constants_container } - node.definitions.each { |defn| render_definition(defn, constants_container) } - end - - def add_imports_for_definition(defn, imports) - imports.add("org.stellar.sdk.Base64Factory") - imports.add("java.io.ByteArrayInputStream") - imports.add("java.io.ByteArrayOutputStream") - - case defn - when AST::Definitions::Struct, AST::Definitions::Union - imports.add("lombok.Data") - imports.add("lombok.NoArgsConstructor") - imports.add("lombok.AllArgsConstructor") - imports.add("lombok.Builder") - when AST::Definitions::Typedef - imports.add("lombok.Data") - imports.add("lombok.NoArgsConstructor") - imports.add("lombok.AllArgsConstructor") - end - - if defn.respond_to? :nested_definitions - defn.nested_definitions.each{ |child_defn| add_imports_for_definition(child_defn, imports) } - end - end - - def render_definition(defn, constants_container) - imports = Set[] - add_imports_for_definition(defn, imports) - - case defn - when AST::Definitions::Struct ; - render_element defn, imports, defn do |out| - render_struct defn, out - render_nested_definitions defn, out - end - when AST::Definitions::Enum ; - render_element defn, imports, defn do |out| - render_enum defn, out - end - when AST::Definitions::Union ; - render_element defn, imports, defn do |out| - render_union defn, out - render_nested_definitions defn, out - end - when AST::Definitions::Typedef ; - render_element defn, imports, defn do |out| - render_typedef defn, out - end - when AST::Definitions::Const ; - const_name = defn.name - const_value = defn.value - constants_container.add([const_name, const_value]) - end - end - - def render_nested_definitions(defn, out, post_name="implements XdrElement") - return unless defn.respond_to? :nested_definitions - defn.nested_definitions.each{|ndefn| - render_source_comment out, ndefn - case ndefn - when AST::Definitions::Struct ; - name = name ndefn - out.puts "@Data" - out.puts "@NoArgsConstructor" - out.puts "@AllArgsConstructor" - out.puts "@Builder(toBuilder = true)" - out.puts "public static class #{name} #{post_name} {" - out.indent do - render_struct ndefn, out - render_nested_definitions ndefn , out - end - out.puts "}" - when AST::Definitions::Enum ; - name = name ndefn - out.puts "public static enum #{name} #{post_name} {" - out.indent do - render_enum ndefn, out - end - out.puts "}" - when AST::Definitions::Union ; - name = name ndefn - out.puts "@Data" - out.puts "@NoArgsConstructor" - out.puts "@AllArgsConstructor" - out.puts "@Builder(toBuilder = true)" - out.puts "public static class #{name} #{post_name} {" - out.indent do - render_union ndefn, out - render_nested_definitions ndefn, out - end - out.puts "}" - when AST::Definitions::Typedef ; - name = name ndefn - out.puts "@Data" - out.puts "@NoArgsConstructor" - out.puts "@AllArgsConstructor" - out.puts "public static class #{name} #{post_name} {" - out.indent do - render_typedef ndefn, out - end - out.puts "}" - end - } - end - - def render_element(defn, imports, element, post_name="implements XdrElement") - path = element.name.camelize + ".java" - name = name_string element.name - out = @output.open(path) - render_top_matter out - imports.each do |import| - out.puts "import #{import};" - end - out.puts "\n" - render_source_comment out, element - case defn - when AST::Definitions::Struct, AST::Definitions::Union - out.puts "@Data" - out.puts "@NoArgsConstructor" - out.puts "@AllArgsConstructor" - out.puts "@Builder(toBuilder = true)" - out.puts "public class #{name} #{post_name} {" - when AST::Definitions::Enum - out.puts "public enum #{name} #{post_name} {" - when AST::Definitions::Typedef - out.puts "@Data" - out.puts "@NoArgsConstructor" - out.puts "@AllArgsConstructor" - out.puts "public class #{name} #{post_name} {" - end - out.indent do - yield out - out.unbreak - end - out.puts "}" - end - - def render_constants(constants_container) - out = @output.open("Constants.java") - render_top_matter out - out.puts "public final class Constants {" - out.indent do - out.puts "private Constants() {}" - constants_container.each do |const_name, const_value| - out.puts "public static final int #{const_name} = #{const_value};" - end - end - out.puts "}" - end - - def render_enum(enum, out) - out.balance_after /,[\s]*/ do - enum.members.each_with_index do |em, index| - out.puts "#{em.name}(#{em.value})#{index == enum.members.size - 1 ? ';' : ','}" - end - end - out.break - out.puts <<-EOS.strip_heredoc - private final int value; - - #{name_string enum.name}(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static #{name_string enum.name} decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - EOS - out.indent 2 do - enum.members.each do |em| - out.puts "case #{em.value}: return #{em.name};" - end - end - out.puts <<-EOS.strip_heredoc - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - EOS - render_base64((name_string enum.name), out) - out.break - end - - def render_struct(struct, out) - struct.members.each do |m| - out.puts "private #{decl_string(m.declaration)} #{m.name};" - end - - out.puts "public void encode(XdrDataOutputStream stream) throws IOException{" - struct.members.each do |m| - out.indent do - encode_member m, out - end - end - out.puts "}" - - out.puts <<-EOS.strip_heredoc - public static #{name struct} decode(XdrDataInputStream stream) throws IOException { - #{name struct} decoded#{name struct} = new #{name struct}(); - EOS - struct.members.each do |m| - out.indent do - decode_member "decoded#{name struct}", m, out - end - end - out.indent do - out.puts "return decoded#{name struct};" - end - out.puts "}" - - render_base64((name struct), out) - out.break - end - - def render_typedef(typedef, out) - out.puts "private #{decl_string typedef.declaration} #{typedef.name};" - out.puts "public void encode(XdrDataOutputStream stream) throws IOException {" - out.indent do - encode_member typedef, out - end - out.puts "}" - out.break - - out.puts <<-EOS.strip_heredoc - public static #{name typedef} decode(XdrDataInputStream stream) throws IOException { - #{name typedef} decoded#{name typedef} = new #{name typedef}(); - EOS - out.indent do - decode_member "decoded#{name typedef}", typedef, out - out.puts "return decoded#{name typedef};" - end - out.puts "}" - out.break - render_base64(typedef.name.camelize, out) - end - - def render_union(union, out) - out.puts "private #{type_string union.discriminant.type} discriminant;" - union.arms.each do |arm| - next if arm.void? - out.puts "private #{decl_string(arm.declaration)} #{arm.name};" - end - out.break - - out.puts "public void encode(XdrDataOutputStream stream) throws IOException {" - if union.discriminant.type.is_a?(AST::Typespecs::Int) - out.puts "stream.writeInt(discriminant);" - elsif type_string(union.discriminant.type) == "Uint32" - # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core - out.puts "stream.writeInt(discriminant.getUint32().getNumber().intValue());" - else - out.puts "stream.writeInt(discriminant.getValue());" - end - if type_string(union.discriminant.type) == "Uint32" - # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core - out.puts "switch (discriminant.getUint32().getNumber().intValue()) {" - else - out.puts "switch (discriminant) {" - end - union.arms.each do |arm| - case arm - when AST::Definitions::UnionDefaultArm ; - out.puts "default:" - else - arm.cases.each do |kase| - if kase.value.is_a?(AST::Identifier) - if type_string(union.discriminant.type) == "Integer" - member = union.resolved_case(kase) - out.puts "case #{member.value}:" - else - out.puts "case #{kase.value.name}:" - end - else - out.puts "case #{kase.value.value}:" - end - end - end - encode_member arm, out - out.puts "break;" - end - out.puts "}\n}" - - out.puts "public static #{name union} decode(XdrDataInputStream stream) throws IOException {" - out.puts "#{name union} decoded#{name union} = new #{name union}();" - if union.discriminant.type.is_a?(AST::Typespecs::Int) - out.puts "Integer discriminant = stream.readInt();" - else - out.puts "#{name union.discriminant.type} discriminant = #{name union.discriminant.type}.decode(stream);" - end - out.puts "decoded#{name union}.setDiscriminant(discriminant);" - - if type_string(union.discriminant.type) == "Uint32" - # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core - out.puts "switch (decoded#{name union}.getDiscriminant().getUint32().getNumber().intValue()) {" - else - out.puts "switch (decoded#{name union}.getDiscriminant()) {" - end - - union.arms.each do |arm| - case arm - when AST::Definitions::UnionDefaultArm ; - out.puts "default:" - else - arm.cases.each do |kase| - if kase.value.is_a?(AST::Identifier) - if type_string(union.discriminant.type) == "Integer" - member = union.resolved_case(kase) - out.puts "case #{member.value}:" - else - out.puts "case #{kase.value.name}:" - end - else - out.puts "case #{kase.value.value}:" - end - end - end - decode_member "decoded#{name union}", arm, out - out.puts "break;" - end - out.puts "}\n" - out.indent do - out.puts "return decoded#{name union};" - end - out.puts "}" - render_base64((name union), out) - out.break - end - - def render_top_matter(out) - out.puts <<-EOS.strip_heredoc - // Automatically generated by xdrgen - // DO NOT EDIT or your changes may be overwritten - - package #{@namespace}; - - import java.io.IOException; - EOS - out.break - end - - def render_source_comment(out, defn) - return if defn.is_a?(AST::Definitions::Namespace) - - out.puts "/**" - out.puts " * #{name defn}'s original definition in the XDR file is:" - out.puts " *
"
-        out.puts " * " + escape_html(defn.text_value).split("\n").join("\n * ")
-        out.puts " * 
" - out.puts " */" - end - - def render_base64(return_type, out) - out.puts <<-EOS.strip_heredoc - public static #{return_type} fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static #{return_type} fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - EOS - end - - def encode_member(member, out) - case member.declaration - when AST::Declarations::Void - return - end - - if member.type.sub_type == :optional - out.puts "if (#{member.name} != null) {" - out.puts "stream.writeInt(1);" - end - case member.declaration - when AST::Declarations::Opaque ; - out.puts "int #{member.name}Size = #{member.name}.length;" - unless member.declaration.fixed? - out.puts "stream.writeInt(#{member.name}Size);" - end - out.puts <<-EOS.strip_heredoc - stream.write(get#{member.name.slice(0,1).capitalize+member.name.slice(1..-1)}(), 0, #{member.name}Size); - EOS - when AST::Declarations::Array ; - out.puts "int #{member.name}Size = get#{member.name.slice(0,1).capitalize+member.name.slice(1..-1)}().length;" - unless member.declaration.fixed? - out.puts "stream.writeInt(#{member.name}Size);" - end - out.puts <<-EOS.strip_heredoc - for (int i = 0; i < #{member.name}Size; i++) { - #{encode_type member.declaration.type, "#{member.name}[i]"}; - } - EOS - else - out.puts "#{encode_type member.declaration.type, "#{member.name}"};" - end - if member.type.sub_type == :optional - out.puts "} else {" - out.puts "stream.writeInt(0);" - out.puts "}" - end - end - - def encode_type(type, value) - case type - when AST::Typespecs::Int ; - "stream.writeInt(#{value})" - when AST::Typespecs::UnsignedInt ; - "#{value}.encode(stream)" - when AST::Typespecs::Hyper ; - "stream.writeLong(#{value})" - when AST::Typespecs::UnsignedHyper ; - "#{value}.encode(stream)" - when AST::Typespecs::Float ; - "stream.writeFloat(#{value})" - when AST::Typespecs::Double ; - "stream.writeDouble(#{value})" - when AST::Typespecs::Quadruple ; - raise "cannot render quadruple in golang" - when AST::Typespecs::Bool ; - "stream.writeInt(#{value} ? 1 : 0)" - when AST::Typespecs::String ; - "#{value}.encode(stream)" - when AST::Typespecs::Simple ; - "#{value}.encode(stream)" - when AST::Concerns::NestedDefinition ; - "#{value}.encode(stream)" - else - raise "Unknown typespec: #{type.class.name}" - end - end - - def decode_member(value, member, out) - case member.declaration - when AST::Declarations::Void ; - return - end - if member.type.sub_type == :optional - out.puts <<-EOS.strip_heredoc - int #{member.name}Present = stream.readInt(); - if (#{member.name}Present != 0) { - EOS - end - case member.declaration - when AST::Declarations::Opaque ; - if (member.declaration.fixed?) - out.puts "int #{member.name}Size = #{convert_constant member.declaration.size};" - else - out.puts "int #{member.name}Size = stream.readInt();" - end - out.puts <<-EOS.strip_heredoc - #{value}.#{member.name} = new byte[#{member.name}Size]; - stream.read(#{value}.#{member.name}, 0, #{member.name}Size); - EOS - when AST::Declarations::Array ; - if (member.declaration.fixed?) - out.puts "int #{member.name}Size = #{convert_constant member.declaration.size};" - else - out.puts "int #{member.name}Size = stream.readInt();" - end - out.puts <<-EOS.strip_heredoc - #{value}.#{member.name} = new #{type_string member.type}[#{member.name}Size]; - for (int i = 0; i < #{member.name}Size; i++) { - #{value}.#{member.name}[i] = #{decode_type member.declaration}; - } - EOS - else - out.puts "#{value}.#{member.name} = #{decode_type member.declaration};" - end - if member.type.sub_type == :optional - out.puts "}" - end - end - - def decode_type(decl) - case decl.type - when AST::Typespecs::Int ; - "stream.readInt()" - when AST::Typespecs::UnsignedInt ; - "XdrUnsignedInteger.decode(stream)" - when AST::Typespecs::Hyper ; - "stream.readLong()" - when AST::Typespecs::UnsignedHyper ; - "XdrUnsignedHyperInteger.decode(stream)" - when AST::Typespecs::Float ; - "stream.readFloat()" - when AST::Typespecs::Double ; - "stream.readDouble()" - when AST::Typespecs::Quadruple ; - raise "cannot render quadruple in golang" - when AST::Typespecs::Bool ; - "stream.readInt() == 1 ? true : false" - when AST::Typespecs::String ; - "XdrString.decode(stream, #{(convert_constant decl.size) || 'Integer.MAX_VALUE'})" - when AST::Typespecs::Simple ; - "#{name decl.type.resolved_type}.decode(stream)" - when AST::Concerns::NestedDefinition ; - "#{name decl.type}.decode(stream)" - else - raise "Unknown typespec: #{decl.type.class.name}" - end - end - - def decl_string(decl) - case decl - when AST::Declarations::Opaque ; - "byte[]" - when AST::Declarations::String ; - "XdrString" - when AST::Declarations::Array ; - "#{type_string decl.type}[]" - when AST::Declarations::Optional ; - "#{type_string(decl.type)}" - when AST::Declarations::Simple ; - type_string(decl.type) - else - raise "Unknown declaration type: #{decl.class.name}" - end - end - - def is_decl_array(decl) - case decl - when AST::Declarations::Opaque ; - true - when AST::Declarations::Array ; - true - when AST::Declarations::Optional ; - is_type_array(decl.type) - when AST::Declarations::Simple ; - is_type_array(decl.type) - else - false - end - end - - def is_type_array(type) - case type - when AST::Typespecs::Opaque ; - true - else - false - end - end - - def type_string(type) - case type - when AST::Typespecs::Int ; - "Integer" - when AST::Typespecs::UnsignedInt ; - "XdrUnsignedInteger" - when AST::Typespecs::Hyper ; - "Long" - when AST::Typespecs::UnsignedHyper ; - "XdrUnsignedHyperInteger" - when AST::Typespecs::Float ; - "Float" - when AST::Typespecs::Double ; - "Double" - when AST::Typespecs::Quadruple ; - raise "cannot render quadruple in golang" - when AST::Typespecs::Bool ; - "Boolean" - when AST::Typespecs::Opaque ; - "Byte[#{convert_constant type.size}]" - when AST::Typespecs::String ; - "XdrString" - when AST::Typespecs::Simple ; - name type.resolved_type - when AST::Concerns::NestedDefinition ; - name type - else - raise "Unknown typespec: #{type.class.name}" - end - end - - def name(named) - parent = name named.parent_defn if named.is_a?(AST::Concerns::NestedDefinition) - result = named.name.camelize - - "#{parent}#{result}" - end - - def name_string(name) - name.camelize - end - - def escape_html(value) - value.to_s - .gsub('&', '&') - .gsub('<', '<') - .gsub('>', '>') - .gsub('*', '*') # to avoid encountering`*/` - end - - def convert_constant(str) - if str.nil? || str.empty? - str - elsif str =~ /\A\d+\z/ - str - else - "Constants.#{str}" - end - end - end - end -end diff --git a/lib/xdrgen/generators/java/XdrDataInputStream.erb b/lib/xdrgen/generators/java/XdrDataInputStream.erb deleted file mode 100644 index 127551e48..000000000 --- a/lib/xdrgen/generators/java/XdrDataInputStream.erb +++ /dev/null @@ -1,122 +0,0 @@ -package <%= @namespace %>; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/lib/xdrgen/generators/java/XdrDataOutputStream.erb b/lib/xdrgen/generators/java/XdrDataOutputStream.erb deleted file mode 100644 index 6069833a9..000000000 --- a/lib/xdrgen/generators/java/XdrDataOutputStream.erb +++ /dev/null @@ -1,96 +0,0 @@ -package <%= @namespace %>; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/lib/xdrgen/generators/java/XdrElement.erb b/lib/xdrgen/generators/java/XdrElement.erb deleted file mode 100644 index 13daaaf5d..000000000 --- a/lib/xdrgen/generators/java/XdrElement.erb +++ /dev/null @@ -1,21 +0,0 @@ -package <%= @namespace %>; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/lib/xdrgen/generators/java/XdrString.erb b/lib/xdrgen/generators/java/XdrString.erb deleted file mode 100644 index cceb6d475..000000000 --- a/lib/xdrgen/generators/java/XdrString.erb +++ /dev/null @@ -1,62 +0,0 @@ -package <%= @namespace %>; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb b/lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb deleted file mode 100644 index e586be844..000000000 --- a/lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb +++ /dev/null @@ -1,69 +0,0 @@ -package <%= @namespace %>; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/lib/xdrgen/generators/java/XdrUnsignedInteger.erb b/lib/xdrgen/generators/java/XdrUnsignedInteger.erb deleted file mode 100644 index 8767d6d12..000000000 --- a/lib/xdrgen/generators/java/XdrUnsignedInteger.erb +++ /dev/null @@ -1,57 +0,0 @@ -package <%= @namespace %>; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/lib/xdrgen/generators/python.rb b/lib/xdrgen/generators/python.rb deleted file mode 100644 index 507f49f92..000000000 --- a/lib/xdrgen/generators/python.rb +++ /dev/null @@ -1,695 +0,0 @@ -# Note: -# 1. If the .x file contains Python reserved words, I suggest you change them to non-reserved words. -# 2. You can generate the file with the following command -# xdrgen -o OUTPUT_DIR INPUT -l python -# 3. The generated code is unformatted, I suggest you format it by the following command: -# autoflake --in-place --ignore-init-module-imports --remove-all-unused-imports OUTPUT_DIR/*.py -# isort OUTPUT_DIR/ -# black OUTPUT_DIR/ - -module Xdrgen - module Generators - class Python < Xdrgen::Generators::Base - MAX_SIZE = (2 ** 32) - 1 - CIRCLE_IMPORT_UNION = %w[SCVal SCSpecTypeDef] - - def generate - @constants_out = @output.open("constants.py") - @constants_out.puts <<-EOS.strip_heredoc - # This is an automatically generated file. - # DO NOT EDIT or your changes may be overwritten - EOS - - @init_out = @output.open("__init__.py") - @init_out.puts <<-EOS.strip_heredoc - # Automatically generated by xdrgen - # DO NOT EDIT or your changes may be overwritten - from .base import * - from .constants import * - EOS - - render_base_classes - render_definitions(@top) - end - - private - - def render_definitions(node) - node.definitions.each { |n| render_definition n } - node.namespaces.each { |n| render_definitions n } - end - - def render_nested_definitions(defn) - return unless defn.respond_to? :nested_definitions - defn.nested_definitions.each { |ndefn| render_definition ndefn } - end - - def render_definition(defn) - render_nested_definitions(defn) - case defn - when AST::Definitions::Struct; - render_struct defn - when AST::Definitions::Enum - render_enum defn - when AST::Definitions::Union; - if CIRCLE_IMPORT_UNION.include?(defn.name) - render_union defn, true - else - render_union defn - end - when AST::Definitions::Typedef - render_typedef defn - when AST::Definitions::Const - render_const defn - end - end - - def render_const(const) - render_const_source_comment @constants_out, const - @constants_out.puts "#{const.name}: int = #{const.value}" - end - - def render_enum(enum) - enum_name = name enum - enum_name_underscore = enum_name.underscore - @init_out.puts "from .#{enum_name_underscore} import #{enum_name}" - - file_name = "#{enum_name_underscore}.py" - out = @output.open(file_name) - render_common_import out - - out.puts "__all__ = ['#{enum_name}']" - - out.puts "class #{enum_name}(IntEnum):" - out.indent(2) do - render_source_comment out, enum - enum.members.each do |member| - out.puts "#{member.name} = #{member.value}" - end - - out.puts <<~HEREDOC - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> #{enum_name}: - value = unpacker.unpack_int() - return cls(value) - HEREDOC - - render_xdr_utils out, enum_name - out.close - end - end - - def render_typedef(typedef) - typedef_name = typedef.name.camelize - typedef_name_underscore = typedef.name.underscore - - @init_out.puts "from .#{typedef_name_underscore} import #{typedef_name}" - - file_name = "#{typedef_name_underscore}.py" - out = @output.open(file_name) - render_common_import out - - render_import out, typedef, typedef_name - - out.puts "__all__ = ['#{typedef_name}']" - - out.puts "class #{typedef_name}:" - out.indent(2) do - render_source_comment(out, typedef) - out.puts "def __init__(self, #{typedef_name_underscore}: #{type_hint_string typedef, typedef_name}) -> None:" - out.indent(2) do - render_array_length_checker typedef, out - out.puts "self.#{typedef_name_underscore} = #{typedef_name_underscore}" - end - - out.puts "def pack(self, packer: Packer) -> None:" - out.indent(2) do - encode_member typedef, out - end - - out.puts "@classmethod" - out.puts "def unpack(cls, unpacker: Unpacker) -> #{typedef_name}:" - out.indent(2) do - decode_member typedef, out - out.puts "return cls(#{typedef_name_underscore})" - end - render_xdr_utils(out, typedef_name) - out.puts <<~HEREDOC - def __hash__(self): - return hash(self.#{typedef_name_underscore}) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.#{typedef_name_underscore} == other.#{typedef_name_underscore} - - def __str__(self): - return f"<#{typedef_name} [#{typedef_name_underscore}={self.#{typedef_name_underscore}}]>" - HEREDOC - end - out.close - end - - def render_import(out, member, container_name) - member_type = type_string member.type - unless is_base_type member.type or container_name == member_type - out.puts "from .#{member_type.underscore} import #{member_type}" - end - end - - def render_union(union, render_import_in_func = false) - union_name = name union - union_name_underscore = union_name.underscore - @init_out.puts "from .#{union_name_underscore} import #{union_name}" - - file_name = "#{union_name_underscore}.py" - out = @output.open(file_name) - render_common_import out - - render_import out, union.discriminant, union_name - - if render_import_in_func - out.puts "if TYPE_CHECKING:" - out.indent(2) do - union.arms.each do |arm| - next if arm.void? - # This may cause duplicate imports, we can remove it with autoflake - render_import out, arm.declaration, union_name - end - end - else - union.arms.each do |arm| - next if arm.void? - # This may cause duplicate imports, we can remove it with autoflake - render_import out, arm.declaration, union_name - end - end - - out.puts "__all__ = ['#{union_name}']" - - out.puts "class #{union_name}:" - out.indent(2) do - render_source_comment(out, union) - union_discriminant_name_underscore = union.discriminant.name.underscore - out.puts <<~HEREDOC - def __init__( - self, - #{union_discriminant_name_underscore}: #{type_hint_string union.discriminant, union_name}, - HEREDOC - - out.indent(2) do - union.arms.each do |arm| - next if arm.void? - out.puts "#{arm.name.underscore}: #{type_hint_string arm.declaration, union_name} = None," - end - end - - out.puts ") -> None:" - out.indent(2) do - union.arms.each do |arm| - next if arm.void? - render_array_length_checker arm, out - end - - out.puts "self.#{union_discriminant_name_underscore} = #{union_discriminant_name_underscore}" - union.arms.each do |arm| - next if arm.void? - arm_name_underscore = arm.name.underscore - out.puts "self.#{arm_name_underscore} = #{arm_name_underscore}" - end - end - - out.puts "def pack(self, packer: Packer) -> None:" - out.indent(2) do - out.puts "#{encode_type union.discriminant, 'self.' + union_discriminant_name_underscore}" - union.normal_arms.each do |arm| - arm.cases.each do |c| - if c.value.is_a?(AST::Identifier) - out.puts "if self.#{union_discriminant_name_underscore} == #{type_string union.discriminant.type}.#{c.value.name}:" - else - out.puts "if self.#{union_discriminant_name_underscore} == #{c.value.value}:" - end - out.indent(2) do - unless arm.void? - encode_member arm, out, true - end - out.puts "return" - end - end - end - if union.default_arm.present? and not union.default_arm.void? - encode_member union.default_arm, out, true - end - end - - out.puts "@classmethod" - out.puts "def unpack(cls, unpacker: Unpacker) -> #{union_name}:" - out.indent(2) do - out.puts "#{union_discriminant_name_underscore} = #{decode_type union.discriminant}" - union.normal_arms.each do |arm| - arm.cases.each do |c| - if c.value.is_a?(AST::Identifier) - out.puts "if #{union_discriminant_name_underscore} == #{type_string union.discriminant.type}.#{c.value.name}:" - else - out.puts "if #{union_discriminant_name_underscore} == #{c.value.value}:" - end - out.indent(2) do - if arm.void? - out.puts "return cls(#{union_discriminant_name_underscore}=#{union_discriminant_name_underscore})" - else - if render_import_in_func - render_import out, arm.declaration, union_name - end - - decode_member arm, out - arm_name_underscore = arm.name.underscore - out.puts "return cls(#{union_discriminant_name_underscore}=#{union_discriminant_name_underscore}, #{arm_name_underscore}=#{arm_name_underscore})" - end - end - end - end - - if union.default_arm.present? and not union.default_arm.void? - decode_member union.default_arm, out - arm_name_underscore = union.default_arm.name.underscore - out.puts "return cls(#{union_discriminant_name_underscore}=#{union_discriminant_name_underscore}, #{arm_name_underscore}=#{arm_name_underscore})" - else - out.puts "return cls(#{union_discriminant_name_underscore}=#{union_discriminant_name_underscore})" - end - end - - render_xdr_utils(out, union_name) - attribute_names = [] - attribute_names.push(union_discriminant_name_underscore) - union.arms.each do |arm| - next if arm.void? - attribute_names.push(arm.name.underscore) - end - out.puts <<~HEREDOC - def __hash__(self): - return hash((#{attribute_names.map { |m| 'self.' + m }.join(", ")},)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return #{attribute_names.map { |m| 'self.' + m + '== other.' + m }.join(" and ")} - HEREDOC - - out.puts "def __str__(self):" - out.indent(2) do - out.puts "out = []" - out.puts "out.append(f'#{union_discriminant_name_underscore}={self.#{union_discriminant_name_underscore}}')" - union.arms.each do |arm| - next if arm.void? - arm_name_underscore = arm.name.underscore - out.puts "out.append(f'#{arm_name_underscore}={self.#{arm_name_underscore}}') if self.#{arm_name_underscore} is not None else None" - end - out.puts "return f\"<#{union_name} [{', '.join(out)}]>\"" - end - end - out.close - end - - def render_struct(struct) - struct_name = name struct - struct_name_underscore = struct_name.underscore - @init_out.puts "from .#{struct_name_underscore} import #{struct_name}" - - file_name = "#{struct_name_underscore}.py" - out = @output.open(file_name) - render_common_import out - - struct.members.each do |member| - # This may cause duplicate imports, we can remove it through autoflake - render_import out, member.declaration, struct_name - end - - out.puts "__all__ = ['#{struct_name}']" - - out.puts "class #{struct_name}:" - out.indent(2) do - render_source_comment(out, struct) - out.puts <<~HEREDOC - def __init__( - self, - HEREDOC - - out.indent(2) do - struct.members.each do |member| - out.puts "#{member.name.underscore}: #{type_hint_string member.declaration, struct_name}," - end - end - out.puts ") -> None:" - - out.indent(2) do - struct.members.each do |member| - render_array_length_checker member, out - end - struct.members.each do |member| - member_name_underscore = member.name.underscore - out.puts "self.#{member_name_underscore} = #{member_name_underscore}" - end - end - out.puts "def pack(self, packer: Packer) -> None:" - out.indent(2) do - struct.members.each do |member| - encode_member member, out - end - end - - out.puts "@classmethod" - out.puts "def unpack(cls, unpacker: Unpacker) -> #{struct_name}:" - out.indent(2) do - struct.members.each do |member| - decode_member member, out - end - out.puts "return cls(" - out.indent(2) do - struct.members.each do |member| - member_name_underscore = member.name.underscore - out.puts "#{member_name_underscore}=#{member_name_underscore}," - end - end - out.puts ")" - end - - render_xdr_utils(out, struct_name) - - attribute_names = [] - struct.members.each do |member| - attribute_names.push(member.name.underscore) - end - out.puts <<~HEREDOC - def __hash__(self): - return hash((#{attribute_names.map { |m| 'self.' + m }.join(", ")},)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return #{attribute_names.map { |m| 'self.' + m + '== other.' + m }.join(" and ")} - HEREDOC - - out.puts "def __str__(self):" - out.indent(2) do - out.puts "out = [" - out.indent(2) do - attribute_names.each do |name| - name = name - out.puts "f'#{name}={self.#{name}}'," - end - end - out.puts "]" - out.puts "return f\"<#{struct_name} [{', '.join(out)}]>\"" - end - - end - out.close - end - - def encode_member(member, out, is_union_member = false) - case member.declaration - when AST::Declarations::Void - out.puts "return" - end - member_name_underscore = member.name.underscore - if member.type.sub_type == :optional - out.puts <<~HEREDOC - if self.#{member_name_underscore} is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - HEREDOC - end - - out.indent(member.type.sub_type == :optional ? 2 : 0) do - if is_union_member # All members of union are actually optional - out.puts <<~HEREDOC - if self.#{member_name_underscore} is None: - raise ValueError("#{member_name_underscore} should not be None.") - HEREDOC - end - case member.declaration - when AST::Declarations::Array - unless member.declaration.fixed? - out.puts "packer.pack_uint(len(self.#{member_name_underscore}))" - end - out.puts <<~HEREDOC - for #{member_name_underscore}_item in self.#{member_name_underscore}: - #{encode_type member.declaration, member_name_underscore + '_item'} - HEREDOC - else - out.puts encode_type member.declaration, 'self.' + member_name_underscore - end - end - end - - def decode_member(member, out) - case member.declaration - when AST::Declarations::Void; - out.puts "return" - end - member_name_underscore = member.name.underscore - decoded_member_declaration = decode_type member.declaration - - case member.declaration - when AST::Declarations::Array - if member.declaration.fixed? - _, size = member.declaration.type.array_size - out.puts "length = #{size}" - else - out.puts "length = unpacker.unpack_uint()" - end - out.puts <<-EOS.strip_heredoc - #{member_name_underscore} = [] - for _ in range(length): - #{member_name_underscore}.append(#{decoded_member_declaration}) - EOS - else - if member.type.sub_type == :optional - out.puts "#{member_name_underscore} = #{decoded_member_declaration} if unpacker.unpack_uint() else None" - else - out.puts "#{member_name_underscore} = #{decoded_member_declaration}" - end - end - end - - def render_common_import(out) - out.puts <<-EOS.strip_heredoc - # This is an automatically generated file. - # DO NOT EDIT or your changes may be overwritten - from __future__ import annotations - - import base64 - from enum import IntEnum - from typing import List, Optional, TYPE_CHECKING - from xdrlib3 import Packer, Unpacker - from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque - from .constants import * - EOS - out.break - end - - def render_array_length_checker(member, out) - case member.declaration - when AST::Declarations::Array - _, size = member.declaration.type.array_size - member_name_underscore = member.name.underscore - if member.declaration.fixed? - out.puts <<~HEREDOC - _expect_length = #{size} - if #{member_name_underscore} and len(#{member_name_underscore}) != _expect_length: - raise ValueError(f\"The length of `#{member_name_underscore}` should be {_expect_length}, but got {len(#{member_name_underscore})}.\") - HEREDOC - else - out.puts <<~HEREDOC - _expect_max_length = #{size || MAX_SIZE} - if #{member_name_underscore} and len(#{member_name_underscore}) > _expect_max_length: - raise ValueError(f\"The maximum length of `#{member_name_underscore}` should be {_expect_max_length}, but got {len(#{member_name_underscore})}.\") - HEREDOC - end - end - end - - def render_xdr_utils(out, name) - out.puts <<~HEREDOC - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> #{name}: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> #{name}: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - HEREDOC - end - - def render_base_classes - file_name = "base.py" - out = @output.open(file_name) - base_py_content = IO.read(__dir__ + "/python/base.py") - out.puts base_py_content - out.close - end - - def encode_type(decl, value) - case decl.type - when AST::Typespecs::Int; - "Integer(#{value}).pack(packer)" - when AST::Typespecs::UnsignedInt; - "UnsignedInteger(#{value}).pack(packer)" - when AST::Typespecs::Hyper; - "Hyper(#{value}).pack(packer)" - when AST::Typespecs::UnsignedHyper; - "UnsignedHyper(#{value}).pack(packer)" - when AST::Typespecs::Float; - "Float(#{value}).pack(packer)" - when AST::Typespecs::Double; - "Double(#{value}).pack(packer)" - when AST::Typespecs::Quadruple; - raise "cannot render quadruple in Python" - when AST::Typespecs::Bool; - "Boolean(#{value}).pack(packer)" - when AST::Typespecs::Opaque; - "Opaque(#{value}, #{decl.size || MAX_SIZE}, #{decl.fixed? ? "True" : "False"}).pack(packer)" - when AST::Typespecs::String; - "String(#{value}, #{decl.size || MAX_SIZE}).pack(packer)" - else - "#{value}.pack(packer)" - end - end - - def decode_type(decl) - case decl.type - when AST::Typespecs::Int - "Integer.unpack(unpacker)" - when AST::Typespecs::UnsignedInt - "UnsignedInteger.unpack(unpacker)" - when AST::Typespecs::Hyper - "Hyper.unpack(unpacker)" - when AST::Typespecs::UnsignedHyper - "UnsignedHyper.unpack(unpacker)" - when AST::Typespecs::Float - "Float.unpack(unpacker)" - when AST::Typespecs::Double - "Double.unpack(unpacker)" - when AST::Typespecs::Quadruple - raise "cannot render quadruple in Python" - when AST::Typespecs::Bool - "Boolean.unpack(unpacker)" - when AST::Typespecs::Opaque - "Opaque.unpack(unpacker, #{decl.size || MAX_SIZE}, #{decl.fixed? ? "True" : "False"})" - when AST::Typespecs::String - "String.unpack(unpacker)" - when AST::Typespecs::Simple - "#{name decl.type.resolved_type}.unpack(unpacker)" - when AST::Concerns::NestedDefinition - "#{name decl.type}.unpack(unpacker)" - else - raise "Unknown typespec: #{decl.type.class.name}" - end - end - - def render_source_comment(out, defn) - return if defn.is_a?(AST::Definitions::Namespace) - - out.puts <<-EOS.strip_heredoc - """ - XDR Source Code:: - - EOS - out.indent(2) do - out.puts defn.text_value - end - - out.puts '"""' - end - - def render_const_source_comment(out, defn) - return if defn.is_a?(AST::Definitions::Namespace) - out.puts "#: #{defn.text_value}" - end - - def type_hint_string(decl, container_name) - type_hint = type_string decl.type - if type_hint == container_name - type_hint = "\"#{type_hint}\"" - end - - case decl.type.sub_type - when :optional - "Optional[#{type_hint}]" - when :var_array, :array - "List[#{type_hint}]" - else - type_hint - end - end - - def is_base_type(type) - case type - when AST::Typespecs::Bool, - AST::Typespecs::Double, - AST::Typespecs::Float, - AST::Typespecs::Hyper, - AST::Typespecs::Int, - AST::Typespecs::Opaque, - AST::Typespecs::String, - AST::Typespecs::UnsignedHyper, - AST::Typespecs::UnsignedInt - true - else - false - end - end - - def type_string(type) - case type - when AST::Typespecs::Bool - "bool" - when AST::Typespecs::Double - "float" - when AST::Typespecs::Float - "float" - when AST::Typespecs::Hyper - "int" - when AST::Typespecs::Int - "int" - when AST::Typespecs::Opaque - "bytes" - when AST::Typespecs::Quadruple - raise "no quadruple support for Python" - when AST::Typespecs::String - "bytes" - when AST::Typespecs::UnsignedHyper - "int" - when AST::Typespecs::UnsignedInt - "int" - when AST::Typespecs::Simple - name type - when AST::Definitions::Base - name type - when AST::Concerns::NestedDefinition - name type - else - raise "Unknown reference type: #{type.class.name}, #{type.class.ancestors}" - end - end - - def name(named) - parent = name named.parent_defn if named.is_a?(AST::Concerns::NestedDefinition) - result = named.name.camelize - "#{parent}#{result}" - end - end - end -end \ No newline at end of file diff --git a/lib/xdrgen/generators/python/base.py b/lib/xdrgen/generators/python/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/lib/xdrgen/generators/python/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/lib/xdrgen/generator_spec.rb b/spec/lib/xdrgen/generator_spec.rb index 00787f01c..fa66e3ef2 100644 --- a/spec/lib/xdrgen/generator_spec.rb +++ b/spec/lib/xdrgen/generator_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Xdrgen::Generators do - languages = %w(ruby javascript go java elixir python) + languages = %w(ruby javascript go elixir) focus_language = "" #"go" focus_basename = "" #"optional.x" diff --git a/spec/output/generator_spec_java/block_comments.x/AccountFlags.java b/spec/output/generator_spec_java/block_comments.x/AccountFlags.java deleted file mode 100644 index aab0ec7bf..000000000 --- a/spec/output/generator_spec_java/block_comments.x/AccountFlags.java +++ /dev/null @@ -1,56 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * AccountFlags's original definition in the XDR file is: - *
- * enum AccountFlags
- * { // masks for each flag
- *     AUTH_REQUIRED_FLAG = 0x1
- * };
- * 
- */ -public enum AccountFlags implements XdrElement { - AUTH_REQUIRED_FLAG(1); - - private final int value; - - AccountFlags(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static AccountFlags decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 1: return AUTH_REQUIRED_FLAG; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static AccountFlags fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static AccountFlags fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/Constants.java b/spec/output/generator_spec_java/block_comments.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/block_comments.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrDataInputStream.java b/spec/output/generator_spec_java/block_comments.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/block_comments.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrElement.java b/spec/output/generator_spec_java/block_comments.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrString.java b/spec/output/generator_spec_java/block_comments.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/block_comments.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/block_comments.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/block_comments.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/block_comments.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/const.x/Constants.java b/spec/output/generator_spec_java/const.x/Constants.java deleted file mode 100644 index 1e2b80847..000000000 --- a/spec/output/generator_spec_java/const.x/Constants.java +++ /dev/null @@ -1,11 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} - public static final int FOO = 1; -} diff --git a/spec/output/generator_spec_java/const.x/TestArray.java b/spec/output/generator_spec_java/const.x/TestArray.java deleted file mode 100644 index 212c7cc38..000000000 --- a/spec/output/generator_spec_java/const.x/TestArray.java +++ /dev/null @@ -1,53 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * TestArray's original definition in the XDR file is: - *
- * typedef int TestArray[FOO];
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class TestArray implements XdrElement { - private Integer[] TestArray; - public void encode(XdrDataOutputStream stream) throws IOException { - int TestArraySize = getTestArray().length; - for (int i = 0; i < TestArraySize; i++) { - stream.writeInt(TestArray[i]); - } - } - - public static TestArray decode(XdrDataInputStream stream) throws IOException { - TestArray decodedTestArray = new TestArray(); - int TestArraySize = Constants.FOO; - decodedTestArray.TestArray = new Integer[TestArraySize]; - for (int i = 0; i < TestArraySize; i++) { - decodedTestArray.TestArray[i] = stream.readInt(); - } - return decodedTestArray; - } - - public static TestArray fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static TestArray fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/const.x/TestArray2.java b/spec/output/generator_spec_java/const.x/TestArray2.java deleted file mode 100644 index 13212a0b3..000000000 --- a/spec/output/generator_spec_java/const.x/TestArray2.java +++ /dev/null @@ -1,54 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * TestArray2's original definition in the XDR file is: - *
- * typedef int TestArray2<FOO>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class TestArray2 implements XdrElement { - private Integer[] TestArray2; - public void encode(XdrDataOutputStream stream) throws IOException { - int TestArray2Size = getTestArray2().length; - stream.writeInt(TestArray2Size); - for (int i = 0; i < TestArray2Size; i++) { - stream.writeInt(TestArray2[i]); - } - } - - public static TestArray2 decode(XdrDataInputStream stream) throws IOException { - TestArray2 decodedTestArray2 = new TestArray2(); - int TestArray2Size = stream.readInt(); - decodedTestArray2.TestArray2 = new Integer[TestArray2Size]; - for (int i = 0; i < TestArray2Size; i++) { - decodedTestArray2.TestArray2[i] = stream.readInt(); - } - return decodedTestArray2; - } - - public static TestArray2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static TestArray2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrDataInputStream.java b/spec/output/generator_spec_java/const.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/const.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/const.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/const.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrElement.java b/spec/output/generator_spec_java/const.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/const.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrString.java b/spec/output/generator_spec_java/const.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/const.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/const.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/const.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/const.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/const.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/const.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/Color.java b/spec/output/generator_spec_java/enum.x/Color.java deleted file mode 100644 index 55a7f5587..000000000 --- a/spec/output/generator_spec_java/enum.x/Color.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * Color's original definition in the XDR file is: - *
- * enum Color {
- *     RED=0,  
- *     GREEN=1,  
- *     BLUE=2  
- * };
- * 
- */ -public enum Color implements XdrElement { - RED(0), - GREEN(1), - BLUE(2); - - private final int value; - - Color(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Color decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return RED; - case 1: return GREEN; - case 2: return BLUE; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static Color fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Color fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/Color2.java b/spec/output/generator_spec_java/enum.x/Color2.java deleted file mode 100644 index 57053ce2f..000000000 --- a/spec/output/generator_spec_java/enum.x/Color2.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * Color2's original definition in the XDR file is: - *
- * enum Color2 {
- *     RED2=RED,  
- *     GREEN2=1,  
- *     BLUE2=2  
- * };
- * 
- */ -public enum Color2 implements XdrElement { - RED2(0), - GREEN2(1), - BLUE2(2); - - private final int value; - - Color2(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Color2 decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return RED2; - case 1: return GREEN2; - case 2: return BLUE2; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static Color2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Color2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/Color3.java b/spec/output/generator_spec_java/enum.x/Color3.java deleted file mode 100644 index 210d964db..000000000 --- a/spec/output/generator_spec_java/enum.x/Color3.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * Color3's original definition in the XDR file is: - *
- * enum Color3 {
- *     RED_1=1,
- *     RED_2_TWO=2,
- *     RED_3=3
- * };
- * 
- */ -public enum Color3 implements XdrElement { - RED_1(1), - RED_2_TWO(2), - RED_3(3); - - private final int value; - - Color3(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Color3 decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 1: return RED_1; - case 2: return RED_2_TWO; - case 3: return RED_3; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static Color3 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Color3 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/Constants.java b/spec/output/generator_spec_java/enum.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/enum.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/enum.x/MessageType.java b/spec/output/generator_spec_java/enum.x/MessageType.java deleted file mode 100644 index 1ea906d4b..000000000 --- a/spec/output/generator_spec_java/enum.x/MessageType.java +++ /dev/null @@ -1,101 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * MessageType's original definition in the XDR file is: - *
- * enum MessageType
- * {
- *     ERROR_MSG,    
- *     HELLO,
- *     DONT_HAVE,
- * 
- *     GET_PEERS,   // gets a list of peers this guy knows about        
- *     PEERS,
- * 
- *     GET_TX_SET,  // gets a particular txset by hash        
- *     TX_SET,    
- * 
- *     GET_VALIDATIONS, // gets validations for a given ledger hash        
- *     VALIDATIONS,    
- * 
- *     TRANSACTION, //pass on a tx you have heard about        
- *     JSON_TRANSACTION,
- * 
- *     // FBA        
- *     GET_FBA_QUORUMSET,        
- *     FBA_QUORUMSET,    
- *     FBA_MESSAGE
- * };
- * 
- */ -public enum MessageType implements XdrElement { - ERROR_MSG(0), - HELLO(1), - DONT_HAVE(2), - GET_PEERS(3), - PEERS(4), - GET_TX_SET(5), - TX_SET(6), - GET_VALIDATIONS(7), - VALIDATIONS(8), - TRANSACTION(9), - JSON_TRANSACTION(10), - GET_FBA_QUORUMSET(11), - FBA_QUORUMSET(12), - FBA_MESSAGE(13); - - private final int value; - - MessageType(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static MessageType decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return ERROR_MSG; - case 1: return HELLO; - case 2: return DONT_HAVE; - case 3: return GET_PEERS; - case 4: return PEERS; - case 5: return GET_TX_SET; - case 6: return TX_SET; - case 7: return GET_VALIDATIONS; - case 8: return VALIDATIONS; - case 9: return TRANSACTION; - case 10: return JSON_TRANSACTION; - case 11: return GET_FBA_QUORUMSET; - case 12: return FBA_QUORUMSET; - case 13: return FBA_MESSAGE; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static MessageType fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MessageType fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrDataInputStream.java b/spec/output/generator_spec_java/enum.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/enum.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrElement.java b/spec/output/generator_spec_java/enum.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrString.java b/spec/output/generator_spec_java/enum.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/enum.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/enum.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/enum.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/enum.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/Constants.java b/spec/output/generator_spec_java/nesting.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/nesting.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/nesting.x/Foo.java b/spec/output/generator_spec_java/nesting.x/Foo.java deleted file mode 100644 index 1455c50e5..000000000 --- a/spec/output/generator_spec_java/nesting.x/Foo.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Foo's original definition in the XDR file is: - *
- * typedef int Foo;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Foo implements XdrElement { - private Integer Foo; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(Foo); - } - - public static Foo decode(XdrDataInputStream stream) throws IOException { - Foo decodedFoo = new Foo(); - decodedFoo.Foo = stream.readInt(); - return decodedFoo; - } - - public static Foo fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Foo fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/MyUnion.java b/spec/output/generator_spec_java/nesting.x/MyUnion.java deleted file mode 100644 index bc59082e8..000000000 --- a/spec/output/generator_spec_java/nesting.x/MyUnion.java +++ /dev/null @@ -1,158 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * MyUnion's original definition in the XDR file is: - *
- * union MyUnion switch (UnionKey type)
- * {
- *     case ONE:
- *         struct {
- *             int someInt;
- *         } one;
- * 
- *     case TWO:
- *         struct {
- *             int someInt;
- *             Foo foo;
- *         } two;
- * 
- *     case OFFER:
- *         void;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class MyUnion implements XdrElement { - private UnionKey discriminant; - private MyUnionOne one; - private MyUnionTwo two; - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(discriminant.getValue()); - switch (discriminant) { - case ONE: - one.encode(stream); - break; - case TWO: - two.encode(stream); - break; - case OFFER: - break; - } - } - public static MyUnion decode(XdrDataInputStream stream) throws IOException { - MyUnion decodedMyUnion = new MyUnion(); - UnionKey discriminant = UnionKey.decode(stream); - decodedMyUnion.setDiscriminant(discriminant); - switch (decodedMyUnion.getDiscriminant()) { - case ONE: - decodedMyUnion.one = MyUnionOne.decode(stream); - break; - case TWO: - decodedMyUnion.two = MyUnionTwo.decode(stream); - break; - case OFFER: - break; - } - return decodedMyUnion; - } - public static MyUnion fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyUnion fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - /** - * MyUnionOne's original definition in the XDR file is: - *
-   * struct {
-   *             int someInt;
-   *         }
-   * 
- */ - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder(toBuilder = true) - public static class MyUnionOne implements XdrElement { - private Integer someInt; - public void encode(XdrDataOutputStream stream) throws IOException{ - stream.writeInt(someInt); - } - public static MyUnionOne decode(XdrDataInputStream stream) throws IOException { - MyUnionOne decodedMyUnionOne = new MyUnionOne(); - decodedMyUnionOne.someInt = stream.readInt(); - return decodedMyUnionOne; - } - public static MyUnionOne fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyUnionOne fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - } - /** - * MyUnionTwo's original definition in the XDR file is: - *
-   * struct {
-   *             int someInt;
-   *             Foo foo;
-   *         }
-   * 
- */ - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder(toBuilder = true) - public static class MyUnionTwo implements XdrElement { - private Integer someInt; - private Foo foo; - public void encode(XdrDataOutputStream stream) throws IOException{ - stream.writeInt(someInt); - foo.encode(stream); - } - public static MyUnionTwo decode(XdrDataInputStream stream) throws IOException { - MyUnionTwo decodedMyUnionTwo = new MyUnionTwo(); - decodedMyUnionTwo.someInt = stream.readInt(); - decodedMyUnionTwo.foo = Foo.decode(stream); - return decodedMyUnionTwo; - } - public static MyUnionTwo fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyUnionTwo fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - } -} diff --git a/spec/output/generator_spec_java/nesting.x/UnionKey.java b/spec/output/generator_spec_java/nesting.x/UnionKey.java deleted file mode 100644 index 80278b683..000000000 --- a/spec/output/generator_spec_java/nesting.x/UnionKey.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * UnionKey's original definition in the XDR file is: - *
- * enum UnionKey {
- *   ONE = 1,
- *   TWO = 2,
- *   OFFER = 3
- * };
- * 
- */ -public enum UnionKey implements XdrElement { - ONE(1), - TWO(2), - OFFER(3); - - private final int value; - - UnionKey(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static UnionKey decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 1: return ONE; - case 2: return TWO; - case 3: return OFFER; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static UnionKey fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static UnionKey fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrDataInputStream.java b/spec/output/generator_spec_java/nesting.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/nesting.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrElement.java b/spec/output/generator_spec_java/nesting.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrString.java b/spec/output/generator_spec_java/nesting.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/nesting.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/nesting.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/nesting.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/nesting.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/optional.x/Arr.java b/spec/output/generator_spec_java/optional.x/Arr.java deleted file mode 100644 index 4ca07a5cb..000000000 --- a/spec/output/generator_spec_java/optional.x/Arr.java +++ /dev/null @@ -1,53 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Arr's original definition in the XDR file is: - *
- * typedef int Arr[2];
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Arr implements XdrElement { - private Integer[] Arr; - public void encode(XdrDataOutputStream stream) throws IOException { - int ArrSize = getArr().length; - for (int i = 0; i < ArrSize; i++) { - stream.writeInt(Arr[i]); - } - } - - public static Arr decode(XdrDataInputStream stream) throws IOException { - Arr decodedArr = new Arr(); - int ArrSize = 2; - decodedArr.Arr = new Integer[ArrSize]; - for (int i = 0; i < ArrSize; i++) { - decodedArr.Arr[i] = stream.readInt(); - } - return decodedArr; - } - - public static Arr fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Arr fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/optional.x/Constants.java b/spec/output/generator_spec_java/optional.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/optional.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/optional.x/HasOptions.java b/spec/output/generator_spec_java/optional.x/HasOptions.java deleted file mode 100644 index f9bdba731..000000000 --- a/spec/output/generator_spec_java/optional.x/HasOptions.java +++ /dev/null @@ -1,81 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * HasOptions's original definition in the XDR file is: - *
- * struct HasOptions
- * {
- *   int* firstOption;
- *   int *secondOption;
- *   Arr *thirdOption;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class HasOptions implements XdrElement { - private Integer firstOption; - private Integer secondOption; - private Arr thirdOption; - public void encode(XdrDataOutputStream stream) throws IOException{ - if (firstOption != null) { - stream.writeInt(1); - stream.writeInt(firstOption); - } else { - stream.writeInt(0); - } - if (secondOption != null) { - stream.writeInt(1); - stream.writeInt(secondOption); - } else { - stream.writeInt(0); - } - if (thirdOption != null) { - stream.writeInt(1); - thirdOption.encode(stream); - } else { - stream.writeInt(0); - } - } - public static HasOptions decode(XdrDataInputStream stream) throws IOException { - HasOptions decodedHasOptions = new HasOptions(); - int firstOptionPresent = stream.readInt(); - if (firstOptionPresent != 0) { - decodedHasOptions.firstOption = stream.readInt(); - } - int secondOptionPresent = stream.readInt(); - if (secondOptionPresent != 0) { - decodedHasOptions.secondOption = stream.readInt(); - } - int thirdOptionPresent = stream.readInt(); - if (thirdOptionPresent != 0) { - decodedHasOptions.thirdOption = Arr.decode(stream); - } - return decodedHasOptions; - } - public static HasOptions fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static HasOptions fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrDataInputStream.java b/spec/output/generator_spec_java/optional.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/optional.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrElement.java b/spec/output/generator_spec_java/optional.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrString.java b/spec/output/generator_spec_java/optional.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/optional.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/optional.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/optional.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/optional.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/struct.x/Constants.java b/spec/output/generator_spec_java/struct.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/struct.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/struct.x/Int64.java b/spec/output/generator_spec_java/struct.x/Int64.java deleted file mode 100644 index 2ea7adf9b..000000000 --- a/spec/output/generator_spec_java/struct.x/Int64.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Int64's original definition in the XDR file is: - *
- * typedef hyper int64;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Int64 implements XdrElement { - private Long int64; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeLong(int64); - } - - public static Int64 decode(XdrDataInputStream stream) throws IOException { - Int64 decodedInt64 = new Int64(); - decodedInt64.int64 = stream.readLong(); - return decodedInt64; - } - - public static Int64 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Int64 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/struct.x/MyStruct.java b/spec/output/generator_spec_java/struct.x/MyStruct.java deleted file mode 100644 index 446224f88..000000000 --- a/spec/output/generator_spec_java/struct.x/MyStruct.java +++ /dev/null @@ -1,68 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * MyStruct's original definition in the XDR file is: - *
- * struct MyStruct
- * {
- *     int    someInt;
- *     int64  aBigInt;
- *     opaque someOpaque[10];
- *     string someString<>;
- *     string maxString<100>;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class MyStruct implements XdrElement { - private Integer someInt; - private Int64 aBigInt; - private byte[] someOpaque; - private XdrString someString; - private XdrString maxString; - public void encode(XdrDataOutputStream stream) throws IOException{ - stream.writeInt(someInt); - aBigInt.encode(stream); - int someOpaqueSize = someOpaque.length; - stream.write(getSomeOpaque(), 0, someOpaqueSize); - someString.encode(stream); - maxString.encode(stream); - } - public static MyStruct decode(XdrDataInputStream stream) throws IOException { - MyStruct decodedMyStruct = new MyStruct(); - decodedMyStruct.someInt = stream.readInt(); - decodedMyStruct.aBigInt = Int64.decode(stream); - int someOpaqueSize = 10; - decodedMyStruct.someOpaque = new byte[someOpaqueSize]; - stream.read(decodedMyStruct.someOpaque, 0, someOpaqueSize); - decodedMyStruct.someString = XdrString.decode(stream, Integer.MAX_VALUE); - decodedMyStruct.maxString = XdrString.decode(stream, 100); - return decodedMyStruct; - } - public static MyStruct fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyStruct fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrDataInputStream.java b/spec/output/generator_spec_java/struct.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/struct.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrElement.java b/spec/output/generator_spec_java/struct.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrString.java b/spec/output/generator_spec_java/struct.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/struct.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/struct.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/struct.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/struct.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Color.java b/spec/output/generator_spec_java/test.x/Color.java deleted file mode 100644 index 2199ca352..000000000 --- a/spec/output/generator_spec_java/test.x/Color.java +++ /dev/null @@ -1,61 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * Color's original definition in the XDR file is: - *
- * enum Color {
- *   RED,
- *   BLUE = 5,
- *   GREEN
- * };
- * 
- */ -public enum Color implements XdrElement { - RED(0), - BLUE(5), - GREEN(6); - - private final int value; - - Color(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static Color decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return RED; - case 5: return BLUE; - case 6: return GREEN; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static Color fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Color fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Constants.java b/spec/output/generator_spec_java/test.x/Constants.java deleted file mode 100644 index 646061555..000000000 --- a/spec/output/generator_spec_java/test.x/Constants.java +++ /dev/null @@ -1,12 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} - public static final int FOO = 1244; - public static final int BAR = FOO; -} diff --git a/spec/output/generator_spec_java/test.x/HasStuff.java b/spec/output/generator_spec_java/test.x/HasStuff.java deleted file mode 100644 index 2b98be3ec..000000000 --- a/spec/output/generator_spec_java/test.x/HasStuff.java +++ /dev/null @@ -1,49 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * HasStuff's original definition in the XDR file is: - *
- * struct HasStuff
- * {
- *   LotsOfMyStructs data;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class HasStuff implements XdrElement { - private LotsOfMyStructs data; - public void encode(XdrDataOutputStream stream) throws IOException{ - data.encode(stream); - } - public static HasStuff decode(XdrDataInputStream stream) throws IOException { - HasStuff decodedHasStuff = new HasStuff(); - decodedHasStuff.data = LotsOfMyStructs.decode(stream); - return decodedHasStuff; - } - public static HasStuff fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static HasStuff fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Hash.java b/spec/output/generator_spec_java/test.x/Hash.java deleted file mode 100644 index 2546f2dcf..000000000 --- a/spec/output/generator_spec_java/test.x/Hash.java +++ /dev/null @@ -1,49 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Hash's original definition in the XDR file is: - *
- * typedef opaque Hash[32];
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Hash implements XdrElement { - private byte[] Hash; - public void encode(XdrDataOutputStream stream) throws IOException { - int HashSize = Hash.length; - stream.write(getHash(), 0, HashSize); - } - - public static Hash decode(XdrDataInputStream stream) throws IOException { - Hash decodedHash = new Hash(); - int HashSize = 32; - decodedHash.Hash = new byte[HashSize]; - stream.read(decodedHash.Hash, 0, HashSize); - return decodedHash; - } - - public static Hash fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Hash fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Hashes1.java b/spec/output/generator_spec_java/test.x/Hashes1.java deleted file mode 100644 index bb181417c..000000000 --- a/spec/output/generator_spec_java/test.x/Hashes1.java +++ /dev/null @@ -1,53 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Hashes1's original definition in the XDR file is: - *
- * typedef Hash Hashes1[12];
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Hashes1 implements XdrElement { - private Hash[] Hashes1; - public void encode(XdrDataOutputStream stream) throws IOException { - int Hashes1Size = getHashes1().length; - for (int i = 0; i < Hashes1Size; i++) { - Hashes1[i].encode(stream); - } - } - - public static Hashes1 decode(XdrDataInputStream stream) throws IOException { - Hashes1 decodedHashes1 = new Hashes1(); - int Hashes1Size = 12; - decodedHashes1.Hashes1 = new Hash[Hashes1Size]; - for (int i = 0; i < Hashes1Size; i++) { - decodedHashes1.Hashes1[i] = Hash.decode(stream); - } - return decodedHashes1; - } - - public static Hashes1 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Hashes1 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Hashes2.java b/spec/output/generator_spec_java/test.x/Hashes2.java deleted file mode 100644 index 009e6a06f..000000000 --- a/spec/output/generator_spec_java/test.x/Hashes2.java +++ /dev/null @@ -1,54 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Hashes2's original definition in the XDR file is: - *
- * typedef Hash Hashes2<12>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Hashes2 implements XdrElement { - private Hash[] Hashes2; - public void encode(XdrDataOutputStream stream) throws IOException { - int Hashes2Size = getHashes2().length; - stream.writeInt(Hashes2Size); - for (int i = 0; i < Hashes2Size; i++) { - Hashes2[i].encode(stream); - } - } - - public static Hashes2 decode(XdrDataInputStream stream) throws IOException { - Hashes2 decodedHashes2 = new Hashes2(); - int Hashes2Size = stream.readInt(); - decodedHashes2.Hashes2 = new Hash[Hashes2Size]; - for (int i = 0; i < Hashes2Size; i++) { - decodedHashes2.Hashes2[i] = Hash.decode(stream); - } - return decodedHashes2; - } - - public static Hashes2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Hashes2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Hashes3.java b/spec/output/generator_spec_java/test.x/Hashes3.java deleted file mode 100644 index c8c6c5cf4..000000000 --- a/spec/output/generator_spec_java/test.x/Hashes3.java +++ /dev/null @@ -1,54 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Hashes3's original definition in the XDR file is: - *
- * typedef Hash Hashes3<>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Hashes3 implements XdrElement { - private Hash[] Hashes3; - public void encode(XdrDataOutputStream stream) throws IOException { - int Hashes3Size = getHashes3().length; - stream.writeInt(Hashes3Size); - for (int i = 0; i < Hashes3Size; i++) { - Hashes3[i].encode(stream); - } - } - - public static Hashes3 decode(XdrDataInputStream stream) throws IOException { - Hashes3 decodedHashes3 = new Hashes3(); - int Hashes3Size = stream.readInt(); - decodedHashes3.Hashes3 = new Hash[Hashes3Size]; - for (int i = 0; i < Hashes3Size; i++) { - decodedHashes3.Hashes3[i] = Hash.decode(stream); - } - return decodedHashes3; - } - - public static Hashes3 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Hashes3 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Int1.java b/spec/output/generator_spec_java/test.x/Int1.java deleted file mode 100644 index 499189275..000000000 --- a/spec/output/generator_spec_java/test.x/Int1.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Int1's original definition in the XDR file is: - *
- * typedef int             int1;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Int1 implements XdrElement { - private Integer int1; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(int1); - } - - public static Int1 decode(XdrDataInputStream stream) throws IOException { - Int1 decodedInt1 = new Int1(); - decodedInt1.int1 = stream.readInt(); - return decodedInt1; - } - - public static Int1 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Int1 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Int2.java b/spec/output/generator_spec_java/test.x/Int2.java deleted file mode 100644 index 8b600e54e..000000000 --- a/spec/output/generator_spec_java/test.x/Int2.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Int2's original definition in the XDR file is: - *
- * typedef hyper           int2;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Int2 implements XdrElement { - private Long int2; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeLong(int2); - } - - public static Int2 decode(XdrDataInputStream stream) throws IOException { - Int2 decodedInt2 = new Int2(); - decodedInt2.int2 = stream.readLong(); - return decodedInt2; - } - - public static Int2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Int2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Int3.java b/spec/output/generator_spec_java/test.x/Int3.java deleted file mode 100644 index 86e303c0b..000000000 --- a/spec/output/generator_spec_java/test.x/Int3.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Int3's original definition in the XDR file is: - *
- * typedef unsigned int    int3;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Int3 implements XdrElement { - private XdrUnsignedInteger int3; - public void encode(XdrDataOutputStream stream) throws IOException { - int3.encode(stream); - } - - public static Int3 decode(XdrDataInputStream stream) throws IOException { - Int3 decodedInt3 = new Int3(); - decodedInt3.int3 = XdrUnsignedInteger.decode(stream); - return decodedInt3; - } - - public static Int3 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Int3 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Int4.java b/spec/output/generator_spec_java/test.x/Int4.java deleted file mode 100644 index 69c5a608b..000000000 --- a/spec/output/generator_spec_java/test.x/Int4.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Int4's original definition in the XDR file is: - *
- * typedef unsigned hyper  int4;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Int4 implements XdrElement { - private XdrUnsignedHyperInteger int4; - public void encode(XdrDataOutputStream stream) throws IOException { - int4.encode(stream); - } - - public static Int4 decode(XdrDataInputStream stream) throws IOException { - Int4 decodedInt4 = new Int4(); - decodedInt4.int4 = XdrUnsignedHyperInteger.decode(stream); - return decodedInt4; - } - - public static Int4 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Int4 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/LotsOfMyStructs.java b/spec/output/generator_spec_java/test.x/LotsOfMyStructs.java deleted file mode 100644 index eaa3fb0c1..000000000 --- a/spec/output/generator_spec_java/test.x/LotsOfMyStructs.java +++ /dev/null @@ -1,57 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * LotsOfMyStructs's original definition in the XDR file is: - *
- * struct LotsOfMyStructs
- * {
- *     MyStruct members<>;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class LotsOfMyStructs implements XdrElement { - private MyStruct[] members; - public void encode(XdrDataOutputStream stream) throws IOException{ - int membersSize = getMembers().length; - stream.writeInt(membersSize); - for (int i = 0; i < membersSize; i++) { - members[i].encode(stream); - } - } - public static LotsOfMyStructs decode(XdrDataInputStream stream) throws IOException { - LotsOfMyStructs decodedLotsOfMyStructs = new LotsOfMyStructs(); - int membersSize = stream.readInt(); - decodedLotsOfMyStructs.members = new MyStruct[membersSize]; - for (int i = 0; i < membersSize; i++) { - decodedLotsOfMyStructs.members[i] = MyStruct.decode(stream); - } - return decodedLotsOfMyStructs; - } - public static LotsOfMyStructs fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static LotsOfMyStructs fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/MyStruct.java b/spec/output/generator_spec_java/test.x/MyStruct.java deleted file mode 100644 index 44f18615c..000000000 --- a/spec/output/generator_spec_java/test.x/MyStruct.java +++ /dev/null @@ -1,73 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * MyStruct's original definition in the XDR file is: - *
- * struct MyStruct
- * {
- *     uint512 field1;
- *     optHash1 field2;
- *     int1 field3;
- *     unsigned int field4;
- *     float field5;
- *     double field6;
- *     bool field7;
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class MyStruct implements XdrElement { - private Uint512 field1; - private OptHash1 field2; - private Int1 field3; - private XdrUnsignedInteger field4; - private Float field5; - private Double field6; - private Boolean field7; - public void encode(XdrDataOutputStream stream) throws IOException{ - field1.encode(stream); - field2.encode(stream); - field3.encode(stream); - field4.encode(stream); - stream.writeFloat(field5); - stream.writeDouble(field6); - stream.writeInt(field7 ? 1 : 0); - } - public static MyStruct decode(XdrDataInputStream stream) throws IOException { - MyStruct decodedMyStruct = new MyStruct(); - decodedMyStruct.field1 = Uint512.decode(stream); - decodedMyStruct.field2 = OptHash1.decode(stream); - decodedMyStruct.field3 = Int1.decode(stream); - decodedMyStruct.field4 = XdrUnsignedInteger.decode(stream); - decodedMyStruct.field5 = stream.readFloat(); - decodedMyStruct.field6 = stream.readDouble(); - decodedMyStruct.field7 = stream.readInt() == 1 ? true : false; - return decodedMyStruct; - } - public static MyStruct fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyStruct fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Nester.java b/spec/output/generator_spec_java/test.x/Nester.java deleted file mode 100644 index bc8bcce59..000000000 --- a/spec/output/generator_spec_java/test.x/Nester.java +++ /dev/null @@ -1,208 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * Nester's original definition in the XDR file is: - *
- * struct Nester
- * {
- *   enum {
- *     BLAH_1,
- *     BLAH_2
- *   } nestedEnum;
- * 
- *   struct {
- *     int blah;
- *   } nestedStruct;
- * 
- *   union switch (Color color) {
- *     case RED:
- *       void;
- *     default:
- *       int blah2;
- *   } nestedUnion;
- * 
- * 
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class Nester implements XdrElement { - private NesterNestedEnum nestedEnum; - private NesterNestedStruct nestedStruct; - private NesterNestedUnion nestedUnion; - public void encode(XdrDataOutputStream stream) throws IOException{ - nestedEnum.encode(stream); - nestedStruct.encode(stream); - nestedUnion.encode(stream); - } - public static Nester decode(XdrDataInputStream stream) throws IOException { - Nester decodedNester = new Nester(); - decodedNester.nestedEnum = NesterNestedEnum.decode(stream); - decodedNester.nestedStruct = NesterNestedStruct.decode(stream); - decodedNester.nestedUnion = NesterNestedUnion.decode(stream); - return decodedNester; - } - public static Nester fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Nester fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - /** - * NesterNestedEnum's original definition in the XDR file is: - *
-   * enum {
-   *     BLAH_1,
-   *     BLAH_2
-   *   }
-   * 
- */ - public static enum NesterNestedEnum implements XdrElement { - BLAH_1(0), - BLAH_2(1); - - private final int value; - - NestedEnum(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static NestedEnum decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return BLAH_1; - case 1: return BLAH_2; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static NestedEnum fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static NestedEnum fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - } - /** - * NesterNestedStruct's original definition in the XDR file is: - *
-   * struct {
-   *     int blah;
-   *   }
-   * 
- */ - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder(toBuilder = true) - public static class NesterNestedStruct implements XdrElement { - private Integer blah; - public void encode(XdrDataOutputStream stream) throws IOException{ - stream.writeInt(blah); - } - public static NesterNestedStruct decode(XdrDataInputStream stream) throws IOException { - NesterNestedStruct decodedNesterNestedStruct = new NesterNestedStruct(); - decodedNesterNestedStruct.blah = stream.readInt(); - return decodedNesterNestedStruct; - } - public static NesterNestedStruct fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static NesterNestedStruct fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - } - /** - * NesterNestedUnion's original definition in the XDR file is: - *
-   * union switch (Color color) {
-   *     case RED:
-   *       void;
-   *     default:
-   *       int blah2;
-   *   }
-   * 
- */ - @Data - @NoArgsConstructor - @AllArgsConstructor - @Builder(toBuilder = true) - public static class NesterNestedUnion implements XdrElement { - private Color discriminant; - private Integer blah2; - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(discriminant.getValue()); - switch (discriminant) { - case RED: - break; - default: - stream.writeInt(blah2); - break; - } - } - public static NesterNestedUnion decode(XdrDataInputStream stream) throws IOException { - NesterNestedUnion decodedNesterNestedUnion = new NesterNestedUnion(); - Color discriminant = Color.decode(stream); - decodedNesterNestedUnion.setDiscriminant(discriminant); - switch (decodedNesterNestedUnion.getDiscriminant()) { - case RED: - break; - default: - decodedNesterNestedUnion.blah2 = stream.readInt(); - break; - } - return decodedNesterNestedUnion; - } - public static NesterNestedUnion fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static NesterNestedUnion fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } - - } -} diff --git a/spec/output/generator_spec_java/test.x/OptHash1.java b/spec/output/generator_spec_java/test.x/OptHash1.java deleted file mode 100644 index 7d3f328b0..000000000 --- a/spec/output/generator_spec_java/test.x/OptHash1.java +++ /dev/null @@ -1,54 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * OptHash1's original definition in the XDR file is: - *
- * typedef Hash *optHash1;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class OptHash1 implements XdrElement { - private Hash optHash1; - public void encode(XdrDataOutputStream stream) throws IOException { - if (optHash1 != null) { - stream.writeInt(1); - optHash1.encode(stream); - } else { - stream.writeInt(0); - } - } - - public static OptHash1 decode(XdrDataInputStream stream) throws IOException { - OptHash1 decodedOptHash1 = new OptHash1(); - int optHash1Present = stream.readInt(); - if (optHash1Present != 0) { - decodedOptHash1.optHash1 = Hash.decode(stream); - } - return decodedOptHash1; - } - - public static OptHash1 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static OptHash1 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/OptHash2.java b/spec/output/generator_spec_java/test.x/OptHash2.java deleted file mode 100644 index 65817c9a7..000000000 --- a/spec/output/generator_spec_java/test.x/OptHash2.java +++ /dev/null @@ -1,54 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * OptHash2's original definition in the XDR file is: - *
- * typedef Hash* optHash2;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class OptHash2 implements XdrElement { - private Hash optHash2; - public void encode(XdrDataOutputStream stream) throws IOException { - if (optHash2 != null) { - stream.writeInt(1); - optHash2.encode(stream); - } else { - stream.writeInt(0); - } - } - - public static OptHash2 decode(XdrDataInputStream stream) throws IOException { - OptHash2 decodedOptHash2 = new OptHash2(); - int optHash2Present = stream.readInt(); - if (optHash2Present != 0) { - decodedOptHash2.optHash2 = Hash.decode(stream); - } - return decodedOptHash2; - } - - public static OptHash2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static OptHash2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Str.java b/spec/output/generator_spec_java/test.x/Str.java deleted file mode 100644 index 9d79f0dbb..000000000 --- a/spec/output/generator_spec_java/test.x/Str.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Str's original definition in the XDR file is: - *
- * typedef string str<64>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Str implements XdrElement { - private XdrString str; - public void encode(XdrDataOutputStream stream) throws IOException { - str.encode(stream); - } - - public static Str decode(XdrDataInputStream stream) throws IOException { - Str decodedStr = new Str(); - decodedStr.str = XdrString.decode(stream, 64); - return decodedStr; - } - - public static Str fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Str fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Str2.java b/spec/output/generator_spec_java/test.x/Str2.java deleted file mode 100644 index 28dc3a00f..000000000 --- a/spec/output/generator_spec_java/test.x/Str2.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Str2's original definition in the XDR file is: - *
- * typedef string str2<>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Str2 implements XdrElement { - private XdrString str2; - public void encode(XdrDataOutputStream stream) throws IOException { - str2.encode(stream); - } - - public static Str2 decode(XdrDataInputStream stream) throws IOException { - Str2 decodedStr2 = new Str2(); - decodedStr2.str2 = XdrString.decode(stream, Integer.MAX_VALUE); - return decodedStr2; - } - - public static Str2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Str2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Uint512.java b/spec/output/generator_spec_java/test.x/Uint512.java deleted file mode 100644 index 1eaa83cf1..000000000 --- a/spec/output/generator_spec_java/test.x/Uint512.java +++ /dev/null @@ -1,49 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Uint512's original definition in the XDR file is: - *
- * typedef opaque uint512[64];
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Uint512 implements XdrElement { - private byte[] uint512; - public void encode(XdrDataOutputStream stream) throws IOException { - int uint512Size = uint512.length; - stream.write(getUint512(), 0, uint512Size); - } - - public static Uint512 decode(XdrDataInputStream stream) throws IOException { - Uint512 decodedUint512 = new Uint512(); - int uint512Size = 64; - decodedUint512.uint512 = new byte[uint512Size]; - stream.read(decodedUint512.uint512, 0, uint512Size); - return decodedUint512; - } - - public static Uint512 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Uint512 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Uint513.java b/spec/output/generator_spec_java/test.x/Uint513.java deleted file mode 100644 index 3bc636f5f..000000000 --- a/spec/output/generator_spec_java/test.x/Uint513.java +++ /dev/null @@ -1,50 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Uint513's original definition in the XDR file is: - *
- * typedef opaque uint513<64>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Uint513 implements XdrElement { - private byte[] uint513; - public void encode(XdrDataOutputStream stream) throws IOException { - int uint513Size = uint513.length; - stream.writeInt(uint513Size); - stream.write(getUint513(), 0, uint513Size); - } - - public static Uint513 decode(XdrDataInputStream stream) throws IOException { - Uint513 decodedUint513 = new Uint513(); - int uint513Size = stream.readInt(); - decodedUint513.uint513 = new byte[uint513Size]; - stream.read(decodedUint513.uint513, 0, uint513Size); - return decodedUint513; - } - - public static Uint513 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Uint513 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/Uint514.java b/spec/output/generator_spec_java/test.x/Uint514.java deleted file mode 100644 index a8b500329..000000000 --- a/spec/output/generator_spec_java/test.x/Uint514.java +++ /dev/null @@ -1,50 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Uint514's original definition in the XDR file is: - *
- * typedef opaque uint514<>;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Uint514 implements XdrElement { - private byte[] uint514; - public void encode(XdrDataOutputStream stream) throws IOException { - int uint514Size = uint514.length; - stream.writeInt(uint514Size); - stream.write(getUint514(), 0, uint514Size); - } - - public static Uint514 decode(XdrDataInputStream stream) throws IOException { - Uint514 decodedUint514 = new Uint514(); - int uint514Size = stream.readInt(); - decodedUint514.uint514 = new byte[uint514Size]; - stream.read(decodedUint514.uint514, 0, uint514Size); - return decodedUint514; - } - - public static Uint514 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Uint514 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrDataInputStream.java b/spec/output/generator_spec_java/test.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/test.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/test.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/test.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrElement.java b/spec/output/generator_spec_java/test.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/test.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrString.java b/spec/output/generator_spec_java/test.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/test.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/test.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/test.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/test.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/test.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/test.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/Constants.java b/spec/output/generator_spec_java/union.x/Constants.java deleted file mode 100644 index d7d9f4cfc..000000000 --- a/spec/output/generator_spec_java/union.x/Constants.java +++ /dev/null @@ -1,10 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -public final class Constants { - private Constants() {} -} diff --git a/spec/output/generator_spec_java/union.x/Error.java b/spec/output/generator_spec_java/union.x/Error.java deleted file mode 100644 index 197d97721..000000000 --- a/spec/output/generator_spec_java/union.x/Error.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Error's original definition in the XDR file is: - *
- * typedef int Error;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Error implements XdrElement { - private Integer Error; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(Error); - } - - public static Error decode(XdrDataInputStream stream) throws IOException { - Error decodedError = new Error(); - decodedError.Error = stream.readInt(); - return decodedError; - } - - public static Error fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Error fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/IntUnion.java b/spec/output/generator_spec_java/union.x/IntUnion.java deleted file mode 100644 index f08e1a7e5..000000000 --- a/spec/output/generator_spec_java/union.x/IntUnion.java +++ /dev/null @@ -1,81 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * IntUnion's original definition in the XDR file is: - *
- * union IntUnion switch (int type)
- * {
- *     case 0:
- *         Error error;
- *     case 1:
- *         Multi things<>;
- * 
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class IntUnion implements XdrElement { - private Integer discriminant; - private Error error; - private Multi[] things; - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(discriminant); - switch (discriminant) { - case 0: - error.encode(stream); - break; - case 1: - int thingsSize = getThings().length; - stream.writeInt(thingsSize); - for (int i = 0; i < thingsSize; i++) { - things[i].encode(stream); - } - break; - } - } - public static IntUnion decode(XdrDataInputStream stream) throws IOException { - IntUnion decodedIntUnion = new IntUnion(); - Integer discriminant = stream.readInt(); - decodedIntUnion.setDiscriminant(discriminant); - switch (decodedIntUnion.getDiscriminant()) { - case 0: - decodedIntUnion.error = Error.decode(stream); - break; - case 1: - int thingsSize = stream.readInt(); - decodedIntUnion.things = new Multi[thingsSize]; - for (int i = 0; i < thingsSize; i++) { - decodedIntUnion.things[i] = Multi.decode(stream); - } - break; - } - return decodedIntUnion; - } - public static IntUnion fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static IntUnion fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/IntUnion2.java b/spec/output/generator_spec_java/union.x/IntUnion2.java deleted file mode 100644 index 71ad3f774..000000000 --- a/spec/output/generator_spec_java/union.x/IntUnion2.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * IntUnion2's original definition in the XDR file is: - *
- * typedef IntUnion IntUnion2;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class IntUnion2 implements XdrElement { - private IntUnion IntUnion2; - public void encode(XdrDataOutputStream stream) throws IOException { - IntUnion2.encode(stream); - } - - public static IntUnion2 decode(XdrDataInputStream stream) throws IOException { - IntUnion2 decodedIntUnion2 = new IntUnion2(); - decodedIntUnion2.IntUnion2 = IntUnion.decode(stream); - return decodedIntUnion2; - } - - public static IntUnion2 fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static IntUnion2 fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/Multi.java b/spec/output/generator_spec_java/union.x/Multi.java deleted file mode 100644 index 18ca8ece1..000000000 --- a/spec/output/generator_spec_java/union.x/Multi.java +++ /dev/null @@ -1,46 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; - -/** - * Multi's original definition in the XDR file is: - *
- * typedef int Multi;
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Multi implements XdrElement { - private Integer Multi; - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(Multi); - } - - public static Multi decode(XdrDataInputStream stream) throws IOException { - Multi decodedMulti = new Multi(); - decodedMulti.Multi = stream.readInt(); - return decodedMulti; - } - - public static Multi fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static Multi fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/MyUnion.java b/spec/output/generator_spec_java/union.x/MyUnion.java deleted file mode 100644 index fc125f853..000000000 --- a/spec/output/generator_spec_java/union.x/MyUnion.java +++ /dev/null @@ -1,82 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.AllArgsConstructor; -import lombok.Builder; - -/** - * MyUnion's original definition in the XDR file is: - *
- * union MyUnion switch (UnionKey type)
- * {
- *     case ERROR:
- *         Error error;
- *     case MULTI:
- *         Multi things<>;
- * 
- * 
- * };
- * 
- */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder(toBuilder = true) -public class MyUnion implements XdrElement { - private UnionKey discriminant; - private Error error; - private Multi[] things; - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(discriminant.getValue()); - switch (discriminant) { - case ERROR: - error.encode(stream); - break; - case MULTI: - int thingsSize = getThings().length; - stream.writeInt(thingsSize); - for (int i = 0; i < thingsSize; i++) { - things[i].encode(stream); - } - break; - } - } - public static MyUnion decode(XdrDataInputStream stream) throws IOException { - MyUnion decodedMyUnion = new MyUnion(); - UnionKey discriminant = UnionKey.decode(stream); - decodedMyUnion.setDiscriminant(discriminant); - switch (decodedMyUnion.getDiscriminant()) { - case ERROR: - decodedMyUnion.error = Error.decode(stream); - break; - case MULTI: - int thingsSize = stream.readInt(); - decodedMyUnion.things = new Multi[thingsSize]; - for (int i = 0; i < thingsSize; i++) { - decodedMyUnion.things[i] = Multi.decode(stream); - } - break; - } - return decodedMyUnion; - } - public static MyUnion fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static MyUnion fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/UnionKey.java b/spec/output/generator_spec_java/union.x/UnionKey.java deleted file mode 100644 index 504e811cc..000000000 --- a/spec/output/generator_spec_java/union.x/UnionKey.java +++ /dev/null @@ -1,58 +0,0 @@ -// Automatically generated by xdrgen -// DO NOT EDIT or your changes may be overwritten - -package MyXDR; - -import java.io.IOException; - -import org.stellar.sdk.Base64Factory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - -/** - * UnionKey's original definition in the XDR file is: - *
- * enum UnionKey {
- *   ERROR,
- *   MULTI
- * };
- * 
- */ -public enum UnionKey implements XdrElement { - ERROR(0), - MULTI(1); - - private final int value; - - UnionKey(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static UnionKey decode(XdrDataInputStream stream) throws IOException { - int value = stream.readInt(); - switch (value) { - case 0: return ERROR; - case 1: return MULTI; - default: - throw new IllegalArgumentException("Unknown enum value: " + value); - } - } - - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(value); - } - public static UnionKey fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static UnionKey fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrDataInputStream.java b/spec/output/generator_spec_java/union.x/XdrDataInputStream.java deleted file mode 100644 index 6d6592026..000000000 --- a/spec/output/generator_spec_java/union.x/XdrDataInputStream.java +++ /dev/null @@ -1,122 +0,0 @@ -package MyXDR; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -public class XdrDataInputStream extends DataInputStream { - - // The underlying input stream - private final XdrInputStream mIn; - - /** - * Creates a XdrDataInputStream that uses the specified - * underlying InputStream. - * - * @param in the specified input stream - */ - public XdrDataInputStream(InputStream in) { - super(new XdrInputStream(in)); - mIn = (XdrInputStream) super.in; - } - - public int[] readIntArray() throws IOException { - int l = readInt(); - return readIntArray(l); - } - - private int[] readIntArray(int l) throws IOException { - int[] arr = new int[l]; - for (int i = 0; i < l; i++) { - arr[i] = readInt(); - } - return arr; - } - - public float[] readFloatArray() throws IOException { - int l = readInt(); - return readFloatArray(l); - } - - private float[] readFloatArray(int l) throws IOException { - float[] arr = new float[l]; - for (int i = 0; i < l; i++) { - arr[i] = readFloat(); - } - return arr; - } - - public double[] readDoubleArray() throws IOException { - int l = readInt(); - return readDoubleArray(l); - } - - private double[] readDoubleArray(int l) throws IOException { - double[] arr = new double[l]; - for (int i = 0; i < l; i++) { - arr[i] = readDouble(); - } - return arr; - } - - @Override - public int read() throws IOException { - return super.read(); - } - - /** - * Need to provide a custom impl of InputStream as DataInputStream's read methods - * are final and we need to keep track of the count for padding purposes. - */ - private static final class XdrInputStream extends InputStream { - - // The underlying input stream - private final InputStream mIn; - - // The amount of bytes read so far. - private int mCount; - - public XdrInputStream(InputStream in) { - mIn = in; - mCount = 0; - } - - @Override - public int read() throws IOException { - int read = mIn.read(); - if (read >= 0) { - mCount++; - } - return read; - } - - @Override - public int read(byte[] b) throws IOException { - return read(b, 0, b.length); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - int read = mIn.read(b, off, len); - mCount += read; - pad(); - return read; - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - - while (pad-- > 0) { - int b = read(); - if (b != 0) { - throw new IOException("non-zero padding"); - } - } - } - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrDataOutputStream.java b/spec/output/generator_spec_java/union.x/XdrDataOutputStream.java deleted file mode 100644 index 729aec05b..000000000 --- a/spec/output/generator_spec_java/union.x/XdrDataOutputStream.java +++ /dev/null @@ -1,96 +0,0 @@ -package MyXDR; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; - -public class XdrDataOutputStream extends DataOutputStream { - - private final XdrOutputStream mOut; - - public XdrDataOutputStream(OutputStream out) { - super(new XdrOutputStream(out)); - mOut = (XdrOutputStream) super.out; - } - - public void writeIntArray(int[] a) throws IOException { - writeInt(a.length); - writeIntArray(a, a.length); - } - - private void writeIntArray(int[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeInt(a[i]); - } - } - - public void writeFloatArray(float[] a) throws IOException { - writeInt(a.length); - writeFloatArray(a, a.length); - } - - private void writeFloatArray(float[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeFloat(a[i]); - } - } - - public void writeDoubleArray(double[] a) throws IOException { - writeInt(a.length); - writeDoubleArray(a, a.length); - } - - private void writeDoubleArray(double[] a, int l) throws IOException { - for (int i = 0; i < l; i++) { - writeDouble(a[i]); - } - } - - private static final class XdrOutputStream extends OutputStream { - - private final OutputStream mOut; - - // Number of bytes written - private int mCount; - - public XdrOutputStream(OutputStream out) { - mOut = out; - mCount = 0; - } - - @Override - public void write(int b) throws IOException { - mOut.write(b); - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int): - // > The byte to be written is the eight low-order bits of the argument b. - // > The 24 high-order bits of b are ignored. - mCount++; - } - - @Override - public void write(byte[] b) throws IOException { - // https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]): - // > The general contract for write(b) is that it should have exactly the same effect - // > as the call write(b, 0, b.length). - write(b, 0, b.length); - } - - public void write(byte[] b, int offset, int length) throws IOException { - mOut.write(b, offset, length); - mCount += length; - pad(); - } - - public void pad() throws IOException { - int pad = 0; - int mod = mCount % 4; - if (mod > 0) { - pad = 4-mod; - } - while (pad-- > 0) { - write(0); - } - } - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrElement.java b/spec/output/generator_spec_java/union.x/XdrElement.java deleted file mode 100644 index 453491ea2..000000000 --- a/spec/output/generator_spec_java/union.x/XdrElement.java +++ /dev/null @@ -1,21 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import org.stellar.sdk.Base64Factory; - -/** Common parent interface for all generated classes. */ -interface XdrElement { - void encode(XdrDataOutputStream stream) throws IOException; - - default String toXdrBase64() throws IOException { - return Base64Factory.getInstance().encodeToString(toXdrByteArray()); - } - - default byte[] toXdrByteArray() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream); - encode(xdrDataOutputStream); - return byteArrayOutputStream.toByteArray(); - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrString.java b/spec/output/generator_spec_java/union.x/XdrString.java deleted file mode 100644 index a51eead77..000000000 --- a/spec/output/generator_spec_java/union.x/XdrString.java +++ /dev/null @@ -1,62 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InvalidClassException; -import java.nio.charset.StandardCharsets; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -@Value -public class XdrString implements XdrElement { - byte[] bytes; - - public XdrString(byte[] bytes) { - this.bytes = bytes; - } - - public XdrString(String text) { - this.bytes = text.getBytes(StandardCharsets.UTF_8); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(this.bytes.length); - stream.write(this.bytes, 0, this.bytes.length); - } - - public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException { - int size = stream.readInt(); - if (size > maxSize) { - throw new IllegalArgumentException("String length " + size + " exceeds max size " + maxSize); - } - byte[] bytes = new byte[size]; - stream.read(bytes); - return new XdrString(bytes); - } - - public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes, maxSize); - } - - public static XdrString fromXdrBase64(String xdr) throws IOException { - return fromXdrBase64(xdr, Integer.MAX_VALUE); - } - - public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream, maxSize); - } - - public static XdrString fromXdrByteArray(byte[] xdr) throws IOException { - return fromXdrByteArray(xdr, Integer.MAX_VALUE); - } - - @Override - public String toString() { - return new String(bytes, StandardCharsets.UTF_8); - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrUnsignedHyperInteger.java b/spec/output/generator_spec_java/union.x/XdrUnsignedHyperInteger.java deleted file mode 100644 index 5d798cded..000000000 --- a/spec/output/generator_spec_java/union.x/XdrUnsignedHyperInteger.java +++ /dev/null @@ -1,69 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Hyper Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedHyperInteger implements XdrElement { - public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); - public static final BigInteger MIN_VALUE = BigInteger.ZERO; - BigInteger number; - - public XdrUnsignedHyperInteger(BigInteger number) { - if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { - throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedHyperInteger(Long number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Long"); - } - this.number = BigInteger.valueOf(number); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.write(getBytes()); - } - - public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { - byte[] bytes = new byte[8]; - stream.readFully(bytes); - BigInteger uint64 = new BigInteger(1, bytes); - return new XdrUnsignedHyperInteger(uint64); - } - - private byte[] getBytes() { - byte[] bytes = number.toByteArray(); - byte[] paddedBytes = new byte[8]; - - int numBytesToCopy = Math.min(bytes.length, 8); - int copyStartIndex = bytes.length - numBytesToCopy; - System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); - return paddedBytes; - } - - public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_java/union.x/XdrUnsignedInteger.java b/spec/output/generator_spec_java/union.x/XdrUnsignedInteger.java deleted file mode 100644 index bea880aee..000000000 --- a/spec/output/generator_spec_java/union.x/XdrUnsignedInteger.java +++ /dev/null @@ -1,57 +0,0 @@ -package MyXDR; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import lombok.Value; -import org.stellar.sdk.Base64Factory; - -/** - * Represents XDR Unsigned Integer. - * - * @see XDR: External Data - * Representation Standard - */ -@Value -public class XdrUnsignedInteger implements XdrElement { - public static final long MAX_VALUE = (1L << 32) - 1; - public static final long MIN_VALUE = 0; - Long number; - - public XdrUnsignedInteger(Long number) { - if (number < MIN_VALUE || number > MAX_VALUE) { - throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); - } - this.number = number; - } - - public XdrUnsignedInteger(Integer number) { - if (number < 0) { - throw new IllegalArgumentException( - "number must be greater than or equal to 0 if you want to construct it from Integer"); - } - this.number = number.longValue(); - } - - public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { - int intValue = stream.readInt(); - long uint32Value = Integer.toUnsignedLong(intValue); - return new XdrUnsignedInteger(uint32Value); - } - - @Override - public void encode(XdrDataOutputStream stream) throws IOException { - stream.writeInt(number.intValue()); - } - - public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException { - byte[] bytes = Base64Factory.getInstance().decode(xdr); - return fromXdrByteArray(bytes); - } - - public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr); - XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream); - return decode(xdrDataInputStream); - } -} diff --git a/spec/output/generator_spec_python/block_comments.x/__init__.py b/spec/output/generator_spec_python/block_comments.x/__init__.py deleted file mode 100644 index fc7fc5c15..000000000 --- a/spec/output/generator_spec_python/block_comments.x/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .account_flags import AccountFlags diff --git a/spec/output/generator_spec_python/block_comments.x/account_flags.py b/spec/output/generator_spec_python/block_comments.x/account_flags.py deleted file mode 100644 index 2073c5b6e..000000000 --- a/spec/output/generator_spec_python/block_comments.x/account_flags.py +++ /dev/null @@ -1,47 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['AccountFlags'] -class AccountFlags(IntEnum): - """ - XDR Source Code:: - - enum AccountFlags - { // masks for each flag - AUTH_REQUIRED_FLAG = 0x1 - }; - """ - AUTH_REQUIRED_FLAG = 1 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> AccountFlags: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> AccountFlags: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> AccountFlags: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/block_comments.x/base.py b/spec/output/generator_spec_python/block_comments.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/block_comments.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/block_comments.x/constants.py b/spec/output/generator_spec_python/block_comments.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/block_comments.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/const.x/__init__.py b/spec/output/generator_spec_python/const.x/__init__.py deleted file mode 100644 index 8519645a5..000000000 --- a/spec/output/generator_spec_python/const.x/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .test_array import TestArray -from .test_array2 import TestArray2 diff --git a/spec/output/generator_spec_python/const.x/base.py b/spec/output/generator_spec_python/const.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/const.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/const.x/constants.py b/spec/output/generator_spec_python/const.x/constants.py deleted file mode 100644 index 4209e14f4..000000000 --- a/spec/output/generator_spec_python/const.x/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -#: const FOO = 1; -FOO: int = 1 diff --git a/spec/output/generator_spec_python/const.x/test_array.py b/spec/output/generator_spec_python/const.x/test_array.py deleted file mode 100644 index ea0e257f5..000000000 --- a/spec/output/generator_spec_python/const.x/test_array.py +++ /dev/null @@ -1,60 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['TestArray'] -class TestArray: - """ - XDR Source Code:: - - typedef int TestArray[FOO]; - """ - def __init__(self, test_array: List[int]) -> None: - _expect_length = FOO - if test_array and len(test_array) != _expect_length: - raise ValueError(f"The length of `test_array` should be {_expect_length}, but got {len(test_array)}.") - self.test_array = test_array - def pack(self, packer: Packer) -> None: - for test_array_item in self.test_array: - Integer(test_array_item).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> TestArray: - length = FOO - test_array = [] - for _ in range(length): - test_array.append(Integer.unpack(unpacker)) - return cls(test_array) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> TestArray: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> TestArray: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.test_array) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.test_array == other.test_array - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/const.x/test_array2.py b/spec/output/generator_spec_python/const.x/test_array2.py deleted file mode 100644 index 9cfae8895..000000000 --- a/spec/output/generator_spec_python/const.x/test_array2.py +++ /dev/null @@ -1,61 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['TestArray2'] -class TestArray2: - """ - XDR Source Code:: - - typedef int TestArray2; - """ - def __init__(self, test_array2: List[int]) -> None: - _expect_max_length = FOO - if test_array2 and len(test_array2) > _expect_max_length: - raise ValueError(f"The maximum length of `test_array2` should be {_expect_max_length}, but got {len(test_array2)}.") - self.test_array2 = test_array2 - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.test_array2)) - for test_array2_item in self.test_array2: - Integer(test_array2_item).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> TestArray2: - length = unpacker.unpack_uint() - test_array2 = [] - for _ in range(length): - test_array2.append(Integer.unpack(unpacker)) - return cls(test_array2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> TestArray2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> TestArray2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.test_array2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.test_array2 == other.test_array2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/enum.x/__init__.py b/spec/output/generator_spec_python/enum.x/__init__.py deleted file mode 100644 index 3d4636082..000000000 --- a/spec/output/generator_spec_python/enum.x/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .message_type import MessageType -from .color import Color -from .color2 import Color2 -from .color3 import Color3 diff --git a/spec/output/generator_spec_python/enum.x/base.py b/spec/output/generator_spec_python/enum.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/enum.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/enum.x/color.py b/spec/output/generator_spec_python/enum.x/color.py deleted file mode 100644 index 64a35b1a9..000000000 --- a/spec/output/generator_spec_python/enum.x/color.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Color'] -class Color(IntEnum): - """ - XDR Source Code:: - - enum Color { - RED=0, - GREEN=1, - BLUE=2 - }; - """ - RED = 0 - GREEN = 1 - BLUE = 2 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> Color: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Color: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Color: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/enum.x/color2.py b/spec/output/generator_spec_python/enum.x/color2.py deleted file mode 100644 index 9200a79f8..000000000 --- a/spec/output/generator_spec_python/enum.x/color2.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Color2'] -class Color2(IntEnum): - """ - XDR Source Code:: - - enum Color2 { - RED2=RED, - GREEN2=1, - BLUE2=2 - }; - """ - RED2 = 0 - GREEN2 = 1 - BLUE2 = 2 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> Color2: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Color2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Color2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/enum.x/color3.py b/spec/output/generator_spec_python/enum.x/color3.py deleted file mode 100644 index 449df02ca..000000000 --- a/spec/output/generator_spec_python/enum.x/color3.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Color3'] -class Color3(IntEnum): - """ - XDR Source Code:: - - enum Color3 { - RED_1=1, - RED_2_TWO=2, - RED_3=3 - }; - """ - RED_1 = 1 - RED_2_TWO = 2 - RED_3 = 3 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> Color3: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Color3: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Color3: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/enum.x/constants.py b/spec/output/generator_spec_python/enum.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/enum.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/enum.x/message_type.py b/spec/output/generator_spec_python/enum.x/message_type.py deleted file mode 100644 index 2c377f957..000000000 --- a/spec/output/generator_spec_python/enum.x/message_type.py +++ /dev/null @@ -1,79 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['MessageType'] -class MessageType(IntEnum): - """ - XDR Source Code:: - - enum MessageType - { - ERROR_MSG, - HELLO, - DONT_HAVE, - - GET_PEERS, // gets a list of peers this guy knows about - PEERS, - - GET_TX_SET, // gets a particular txset by hash - TX_SET, - - GET_VALIDATIONS, // gets validations for a given ledger hash - VALIDATIONS, - - TRANSACTION, //pass on a tx you have heard about - JSON_TRANSACTION, - - // FBA - GET_FBA_QUORUMSET, - FBA_QUORUMSET, - FBA_MESSAGE - }; - """ - ERROR_MSG = 0 - HELLO = 1 - DONT_HAVE = 2 - GET_PEERS = 3 - PEERS = 4 - GET_TX_SET = 5 - TX_SET = 6 - GET_VALIDATIONS = 7 - VALIDATIONS = 8 - TRANSACTION = 9 - JSON_TRANSACTION = 10 - GET_FBA_QUORUMSET = 11 - FBA_QUORUMSET = 12 - FBA_MESSAGE = 13 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> MessageType: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MessageType: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MessageType: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/nesting.x/__init__.py b/spec/output/generator_spec_python/nesting.x/__init__.py deleted file mode 100644 index 6b52bfd61..000000000 --- a/spec/output/generator_spec_python/nesting.x/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .union_key import UnionKey -from .foo import Foo -from .my_union_one import MyUnionOne -from .my_union_two import MyUnionTwo -from .my_union import MyUnion diff --git a/spec/output/generator_spec_python/nesting.x/base.py b/spec/output/generator_spec_python/nesting.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/nesting.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/nesting.x/constants.py b/spec/output/generator_spec_python/nesting.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/nesting.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/nesting.x/foo.py b/spec/output/generator_spec_python/nesting.x/foo.py deleted file mode 100644 index ea20e232b..000000000 --- a/spec/output/generator_spec_python/nesting.x/foo.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Foo'] -class Foo: - """ - XDR Source Code:: - - typedef int Foo; - """ - def __init__(self, foo: int) -> None: - self.foo = foo - def pack(self, packer: Packer) -> None: - Integer(self.foo).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Foo: - foo = Integer.unpack(unpacker) - return cls(foo) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Foo: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Foo: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.foo) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.foo == other.foo - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/nesting.x/my_union.py b/spec/output/generator_spec_python/nesting.x/my_union.py deleted file mode 100644 index b166332a5..000000000 --- a/spec/output/generator_spec_python/nesting.x/my_union.py +++ /dev/null @@ -1,101 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .union_key import UnionKey -from .my_union_one import MyUnionOne -from .my_union_two import MyUnionTwo -__all__ = ['MyUnion'] -class MyUnion: - """ - XDR Source Code:: - - union MyUnion switch (UnionKey type) - { - case ONE: - struct { - int someInt; - } one; - - case TWO: - struct { - int someInt; - Foo foo; - } two; - - case OFFER: - void; - }; - """ - def __init__( - self, - type: UnionKey, - one: MyUnionOne = None, - two: MyUnionTwo = None, - ) -> None: - self.type = type - self.one = one - self.two = two - def pack(self, packer: Packer) -> None: - self.type.pack(packer) - if self.type == UnionKey.ONE: - if self.one is None: - raise ValueError("one should not be None.") - self.one.pack(packer) - return - if self.type == UnionKey.TWO: - if self.two is None: - raise ValueError("two should not be None.") - self.two.pack(packer) - return - if self.type == UnionKey.OFFER: - return - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyUnion: - type = UnionKey.unpack(unpacker) - if type == UnionKey.ONE: - one = MyUnionOne.unpack(unpacker) - return cls(type=type, one=one) - if type == UnionKey.TWO: - two = MyUnionTwo.unpack(unpacker) - return cls(type=type, two=two) - if type == UnionKey.OFFER: - return cls(type=type) - return cls(type=type) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyUnion: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyUnion: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.type, self.one, self.two,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.type== other.type and self.one== other.one and self.two== other.two - def __str__(self): - out = [] - out.append(f'type={self.type}') - out.append(f'one={self.one}') if self.one is not None else None - out.append(f'two={self.two}') if self.two is not None else None - return f"" diff --git a/spec/output/generator_spec_python/nesting.x/my_union_one.py b/spec/output/generator_spec_python/nesting.x/my_union_one.py deleted file mode 100644 index 38b02f939..000000000 --- a/spec/output/generator_spec_python/nesting.x/my_union_one.py +++ /dev/null @@ -1,62 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['MyUnionOne'] -class MyUnionOne: - """ - XDR Source Code:: - - struct { - int someInt; - } - """ - def __init__( - self, - some_int: int, - ) -> None: - self.some_int = some_int - def pack(self, packer: Packer) -> None: - Integer(self.some_int).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyUnionOne: - some_int = Integer.unpack(unpacker) - return cls( - some_int=some_int, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyUnionOne: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyUnionOne: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.some_int,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.some_int== other.some_int - def __str__(self): - out = [ - f'some_int={self.some_int}', - ] - return f"" diff --git a/spec/output/generator_spec_python/nesting.x/my_union_two.py b/spec/output/generator_spec_python/nesting.x/my_union_two.py deleted file mode 100644 index f9e870d00..000000000 --- a/spec/output/generator_spec_python/nesting.x/my_union_two.py +++ /dev/null @@ -1,70 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .foo import Foo -__all__ = ['MyUnionTwo'] -class MyUnionTwo: - """ - XDR Source Code:: - - struct { - int someInt; - Foo foo; - } - """ - def __init__( - self, - some_int: int, - foo: Foo, - ) -> None: - self.some_int = some_int - self.foo = foo - def pack(self, packer: Packer) -> None: - Integer(self.some_int).pack(packer) - self.foo.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyUnionTwo: - some_int = Integer.unpack(unpacker) - foo = Foo.unpack(unpacker) - return cls( - some_int=some_int, - foo=foo, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyUnionTwo: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyUnionTwo: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.some_int, self.foo,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.some_int== other.some_int and self.foo== other.foo - def __str__(self): - out = [ - f'some_int={self.some_int}', - f'foo={self.foo}', - ] - return f"" diff --git a/spec/output/generator_spec_python/nesting.x/union_key.py b/spec/output/generator_spec_python/nesting.x/union_key.py deleted file mode 100644 index 9348565b9..000000000 --- a/spec/output/generator_spec_python/nesting.x/union_key.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['UnionKey'] -class UnionKey(IntEnum): - """ - XDR Source Code:: - - enum UnionKey { - ONE = 1, - TWO = 2, - OFFER = 3 - }; - """ - ONE = 1 - TWO = 2 - OFFER = 3 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> UnionKey: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> UnionKey: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> UnionKey: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/optional.x/__init__.py b/spec/output/generator_spec_python/optional.x/__init__.py deleted file mode 100644 index 281f0850a..000000000 --- a/spec/output/generator_spec_python/optional.x/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .arr import Arr -from .has_options import HasOptions diff --git a/spec/output/generator_spec_python/optional.x/arr.py b/spec/output/generator_spec_python/optional.x/arr.py deleted file mode 100644 index f5cbcb140..000000000 --- a/spec/output/generator_spec_python/optional.x/arr.py +++ /dev/null @@ -1,60 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Arr'] -class Arr: - """ - XDR Source Code:: - - typedef int Arr[2]; - """ - def __init__(self, arr: List[int]) -> None: - _expect_length = 2 - if arr and len(arr) != _expect_length: - raise ValueError(f"The length of `arr` should be {_expect_length}, but got {len(arr)}.") - self.arr = arr - def pack(self, packer: Packer) -> None: - for arr_item in self.arr: - Integer(arr_item).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Arr: - length = 2 - arr = [] - for _ in range(length): - arr.append(Integer.unpack(unpacker)) - return cls(arr) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Arr: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Arr: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.arr) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.arr == other.arr - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/optional.x/base.py b/spec/output/generator_spec_python/optional.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/optional.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/optional.x/constants.py b/spec/output/generator_spec_python/optional.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/optional.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/optional.x/has_options.py b/spec/output/generator_spec_python/optional.x/has_options.py deleted file mode 100644 index 1fb4727a4..000000000 --- a/spec/output/generator_spec_python/optional.x/has_options.py +++ /dev/null @@ -1,90 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .arr import Arr -__all__ = ['HasOptions'] -class HasOptions: - """ - XDR Source Code:: - - struct HasOptions - { - int* firstOption; - int *secondOption; - Arr *thirdOption; - }; - """ - def __init__( - self, - first_option: Optional[int], - second_option: Optional[int], - third_option: Optional[Arr], - ) -> None: - self.first_option = first_option - self.second_option = second_option - self.third_option = third_option - def pack(self, packer: Packer) -> None: - if self.first_option is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - Integer(self.first_option).pack(packer) - if self.second_option is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - Integer(self.second_option).pack(packer) - if self.third_option is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - self.third_option.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> HasOptions: - first_option = Integer.unpack(unpacker) if unpacker.unpack_uint() else None - second_option = Integer.unpack(unpacker) if unpacker.unpack_uint() else None - third_option = Arr.unpack(unpacker) if unpacker.unpack_uint() else None - return cls( - first_option=first_option, - second_option=second_option, - third_option=third_option, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> HasOptions: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> HasOptions: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.first_option, self.second_option, self.third_option,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.first_option== other.first_option and self.second_option== other.second_option and self.third_option== other.third_option - def __str__(self): - out = [ - f'first_option={self.first_option}', - f'second_option={self.second_option}', - f'third_option={self.third_option}', - ] - return f"" diff --git a/spec/output/generator_spec_python/struct.x/__init__.py b/spec/output/generator_spec_python/struct.x/__init__.py deleted file mode 100644 index 58197c7ed..000000000 --- a/spec/output/generator_spec_python/struct.x/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .int64 import Int64 -from .my_struct import MyStruct diff --git a/spec/output/generator_spec_python/struct.x/base.py b/spec/output/generator_spec_python/struct.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/struct.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/struct.x/constants.py b/spec/output/generator_spec_python/struct.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/struct.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/struct.x/int64.py b/spec/output/generator_spec_python/struct.x/int64.py deleted file mode 100644 index 263c23a88..000000000 --- a/spec/output/generator_spec_python/struct.x/int64.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Int64'] -class Int64: - """ - XDR Source Code:: - - typedef hyper int64; - """ - def __init__(self, int64: int) -> None: - self.int64 = int64 - def pack(self, packer: Packer) -> None: - Hyper(self.int64).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Int64: - int64 = Hyper.unpack(unpacker) - return cls(int64) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Int64: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Int64: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int64) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int64 == other.int64 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/struct.x/my_struct.py b/spec/output/generator_spec_python/struct.x/my_struct.py deleted file mode 100644 index ba085afbc..000000000 --- a/spec/output/generator_spec_python/struct.x/my_struct.py +++ /dev/null @@ -1,92 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .int64 import Int64 -__all__ = ['MyStruct'] -class MyStruct: - """ - XDR Source Code:: - - struct MyStruct - { - int someInt; - int64 aBigInt; - opaque someOpaque[10]; - string someString<>; - string maxString<100>; - }; - """ - def __init__( - self, - some_int: int, - a_big_int: Int64, - some_opaque: bytes, - some_string: bytes, - max_string: bytes, - ) -> None: - self.some_int = some_int - self.a_big_int = a_big_int - self.some_opaque = some_opaque - self.some_string = some_string - self.max_string = max_string - def pack(self, packer: Packer) -> None: - Integer(self.some_int).pack(packer) - self.a_big_int.pack(packer) - Opaque(self.some_opaque, 10, True).pack(packer) - String(self.some_string, 4294967295).pack(packer) - String(self.max_string, 100).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyStruct: - some_int = Integer.unpack(unpacker) - a_big_int = Int64.unpack(unpacker) - some_opaque = Opaque.unpack(unpacker, 10, True) - some_string = String.unpack(unpacker) - max_string = String.unpack(unpacker) - return cls( - some_int=some_int, - a_big_int=a_big_int, - some_opaque=some_opaque, - some_string=some_string, - max_string=max_string, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyStruct: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyStruct: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.some_int, self.a_big_int, self.some_opaque, self.some_string, self.max_string,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.some_int== other.some_int and self.a_big_int== other.a_big_int and self.some_opaque== other.some_opaque and self.some_string== other.some_string and self.max_string== other.max_string - def __str__(self): - out = [ - f'some_int={self.some_int}', - f'a_big_int={self.a_big_int}', - f'some_opaque={self.some_opaque}', - f'some_string={self.some_string}', - f'max_string={self.max_string}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/__init__.py b/spec/output/generator_spec_python/test.x/__init__.py deleted file mode 100644 index b4a928b4d..000000000 --- a/spec/output/generator_spec_python/test.x/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .uint512 import Uint512 -from .uint513 import Uint513 -from .uint514 import Uint514 -from .str import Str -from .str2 import Str2 -from .hash import Hash -from .hashes1 import Hashes1 -from .hashes2 import Hashes2 -from .hashes3 import Hashes3 -from .opt_hash1 import OptHash1 -from .opt_hash2 import OptHash2 -from .int1 import Int1 -from .int2 import Int2 -from .int3 import Int3 -from .int4 import Int4 -from .my_struct import MyStruct -from .lots_of_my_structs import LotsOfMyStructs -from .has_stuff import HasStuff -from .color import Color -from .nester_nested_enum import NesterNestedEnum -from .nester_nested_struct import NesterNestedStruct -from .nester_nested_union import NesterNestedUnion -from .nester import Nester diff --git a/spec/output/generator_spec_python/test.x/base.py b/spec/output/generator_spec_python/test.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/test.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/color.py b/spec/output/generator_spec_python/test.x/color.py deleted file mode 100644 index 094d8efdb..000000000 --- a/spec/output/generator_spec_python/test.x/color.py +++ /dev/null @@ -1,50 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Color'] -class Color(IntEnum): - """ - XDR Source Code:: - - enum Color { - RED, - BLUE = 5, - GREEN - }; - """ - RED = 0 - BLUE = 5 - GREEN = 6 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> Color: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Color: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Color: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/test.x/constants.py b/spec/output/generator_spec_python/test.x/constants.py deleted file mode 100644 index ec63e1e0d..000000000 --- a/spec/output/generator_spec_python/test.x/constants.py +++ /dev/null @@ -1,6 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -#: const FOO = 1244; -FOO: int = 1244 -#: const BAR = FOO; -BAR: int = FOO diff --git a/spec/output/generator_spec_python/test.x/has_stuff.py b/spec/output/generator_spec_python/test.x/has_stuff.py deleted file mode 100644 index 047cfbac7..000000000 --- a/spec/output/generator_spec_python/test.x/has_stuff.py +++ /dev/null @@ -1,64 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .lots_of_my_structs import LotsOfMyStructs -__all__ = ['HasStuff'] -class HasStuff: - """ - XDR Source Code:: - - struct HasStuff - { - LotsOfMyStructs data; - }; - """ - def __init__( - self, - data: LotsOfMyStructs, - ) -> None: - self.data = data - def pack(self, packer: Packer) -> None: - self.data.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> HasStuff: - data = LotsOfMyStructs.unpack(unpacker) - return cls( - data=data, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> HasStuff: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> HasStuff: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.data,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.data== other.data - def __str__(self): - out = [ - f'data={self.data}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/hash.py b/spec/output/generator_spec_python/test.x/hash.py deleted file mode 100644 index e4f98a726..000000000 --- a/spec/output/generator_spec_python/test.x/hash.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Hash'] -class Hash: - """ - XDR Source Code:: - - typedef opaque Hash[32]; - """ - def __init__(self, hash: bytes) -> None: - self.hash = hash - def pack(self, packer: Packer) -> None: - Opaque(self.hash, 32, True).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Hash: - hash = Opaque.unpack(unpacker, 32, True) - return cls(hash) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Hash: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Hash: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.hash) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.hash == other.hash - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/hashes1.py b/spec/output/generator_spec_python/test.x/hashes1.py deleted file mode 100644 index e9ac423b4..000000000 --- a/spec/output/generator_spec_python/test.x/hashes1.py +++ /dev/null @@ -1,61 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .hash import Hash -__all__ = ['Hashes1'] -class Hashes1: - """ - XDR Source Code:: - - typedef Hash Hashes1[12]; - """ - def __init__(self, hashes1: List[Hash]) -> None: - _expect_length = 12 - if hashes1 and len(hashes1) != _expect_length: - raise ValueError(f"The length of `hashes1` should be {_expect_length}, but got {len(hashes1)}.") - self.hashes1 = hashes1 - def pack(self, packer: Packer) -> None: - for hashes1_item in self.hashes1: - hashes1_item.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Hashes1: - length = 12 - hashes1 = [] - for _ in range(length): - hashes1.append(Hash.unpack(unpacker)) - return cls(hashes1) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Hashes1: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Hashes1: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.hashes1) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.hashes1 == other.hashes1 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/hashes2.py b/spec/output/generator_spec_python/test.x/hashes2.py deleted file mode 100644 index 907128be7..000000000 --- a/spec/output/generator_spec_python/test.x/hashes2.py +++ /dev/null @@ -1,62 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .hash import Hash -__all__ = ['Hashes2'] -class Hashes2: - """ - XDR Source Code:: - - typedef Hash Hashes2<12>; - """ - def __init__(self, hashes2: List[Hash]) -> None: - _expect_max_length = 12 - if hashes2 and len(hashes2) > _expect_max_length: - raise ValueError(f"The maximum length of `hashes2` should be {_expect_max_length}, but got {len(hashes2)}.") - self.hashes2 = hashes2 - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.hashes2)) - for hashes2_item in self.hashes2: - hashes2_item.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Hashes2: - length = unpacker.unpack_uint() - hashes2 = [] - for _ in range(length): - hashes2.append(Hash.unpack(unpacker)) - return cls(hashes2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Hashes2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Hashes2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.hashes2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.hashes2 == other.hashes2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/hashes3.py b/spec/output/generator_spec_python/test.x/hashes3.py deleted file mode 100644 index 1adced2d8..000000000 --- a/spec/output/generator_spec_python/test.x/hashes3.py +++ /dev/null @@ -1,62 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .hash import Hash -__all__ = ['Hashes3'] -class Hashes3: - """ - XDR Source Code:: - - typedef Hash Hashes3<>; - """ - def __init__(self, hashes3: List[Hash]) -> None: - _expect_max_length = 4294967295 - if hashes3 and len(hashes3) > _expect_max_length: - raise ValueError(f"The maximum length of `hashes3` should be {_expect_max_length}, but got {len(hashes3)}.") - self.hashes3 = hashes3 - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.hashes3)) - for hashes3_item in self.hashes3: - hashes3_item.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Hashes3: - length = unpacker.unpack_uint() - hashes3 = [] - for _ in range(length): - hashes3.append(Hash.unpack(unpacker)) - return cls(hashes3) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Hashes3: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Hashes3: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.hashes3) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.hashes3 == other.hashes3 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/int1.py b/spec/output/generator_spec_python/test.x/int1.py deleted file mode 100644 index ffb044eb0..000000000 --- a/spec/output/generator_spec_python/test.x/int1.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Int1'] -class Int1: - """ - XDR Source Code:: - - typedef int int1; - """ - def __init__(self, int1: int) -> None: - self.int1 = int1 - def pack(self, packer: Packer) -> None: - Integer(self.int1).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Int1: - int1 = Integer.unpack(unpacker) - return cls(int1) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Int1: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Int1: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int1) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int1 == other.int1 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/int2.py b/spec/output/generator_spec_python/test.x/int2.py deleted file mode 100644 index 3f3579a2a..000000000 --- a/spec/output/generator_spec_python/test.x/int2.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Int2'] -class Int2: - """ - XDR Source Code:: - - typedef hyper int2; - """ - def __init__(self, int2: int) -> None: - self.int2 = int2 - def pack(self, packer: Packer) -> None: - Hyper(self.int2).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Int2: - int2 = Hyper.unpack(unpacker) - return cls(int2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Int2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Int2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int2 == other.int2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/int3.py b/spec/output/generator_spec_python/test.x/int3.py deleted file mode 100644 index a5ab01bc4..000000000 --- a/spec/output/generator_spec_python/test.x/int3.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Int3'] -class Int3: - """ - XDR Source Code:: - - typedef unsigned int int3; - """ - def __init__(self, int3: int) -> None: - self.int3 = int3 - def pack(self, packer: Packer) -> None: - UnsignedInteger(self.int3).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Int3: - int3 = UnsignedInteger.unpack(unpacker) - return cls(int3) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Int3: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Int3: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int3) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int3 == other.int3 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/int4.py b/spec/output/generator_spec_python/test.x/int4.py deleted file mode 100644 index cbae0fd59..000000000 --- a/spec/output/generator_spec_python/test.x/int4.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Int4'] -class Int4: - """ - XDR Source Code:: - - typedef unsigned hyper int4; - """ - def __init__(self, int4: int) -> None: - self.int4 = int4 - def pack(self, packer: Packer) -> None: - UnsignedHyper(self.int4).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Int4: - int4 = UnsignedHyper.unpack(unpacker) - return cls(int4) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Int4: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Int4: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int4) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int4 == other.int4 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/lots_of_my_structs.py b/spec/output/generator_spec_python/test.x/lots_of_my_structs.py deleted file mode 100644 index 8fbe1d405..000000000 --- a/spec/output/generator_spec_python/test.x/lots_of_my_structs.py +++ /dev/null @@ -1,72 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .my_struct import MyStruct -__all__ = ['LotsOfMyStructs'] -class LotsOfMyStructs: - """ - XDR Source Code:: - - struct LotsOfMyStructs - { - MyStruct members<>; - }; - """ - def __init__( - self, - members: List[MyStruct], - ) -> None: - _expect_max_length = 4294967295 - if members and len(members) > _expect_max_length: - raise ValueError(f"The maximum length of `members` should be {_expect_max_length}, but got {len(members)}.") - self.members = members - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.members)) - for members_item in self.members: - members_item.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> LotsOfMyStructs: - length = unpacker.unpack_uint() - members = [] - for _ in range(length): - members.append(MyStruct.unpack(unpacker)) - return cls( - members=members, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> LotsOfMyStructs: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> LotsOfMyStructs: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.members,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.members== other.members - def __str__(self): - out = [ - f'members={self.members}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/my_struct.py b/spec/output/generator_spec_python/test.x/my_struct.py deleted file mode 100644 index 9cf584141..000000000 --- a/spec/output/generator_spec_python/test.x/my_struct.py +++ /dev/null @@ -1,108 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .uint512 import Uint512 -from .opt_hash1 import OptHash1 -from .int1 import Int1 -__all__ = ['MyStruct'] -class MyStruct: - """ - XDR Source Code:: - - struct MyStruct - { - uint512 field1; - optHash1 field2; - int1 field3; - unsigned int field4; - float field5; - double field6; - bool field7; - }; - """ - def __init__( - self, - field1: Uint512, - field2: OptHash1, - field3: Int1, - field4: int, - field5: float, - field6: float, - field7: bool, - ) -> None: - self.field1 = field1 - self.field2 = field2 - self.field3 = field3 - self.field4 = field4 - self.field5 = field5 - self.field6 = field6 - self.field7 = field7 - def pack(self, packer: Packer) -> None: - self.field1.pack(packer) - self.field2.pack(packer) - self.field3.pack(packer) - UnsignedInteger(self.field4).pack(packer) - Float(self.field5).pack(packer) - Double(self.field6).pack(packer) - Boolean(self.field7).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyStruct: - field1 = Uint512.unpack(unpacker) - field2 = OptHash1.unpack(unpacker) - field3 = Int1.unpack(unpacker) - field4 = UnsignedInteger.unpack(unpacker) - field5 = Float.unpack(unpacker) - field6 = Double.unpack(unpacker) - field7 = Boolean.unpack(unpacker) - return cls( - field1=field1, - field2=field2, - field3=field3, - field4=field4, - field5=field5, - field6=field6, - field7=field7, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyStruct: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyStruct: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.field1, self.field2, self.field3, self.field4, self.field5, self.field6, self.field7,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.field1== other.field1 and self.field2== other.field2 and self.field3== other.field3 and self.field4== other.field4 and self.field5== other.field5 and self.field6== other.field6 and self.field7== other.field7 - def __str__(self): - out = [ - f'field1={self.field1}', - f'field2={self.field2}', - f'field3={self.field3}', - f'field4={self.field4}', - f'field5={self.field5}', - f'field6={self.field6}', - f'field7={self.field7}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/nester.py b/spec/output/generator_spec_python/test.x/nester.py deleted file mode 100644 index d3d3df1f1..000000000 --- a/spec/output/generator_spec_python/test.x/nester.py +++ /dev/null @@ -1,94 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .nester_nested_enum import NesterNestedEnum -from .nester_nested_struct import NesterNestedStruct -from .nester_nested_union import NesterNestedUnion -__all__ = ['Nester'] -class Nester: - """ - XDR Source Code:: - - struct Nester - { - enum { - BLAH_1, - BLAH_2 - } nestedEnum; - - struct { - int blah; - } nestedStruct; - - union switch (Color color) { - case RED: - void; - default: - int blah2; - } nestedUnion; - - - }; - """ - def __init__( - self, - nested_enum: NesterNestedEnum, - nested_struct: NesterNestedStruct, - nested_union: NesterNestedUnion, - ) -> None: - self.nested_enum = nested_enum - self.nested_struct = nested_struct - self.nested_union = nested_union - def pack(self, packer: Packer) -> None: - self.nested_enum.pack(packer) - self.nested_struct.pack(packer) - self.nested_union.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Nester: - nested_enum = NesterNestedEnum.unpack(unpacker) - nested_struct = NesterNestedStruct.unpack(unpacker) - nested_union = NesterNestedUnion.unpack(unpacker) - return cls( - nested_enum=nested_enum, - nested_struct=nested_struct, - nested_union=nested_union, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Nester: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Nester: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.nested_enum, self.nested_struct, self.nested_union,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.nested_enum== other.nested_enum and self.nested_struct== other.nested_struct and self.nested_union== other.nested_union - def __str__(self): - out = [ - f'nested_enum={self.nested_enum}', - f'nested_struct={self.nested_struct}', - f'nested_union={self.nested_union}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/nester_nested_enum.py b/spec/output/generator_spec_python/test.x/nester_nested_enum.py deleted file mode 100644 index 8087fe9a8..000000000 --- a/spec/output/generator_spec_python/test.x/nester_nested_enum.py +++ /dev/null @@ -1,48 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['NesterNestedEnum'] -class NesterNestedEnum(IntEnum): - """ - XDR Source Code:: - - enum { - BLAH_1, - BLAH_2 - } - """ - BLAH_1 = 0 - BLAH_2 = 1 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> NesterNestedEnum: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> NesterNestedEnum: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> NesterNestedEnum: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) diff --git a/spec/output/generator_spec_python/test.x/nester_nested_struct.py b/spec/output/generator_spec_python/test.x/nester_nested_struct.py deleted file mode 100644 index 52b302388..000000000 --- a/spec/output/generator_spec_python/test.x/nester_nested_struct.py +++ /dev/null @@ -1,62 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['NesterNestedStruct'] -class NesterNestedStruct: - """ - XDR Source Code:: - - struct { - int blah; - } - """ - def __init__( - self, - blah: int, - ) -> None: - self.blah = blah - def pack(self, packer: Packer) -> None: - Integer(self.blah).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> NesterNestedStruct: - blah = Integer.unpack(unpacker) - return cls( - blah=blah, - ) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> NesterNestedStruct: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> NesterNestedStruct: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.blah,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.blah== other.blah - def __str__(self): - out = [ - f'blah={self.blah}', - ] - return f"" diff --git a/spec/output/generator_spec_python/test.x/nester_nested_union.py b/spec/output/generator_spec_python/test.x/nester_nested_union.py deleted file mode 100644 index 61f5511df..000000000 --- a/spec/output/generator_spec_python/test.x/nester_nested_union.py +++ /dev/null @@ -1,74 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .color import Color -__all__ = ['NesterNestedUnion'] -class NesterNestedUnion: - """ - XDR Source Code:: - - union switch (Color color) { - case RED: - void; - default: - int blah2; - } - """ - def __init__( - self, - color: Color, - blah2: int = None, - ) -> None: - self.color = color - self.blah2 = blah2 - def pack(self, packer: Packer) -> None: - self.color.pack(packer) - if self.color == Color.RED: - return - if self.blah2 is None: - raise ValueError("blah2 should not be None.") - Integer(self.blah2).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> NesterNestedUnion: - color = Color.unpack(unpacker) - if color == Color.RED: - return cls(color=color) - blah2 = Integer.unpack(unpacker) - return cls(color=color, blah2=blah2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> NesterNestedUnion: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> NesterNestedUnion: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.color, self.blah2,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.color== other.color and self.blah2== other.blah2 - def __str__(self): - out = [] - out.append(f'color={self.color}') - out.append(f'blah2={self.blah2}') if self.blah2 is not None else None - return f"" diff --git a/spec/output/generator_spec_python/test.x/opt_hash1.py b/spec/output/generator_spec_python/test.x/opt_hash1.py deleted file mode 100644 index 17cc091a8..000000000 --- a/spec/output/generator_spec_python/test.x/opt_hash1.py +++ /dev/null @@ -1,58 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .hash import Hash -__all__ = ['OptHash1'] -class OptHash1: - """ - XDR Source Code:: - - typedef Hash *optHash1; - """ - def __init__(self, opt_hash1: Optional[Hash]) -> None: - self.opt_hash1 = opt_hash1 - def pack(self, packer: Packer) -> None: - if self.opt_hash1 is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - self.opt_hash1.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> OptHash1: - opt_hash1 = Hash.unpack(unpacker) if unpacker.unpack_uint() else None - return cls(opt_hash1) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> OptHash1: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> OptHash1: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.opt_hash1) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.opt_hash1 == other.opt_hash1 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/opt_hash2.py b/spec/output/generator_spec_python/test.x/opt_hash2.py deleted file mode 100644 index 04736dd18..000000000 --- a/spec/output/generator_spec_python/test.x/opt_hash2.py +++ /dev/null @@ -1,58 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .hash import Hash -__all__ = ['OptHash2'] -class OptHash2: - """ - XDR Source Code:: - - typedef Hash* optHash2; - """ - def __init__(self, opt_hash2: Optional[Hash]) -> None: - self.opt_hash2 = opt_hash2 - def pack(self, packer: Packer) -> None: - if self.opt_hash2 is None: - packer.pack_uint(0) - else: - packer.pack_uint(1) - self.opt_hash2.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> OptHash2: - opt_hash2 = Hash.unpack(unpacker) if unpacker.unpack_uint() else None - return cls(opt_hash2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> OptHash2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> OptHash2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.opt_hash2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.opt_hash2 == other.opt_hash2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/str.py b/spec/output/generator_spec_python/test.x/str.py deleted file mode 100644 index 18088dd5d..000000000 --- a/spec/output/generator_spec_python/test.x/str.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Str'] -class Str: - """ - XDR Source Code:: - - typedef string str<64>; - """ - def __init__(self, str: bytes) -> None: - self.str = str - def pack(self, packer: Packer) -> None: - String(self.str, 64).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Str: - str = String.unpack(unpacker) - return cls(str) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Str: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Str: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.str) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.str == other.str - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/str2.py b/spec/output/generator_spec_python/test.x/str2.py deleted file mode 100644 index f671ee574..000000000 --- a/spec/output/generator_spec_python/test.x/str2.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Str2'] -class Str2: - """ - XDR Source Code:: - - typedef string str2<>; - """ - def __init__(self, str2: bytes) -> None: - self.str2 = str2 - def pack(self, packer: Packer) -> None: - String(self.str2, 4294967295).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Str2: - str2 = String.unpack(unpacker) - return cls(str2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Str2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Str2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.str2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.str2 == other.str2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/uint512.py b/spec/output/generator_spec_python/test.x/uint512.py deleted file mode 100644 index 70922e63a..000000000 --- a/spec/output/generator_spec_python/test.x/uint512.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Uint512'] -class Uint512: - """ - XDR Source Code:: - - typedef opaque uint512[64]; - """ - def __init__(self, uint512: bytes) -> None: - self.uint512 = uint512 - def pack(self, packer: Packer) -> None: - Opaque(self.uint512, 64, True).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Uint512: - uint512 = Opaque.unpack(unpacker, 64, True) - return cls(uint512) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Uint512: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Uint512: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.uint512) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.uint512 == other.uint512 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/uint513.py b/spec/output/generator_spec_python/test.x/uint513.py deleted file mode 100644 index 2362e303a..000000000 --- a/spec/output/generator_spec_python/test.x/uint513.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Uint513'] -class Uint513: - """ - XDR Source Code:: - - typedef opaque uint513<64>; - """ - def __init__(self, uint513: bytes) -> None: - self.uint513 = uint513 - def pack(self, packer: Packer) -> None: - Opaque(self.uint513, 64, False).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Uint513: - uint513 = Opaque.unpack(unpacker, 64, False) - return cls(uint513) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Uint513: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Uint513: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.uint513) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.uint513 == other.uint513 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/test.x/uint514.py b/spec/output/generator_spec_python/test.x/uint514.py deleted file mode 100644 index be037f25d..000000000 --- a/spec/output/generator_spec_python/test.x/uint514.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Uint514'] -class Uint514: - """ - XDR Source Code:: - - typedef opaque uint514<>; - """ - def __init__(self, uint514: bytes) -> None: - self.uint514 = uint514 - def pack(self, packer: Packer) -> None: - Opaque(self.uint514, 4294967295, False).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Uint514: - uint514 = Opaque.unpack(unpacker, 4294967295, False) - return cls(uint514) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Uint514: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Uint514: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.uint514) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.uint514 == other.uint514 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/union.x/__init__.py b/spec/output/generator_spec_python/union.x/__init__.py deleted file mode 100644 index 605ecd0bf..000000000 --- a/spec/output/generator_spec_python/union.x/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -# Automatically generated by xdrgen -# DO NOT EDIT or your changes may be overwritten -from .base import * -from .constants import * -from .error import Error -from .multi import Multi -from .union_key import UnionKey -from .my_union import MyUnion -from .int_union import IntUnion -from .int_union2 import IntUnion2 diff --git a/spec/output/generator_spec_python/union.x/base.py b/spec/output/generator_spec_python/union.x/base.py deleted file mode 100644 index 65179b8f3..000000000 --- a/spec/output/generator_spec_python/union.x/base.py +++ /dev/null @@ -1,252 +0,0 @@ -from xdrlib3 import Packer, Unpacker - -__all__ = [ - "Integer", - "UnsignedInteger", - "Float", - "Double", - "Hyper", - "UnsignedHyper", - "Boolean", - "String", - "Opaque", -] - - -class Integer: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_int() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedInteger: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uint(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uint() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Float: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_float(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_float() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Double: - def __init__(self, value: float) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_double(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> float: - return unpacker.unpack_double() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Hyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_hyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_hyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class UnsignedHyper: - def __init__(self, value: int) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_uhyper(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> int: - return unpacker.unpack_uhyper() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class Boolean: - def __init__(self, value: bool) -> None: - self.value = value - - def pack(self, packer: Packer) -> None: - packer.pack_bool(self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bool: - return unpacker.unpack_bool() - - def __hash__(self): - return hash(self.value) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value - - def __str__(self): - return f"" - - -class String: - def __init__(self, value: bytes, size: int) -> None: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.size = len(value) - - def pack(self, packer: Packer) -> None: - packer.pack_uint(len(self.value)) - packer.pack_fopaque(len(self.value), self.value) - - @staticmethod - def unpack(unpacker: Unpacker) -> bytes: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return self.value == other.value and self.size == other.size - - def __str__(self): - return f"" - - -class Opaque: - def __init__(self, value: bytes, size: int, fixed: bool) -> None: - if fixed: - if len(value) != size: - raise ValueError( - f"The length of `value` should be {size}, but got {len(value)}." - ) - else: - if len(value) > size: - raise ValueError( - f"The maximum length of `value` should be {size}, but got {len(value)}." - ) - - self.value = value - self.fixed = fixed - self.size = len(value) - - def pack(self, packer: Packer) -> None: - if not self.fixed: - size = len(self.value) - packer.pack_uint(size) - else: - size = self.size - packer.pack_fopaque(size, self.value) - - @staticmethod - def unpack(unpacker: Unpacker, size: int, fixed: bool) -> bytes: - if not fixed: - size = unpacker.unpack_uint() - return unpacker.unpack_fopaque(size) - - def __hash__(self): - return hash((self.value, self.size, self.fixed)) - - def __eq__(self, other: object) -> bool: - if not isinstance(other, self.__class__): - return NotImplemented - return ( - self.value == other.value - and self.fixed == other.fixed - and self.size == other.size - ) - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/union.x/constants.py b/spec/output/generator_spec_python/union.x/constants.py deleted file mode 100644 index c83841ad9..000000000 --- a/spec/output/generator_spec_python/union.x/constants.py +++ /dev/null @@ -1,2 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten diff --git a/spec/output/generator_spec_python/union.x/error.py b/spec/output/generator_spec_python/union.x/error.py deleted file mode 100644 index 3f68c92a1..000000000 --- a/spec/output/generator_spec_python/union.x/error.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Error'] -class Error: - """ - XDR Source Code:: - - typedef int Error; - """ - def __init__(self, error: int) -> None: - self.error = error - def pack(self, packer: Packer) -> None: - Integer(self.error).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Error: - error = Integer.unpack(unpacker) - return cls(error) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Error: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Error: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.error) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.error == other.error - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/union.x/int_union.py b/spec/output/generator_spec_python/union.x/int_union.py deleted file mode 100644 index 43fb8512f..000000000 --- a/spec/output/generator_spec_python/union.x/int_union.py +++ /dev/null @@ -1,96 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .error import Error -from .multi import Multi -__all__ = ['IntUnion'] -class IntUnion: - """ - XDR Source Code:: - - union IntUnion switch (int type) - { - case 0: - Error error; - case 1: - Multi things<>; - - }; - """ - def __init__( - self, - type: int, - error: Error = None, - things: List[Multi] = None, - ) -> None: - _expect_max_length = 4294967295 - if things and len(things) > _expect_max_length: - raise ValueError(f"The maximum length of `things` should be {_expect_max_length}, but got {len(things)}.") - self.type = type - self.error = error - self.things = things - def pack(self, packer: Packer) -> None: - Integer(self.type).pack(packer) - if self.type == 0: - if self.error is None: - raise ValueError("error should not be None.") - self.error.pack(packer) - return - if self.type == 1: - if self.things is None: - raise ValueError("things should not be None.") - packer.pack_uint(len(self.things)) - for things_item in self.things: - things_item.pack(packer) - return - @classmethod - def unpack(cls, unpacker: Unpacker) -> IntUnion: - type = Integer.unpack(unpacker) - if type == 0: - error = Error.unpack(unpacker) - return cls(type=type, error=error) - if type == 1: - length = unpacker.unpack_uint() - things = [] - for _ in range(length): - things.append(Multi.unpack(unpacker)) - return cls(type=type, things=things) - return cls(type=type) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> IntUnion: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> IntUnion: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.type, self.error, self.things,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.type== other.type and self.error== other.error and self.things== other.things - def __str__(self): - out = [] - out.append(f'type={self.type}') - out.append(f'error={self.error}') if self.error is not None else None - out.append(f'things={self.things}') if self.things is not None else None - return f"" diff --git a/spec/output/generator_spec_python/union.x/int_union2.py b/spec/output/generator_spec_python/union.x/int_union2.py deleted file mode 100644 index 2d9d6bafa..000000000 --- a/spec/output/generator_spec_python/union.x/int_union2.py +++ /dev/null @@ -1,54 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .int_union import IntUnion -__all__ = ['IntUnion2'] -class IntUnion2: - """ - XDR Source Code:: - - typedef IntUnion IntUnion2; - """ - def __init__(self, int_union2: IntUnion) -> None: - self.int_union2 = int_union2 - def pack(self, packer: Packer) -> None: - self.int_union2.pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> IntUnion2: - int_union2 = IntUnion.unpack(unpacker) - return cls(int_union2) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> IntUnion2: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> IntUnion2: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.int_union2) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.int_union2 == other.int_union2 - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/union.x/multi.py b/spec/output/generator_spec_python/union.x/multi.py deleted file mode 100644 index fe9ec8cf2..000000000 --- a/spec/output/generator_spec_python/union.x/multi.py +++ /dev/null @@ -1,53 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['Multi'] -class Multi: - """ - XDR Source Code:: - - typedef int Multi; - """ - def __init__(self, multi: int) -> None: - self.multi = multi - def pack(self, packer: Packer) -> None: - Integer(self.multi).pack(packer) - @classmethod - def unpack(cls, unpacker: Unpacker) -> Multi: - multi = Integer.unpack(unpacker) - return cls(multi) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> Multi: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> Multi: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash(self.multi) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.multi == other.multi - - def __str__(self): - return f"" diff --git a/spec/output/generator_spec_python/union.x/my_union.py b/spec/output/generator_spec_python/union.x/my_union.py deleted file mode 100644 index c6f2b7b38..000000000 --- a/spec/output/generator_spec_python/union.x/my_union.py +++ /dev/null @@ -1,98 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -from .union_key import UnionKey -from .error import Error -from .multi import Multi -__all__ = ['MyUnion'] -class MyUnion: - """ - XDR Source Code:: - - union MyUnion switch (UnionKey type) - { - case ERROR: - Error error; - case MULTI: - Multi things<>; - - - }; - """ - def __init__( - self, - type: UnionKey, - error: Error = None, - things: List[Multi] = None, - ) -> None: - _expect_max_length = 4294967295 - if things and len(things) > _expect_max_length: - raise ValueError(f"The maximum length of `things` should be {_expect_max_length}, but got {len(things)}.") - self.type = type - self.error = error - self.things = things - def pack(self, packer: Packer) -> None: - self.type.pack(packer) - if self.type == UnionKey.ERROR: - if self.error is None: - raise ValueError("error should not be None.") - self.error.pack(packer) - return - if self.type == UnionKey.MULTI: - if self.things is None: - raise ValueError("things should not be None.") - packer.pack_uint(len(self.things)) - for things_item in self.things: - things_item.pack(packer) - return - @classmethod - def unpack(cls, unpacker: Unpacker) -> MyUnion: - type = UnionKey.unpack(unpacker) - if type == UnionKey.ERROR: - error = Error.unpack(unpacker) - return cls(type=type, error=error) - if type == UnionKey.MULTI: - length = unpacker.unpack_uint() - things = [] - for _ in range(length): - things.append(Multi.unpack(unpacker)) - return cls(type=type, things=things) - return cls(type=type) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> MyUnion: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> MyUnion: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes) - def __hash__(self): - return hash((self.type, self.error, self.things,)) - def __eq__(self, other: object): - if not isinstance(other, self.__class__): - return NotImplemented - return self.type== other.type and self.error== other.error and self.things== other.things - def __str__(self): - out = [] - out.append(f'type={self.type}') - out.append(f'error={self.error}') if self.error is not None else None - out.append(f'things={self.things}') if self.things is not None else None - return f"" diff --git a/spec/output/generator_spec_python/union.x/union_key.py b/spec/output/generator_spec_python/union.x/union_key.py deleted file mode 100644 index 7184843e6..000000000 --- a/spec/output/generator_spec_python/union.x/union_key.py +++ /dev/null @@ -1,48 +0,0 @@ -# This is an automatically generated file. -# DO NOT EDIT or your changes may be overwritten -from __future__ import annotations - -import base64 -from enum import IntEnum -from typing import List, Optional, TYPE_CHECKING -from xdrlib3 import Packer, Unpacker -from .base import Integer, UnsignedInteger, Float, Double, Hyper, UnsignedHyper, Boolean, String, Opaque -from .constants import * - -__all__ = ['UnionKey'] -class UnionKey(IntEnum): - """ - XDR Source Code:: - - enum UnionKey { - ERROR, - MULTI - }; - """ - ERROR = 0 - MULTI = 1 - def pack(self, packer: Packer) -> None: - packer.pack_int(self.value) - - @classmethod - def unpack(cls, unpacker: Unpacker) -> UnionKey: - value = unpacker.unpack_int() - return cls(value) - def to_xdr_bytes(self) -> bytes: - packer = Packer() - self.pack(packer) - return packer.get_buffer() - - @classmethod - def from_xdr_bytes(cls, xdr: bytes) -> UnionKey: - unpacker = Unpacker(xdr) - return cls.unpack(unpacker) - - def to_xdr(self) -> str: - xdr_bytes = self.to_xdr_bytes() - return base64.b64encode(xdr_bytes).decode() - - @classmethod - def from_xdr(cls, xdr: str) -> UnionKey: - xdr_bytes = base64.b64decode(xdr.encode()) - return cls.from_xdr_bytes(xdr_bytes)