Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct Element: Decodable {
struct BinaryOperationProperties: Decodable
{ let operation: String }
struct BracketsProperties: Decodable
{ let type: String }
struct ChemicalElementProperties: Decodable
{ let element: String }
struct DifferentialProperties: Decodable
{ let letter: String }
struct FunctionProperties: Decodable
{ let name: String, custom: Bool?, innerSuperscript: Bool?, allowSubscript: Bool? }
struct NumberProperties: Decodable {
let significand: Double?
enum CodingKeys: CodingKey {
case significand
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let significand_maybe = try values.decode(String.self, forKey: .significand)
significand = Double(significand_maybe)
}
}
struct ParticleProperties: Decodable
{ let particle: String, type: String }
struct RelationProperties: Decodable
{ let relation: String }
struct StateSymbolProperties: Decodable
{ let state: String }
struct SymbolProperties: Decodable
{ let letter: String }
enum Properties {
case binaryOperation(BinaryOperationProperties),
brackets(BracketsProperties),
chemicalElement(ChemicalElementProperties),
differential(DifferentialProperties),
function(FunctionProperties),
number(NumberProperties),
particle(ParticleProperties),
relation(RelationProperties),
stateSymbol(StateSymbolProperties),
symbol(SymbolProperties)
}
let type: String,
properties: Properties?
enum CodingKeys: CodingKey {
case type, properties
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
type = try values.decode(String.self, forKey: .type)
switch type {
case "BinaryOperation":
properties = try .binaryOperation(values.decode(BinaryOperationProperties.self, forKey: .properties))
case "Brackets":
properties = try .brackets(values.decode(BracketsProperties.self, forKey: .properties))
case "ChemicalElement":
properties = try .chemicalElement(values.decode(ChemicalElementProperties.self, forKey: .properties))
case "Differential":
properties = try .differential(values.decode(DifferentialProperties.self, forKey: .properties))
case "Fn":
properties = try .function(values.decode(FunctionProperties.self, forKey: .properties))
case "Num":
properties = try .number(values.decode(NumberProperties.self, forKey: .properties))
case "Particle":
properties = try .particle(values.decode(ParticleProperties.self, forKey: .properties))
case "Relation":
properties = try .relation(values.decode(RelationProperties.self, forKey: .properties))
case "StateSymbol":
properties = try .stateSymbol(values.decode(StateSymbolProperties.self, forKey: .properties))
case "Symbol":
properties = try .symbol(values.decode(SymbolProperties.self, forKey: .properties))
default:
properties = nil
}
}
}
struct Event: Decodable {
let event: String,
timestamp: Int,
symbol: Element?
}
struct LogRow: Decodable {
let id:Int
let user_id:String
let question_id:String
let actions:[Event]
let timestamp:Double
enum CodingKeys: String, CodingKey {
case id
case user_id
case question_id
case actions
case timestamp
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decode(Int.self, forKey: .id)
user_id = try values.decode(String.self, forKey: .user_id)
question_id = try values.decode(String.self, forKey: .question_id)
timestamp = try values.decode(Double.self, forKey: .timestamp)
let actions_string = try values.decode(String.self, forKey: .actions)
let actions_data = actions_string.data(using: .utf8)!
actions = try JSONDecoder().decode([Event].self, from: actions_data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment