Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Metadata extraction for Metadata Table View #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Metadata extraction for Metadata Table View #215

wants to merge 3 commits into from

Conversation

SlaunchaMan
Copy link

This started as an exercise to code-golf flattenXcodeProjects(_:targets:) after being poked by @nwest. I got this far:

private func flattenXcodeProjects(projects:[CPXcodeProject], targets:[CPCocoaPodsTarget]) -> [AnyObject] {
  return projects.flatMap { project in
    [project] + project.targets.flatMap { target -> [AnyObject] in
      let pods = target.cocoapodsTargets.flatMap { targetName in
        targets.filter { $0.name == targetName }.flatMap { $0.pods }
      } as [AnyObject]

      return [target] + pods + ["spacer"]
    }
  }
}

The more I used it, though, the more I realized that there are two things happening in this file: converting from a list of Xcode projects and a list of CocoaPods targets to an array of entries to display in a table view, and displaying metadata from that list to display in the table. Both of those tasks seemed well-suited to protocols. So, this PR creates two protocols: CPMetadataExtractable, which covers converting the two arrays into one array of metadata, and CPMetadataRepresentable, which covers displaying the data in the table.

This is a pretty big change, but it buys more type safety. Notice that tableView(_:heightOfRow:) and tableView(_: viewForTableColumn:row:) no longer need to handle the “else” case of displaying nothing, since the array of projectMetadataEntries is type-safe.

Overall, in exchange for a net gain of 14 lines, we’ve gained a lot of safety. I think it’s a net positive.

.map { $0 as CPMetadataRepresentable }
}

return [self as CPMetadataRepresentable] + pods + [Spacer() as CPMetadataRepresentable]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea if this has bad compiler performance characteristics? http://irace.me/swift-profiling/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked. I should have some time later today to do a comparison.

@nwest
Copy link
Member

nwest commented Jun 1, 2016

@SlaunchaMan do you mind merging master again?

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

Successfully merging this pull request may close these issues.

None yet

3 participants