Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decoding of a struct nested into an array #85

Merged
merged 2 commits into from
Mar 25, 2024

Conversation

alexejk
Copy link
Owner

@alexejk alexejk commented Mar 25, 2024

Fixes an issues where responses that have an outer array with a nested struct and other mixed types would not be decoded correctly, throwing a panic.

Example response:

<methodResponse>
  <params>
    <param>
      <value>
        <array>
          <data>
            <value>
              <i4>200</i4>
            </value>
            <value>OK</value>
            <value>
              <struct>
                <member>
                  <name>status</name>
                  <value>OK</value>
                </member>
                <member>
                  <name>contact</name>
                  <value>&lt;sip:raf@192.168.164.128:5060&gt;;expires=60</value>
                </member>
              </struct>
            </value>
          </data>
        </array>
      </value>
    </param>
  </params>
</methodResponse>

Correct way of mapping this response is to

struct{
    Array []any
}

Main issue with decoding this response prior to this PR, would be that any in array type is not assignable as is with decoded types.
This PR changes the logic to detect if an interface is mapped when attempting to decode a <struct> and assign a map[string]any to the field instead.

Fixes #84

Copy link

sonarcloud bot commented Mar 25, 2024

Copy link

codecov bot commented Mar 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.91%. Comparing base (96e03e7) to head (1f615e0).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
+ Coverage   77.52%   77.91%   +0.39%     
==========================================
  Files           7        7              
  Lines         565      575      +10     
==========================================
+ Hits          438      448      +10     
  Misses         96       96              
  Partials       31       31              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alexejk alexejk added the bug Something isn't working label Mar 25, 2024
@alexejk alexejk merged commit dd87aa3 into master Mar 25, 2024
4 checks passed
@alexejk alexejk deleted the array-mixed-with-structs branch March 25, 2024 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unmarshal to struct
1 participant