Skip to content

Commit b18ffc6

Browse files
committed
Fix #43
In #private_to_h, do not take root element when my_element is a Model, but transform the model itsel to a Hash. Then, replace upper key in Hash by model name in current model.
1 parent 8ce5e1f commit b18ffc6

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/rasn1/model.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,9 @@ def generate_base_element(elt, opts)
638638
# @author adfoster-r7
639639
def private_to_h(element=nil) # rubocop:disable Metrics/CyclomaticComplexity
640640
my_element = element || root
641-
my_element = my_element.root if my_element.is_a?(Model)
642641
value = case my_element
642+
when Model
643+
model_to_h(my_element)
643644
when Types::SequenceOf
644645
sequence_of_to_h(my_element)
645646
when Types::Sequence
@@ -658,6 +659,11 @@ def private_to_h(element=nil) # rubocop:disable Metrics/CyclomaticComplexity
658659
end
659660
end
660661

662+
def model_to_h(elt)
663+
hsh = elt.to_h
664+
{ @elements.key(elt) => hsh[hsh.keys.first] }
665+
end
666+
661667
def sequence_of_to_h(elt)
662668
if elt.of_type < Model
663669
elt.value&.map { |el| el.to_h.values.first }
@@ -672,8 +678,7 @@ def sequence_to_h(seq)
672678

673679
case el
674680
when Model
675-
hsh = el.to_h
676-
[@elements.key(el), hsh[hsh.keys.first]]
681+
model_to_h(el).to_a[0]
677682
when Wrapper
678683
[unwrap_keyname(@elements.key(el)), wrapper_to_h(el)]
679684
else

0 commit comments

Comments
 (0)