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

Fix Key.NonTraditional model and decoding #120

Merged
merged 5 commits into from
Oct 9, 2019
Merged

Conversation

jsbean
Copy link
Member

@jsbean jsbean commented Oct 7, 2019

Resolves #119.

@codecov-io
Copy link

codecov-io commented Oct 7, 2019

Codecov Report

Merging #120 into latest will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           latest     #120   +/-   ##
=======================================
  Coverage   45.16%   45.16%           
=======================================
  Files         218      218           
  Lines        2555     2555           
=======================================
  Hits         1154     1154           
  Misses       1401     1401

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 017d3ba...1e3e837. Read the comment docs.

@jsbean jsbean changed the title [WIP / Do Not Merge] Fix Key.NonTraditional model and decoding Fix Key.NonTraditional model and decoding Oct 9, 2019
@jsbean
Copy link
Member Author

jsbean commented Oct 9, 2019

@DJBen & @bwetherfield, this is a bit of a tenuous fix. As per the spec, it would be possible to see something like this:

<key>
    <key-step>B</key-step>
    <key-alter>-1</key-alter>

    <key-step>E</key-step>
    <key-alter>-2</key-alter>
    <key-accidental>slash-flat</key-accidental>

    <key-step>A</key-step>
    <key-alter>-2</key-alter>
    <key-accidental>slash-flat</key-accidental>

    <key-step>F</key-step>
    <key-alter>2</key-alter>
</key>

With this fix, this example would not parse correctly because of this maneuver.

Any thoughts on how to handle this better?

@jsbean jsbean merged commit 5e22f50 into latest Oct 9, 2019
@DJBen
Copy link
Collaborator

DJBen commented Oct 9, 2019

See my latest PR #124, I encountered the same issue with harmony having imploded group that is a list: harmony-chord.

I've tried many decoder combinations but none avail. My final solution is to parse them into a list of enums like below

enum KeyComponent {
  case keyStep(KeyStep)
  case keyAlter(KeyAlter)
  case keyAccidental(KeyAccidental)
}

and reassemble it into multiple Keys. (see my implementation)

@bwetherfield
Copy link
Member

bwetherfield commented Oct 9, 2019

That looks good! Would something like this be possible to enforce order?

while !valuesContainer.isAtEnd {
    do {
        keyComponents.append(.keyStep(try valuesContainer.decode(KeyStep.self)))
        keyComponents.append(.keyAlter(try valuesContainer.decode(KeyAlter.self)))
        keyComponents.append(.keyAccidental(try valuesContainer.decodeIfPresent(KeyAccidental.self)))
    } catch {
        break
    }
}

This would work with

enum KeyComponent {
  case keyStep(KeyStep)
  case keyAlter(KeyAlter)
  case keyAccidental(KeyAccidental?)
}

@DJBen
Copy link
Collaborator

DJBen commented Oct 9, 2019

@bwetherfield yeah that would work. In practice I recommend better error handling logic than catch { break } because this catches both errors

  • Wrong key: this is what we want to catch, so we can start parsing next parameters.
  • Decoding failure: this is what we not want to catch.

@bwetherfield
Copy link
Member

@DJBen - 100%. This would be a great adjustment for the other cases we have used this "start parsing next parameters" fix (I noticed this in the PR you referenced too, looks like a great practice). In this particular case, perhaps any kind of catch { break } is overkill; would probably want to just throw at the top level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix Key.NonTraditional model
4 participants