Skip to content

Commit

Permalink
Support for standard type union discriminant with constant value (#44)
Browse files Browse the repository at this point in the history
`Union.resolved_case` doesn't support unions with standard type
discriminant and constant (`const`) values, ex:

```xdr
enum StellarValueType
{
    STELLAR_VALUE_BASIC = 0,
    STELLAR_VALUE_SIGNED = 1
};

union switch (int v)
{
case STELLAR_VALUE_BASIC:
    void;
    ...
```

To fix this, we are searching for identifier in namespace's enum
constants if discriminant type has not been found.
  • Loading branch information
bartekn authored Jun 5, 2019
1 parent 1be840e commit 2717d40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
23 changes: 21 additions & 2 deletions lib/xdrgen/ast/definitions/union.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,27 @@ class Union < Base
end

def resolved_case(kase)
found = discriminant_type.members.find{|m| m.name == kase.value_s}
raise "Case error: #{kase} is not a member of #{discriminant_type.name}" if found.nil?
if discriminant_type.nil? then
# discriminant_type has not been found we need to search for the value in namespace's enum constants.
# It's a case where union discriminant is a standard type (like `int`):
#
# enum StellarValueType
# {
# STELLAR_VALUE_BASIC = 0,
# STELLAR_VALUE_SIGNED = 1
# };
#
# union switch (int v)
# {
# case STELLAR_VALUE_BASIC:
# void;
# ...
found = namespace.find_enum_value(kase.value_s)
raise "Case error: #{kase} (#{kase.value_s}) constant not found" if found.nil?
else
found = discriminant_type.members.find{|m| m.name == kase.value_s}
raise "Case error: #{kase} is not a member of #{discriminant_type.name}" if found.nil?
end
found
end

Expand Down
6 changes: 5 additions & 1 deletion lib/xdrgen/generators/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ def switch_for(out, union, ident)

value = if c.value.is_a?(AST::Identifier)
member = union.resolved_case(c)
"#{name union.discriminant_type}#{name member}"
if union.discriminant_type.nil? then
"int32(#{name member.enum}#{name member})"
else
"#{name union.discriminant_type}#{name member}"
end
else
c.value.text_value
end
Expand Down
14 changes: 12 additions & 2 deletions lib/xdrgen/generators/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ def render_union(union, out)
else
arm.cases.each do |kase|
if kase.value.is_a?(AST::Identifier)
out.puts "case #{kase.value.name}:"
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
Expand Down Expand Up @@ -285,7 +290,12 @@ def render_union(union, out)
else
arm.cases.each do |kase|
if kase.value.is_a?(AST::Identifier)
out.puts "case #{kase.value.name}:"
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
Expand Down
7 changes: 6 additions & 1 deletion lib/xdrgen/generators/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ def render_union(out, union)

arm.cases.each do |acase|
switch = if acase.value.is_a?(AST::Identifier)
'"' + member_name(acase.value) + '"'
if union.discriminant.type.is_a?(AST::Typespecs::Int)
member = union.resolved_case(acase)
"#{member.value}"
else
'"' + member_name(acase.value) + '"'
end
else
acase.value.text_value
end
Expand Down

0 comments on commit 2717d40

Please sign in to comment.