Skip to content

Commit

Permalink
Add ContentTypeName to switch on enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Palleas committed Oct 10, 2017
1 parent f9d214e commit 261c768
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions Sources/Tentacle/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public enum Content: ResourceType {
/// A file in a repository
public struct File: CustomStringConvertible, Decodable {

public enum ContentTypeName: String, Decodable {
case file
case directory = "dir"
case symlink
case submodule
}

/// Type of content in a repository
///
/// - file: a file in a repository
Expand All @@ -57,30 +64,24 @@ public enum Content: ResourceType {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type)
let type = try container.decode(ContentTypeName.self, forKey: .type)
switch type {
case "file":
case .file:
let size = try container.decode(Int.self, forKey: .size)
if let url = try container.decodeIfPresent(URL.self, forKey: .downloadURL) {
self = .file(size: size, downloadURL: url)
} else {
self = .submodule(url: nil)
}
case "dir":
case .directory:
self = .directory
case "submodule":
case .submodule:
let url = try container.decodeIfPresent(String.self, forKey: .submoduleURL)
self = .submodule(url: url)
case "symlink":
case .symlink:
let target = try container.decodeIfPresent(String.self, forKey: .target)
let url = try container.decodeIfPresent(URL.self, forKey: .downloadURL)
self = .symlink(target: target, downloadURL: url)
default:
throw DecodingError.dataCorruptedError(
forKey: CodingKeys.type,
in: container,
debugDescription: "Invalid content-type \(type)"
)
}
}

Expand Down

0 comments on commit 261c768

Please sign in to comment.