-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14ffa61
commit 34b9895
Showing
2 changed files
with
60 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
struct QuestionTemplate { | ||
enum RenderMode { | ||
case singleChoice(values: [String]) | ||
case multipleChoice(values: [String]) | ||
case rating(values: [String]) | ||
case freeText | ||
} | ||
|
||
let id: String | ||
let question: String | ||
let renderMode: RenderMode | ||
} | ||
|
||
extension QuestionTemplate: ImmutableMappable { | ||
init(map: Map) throws { | ||
self.init( | ||
id: try map.value("id"), | ||
question: try map.value("question"), | ||
type: try RenderMode(map: map) | ||
) | ||
} | ||
} | ||
|
||
extension RenderMode { | ||
init(map: Map) throws { | ||
let type: String = try map.value("type") | ||
switch type { | ||
case "single-choice": | ||
self = .singleChoice(try map.value("values")) | ||
case "multipleChoice-choice": | ||
self = .multipleChoice(try map.value("values")) | ||
case "rating": | ||
self = .rating(try map.value("values")) | ||
case "free- text": | ||
self = .freeText | ||
default: | ||
// aqui da pra tentar usar um campo default em caso de falha | ||
// mas o ideal é um throw | ||
throw QuestionError.invalidRenderMode | ||
} | ||
} | ||
} |