<commit_before>//
//  GlucoseEntry.swift
//  NightscoutUploadKit
//
//  Created by <NAME> on 4/19/20.
//  Copyright © 2020 <NAME>. All rights reserved.
//
import Foundation

public struct GlucoseEntry {
    typealias RawValue = [String: Any]

    public var identifier: String
    public var sgv: Double
    public var date: Date
    public var trend: Int
    public var direction: String
    public var device: String
    public var type: String

    init?(rawValue: RawValue) {
        guard
            let identifier = rawValue["_id"] as? String,
            let sgv =  rawValue["sgv"] as? Double,
            let epoch = rawValue["date"] as? Double,
            let trend = rawValue["trend"] as? Int,
            let direction = rawValue["direction"] as? String,
            let device = rawValue["device"] as? String,
            let type = rawValue["type"] as? String
        else {
            return nil
        }

        self.identifier = identifier
        self.sgv = sgv
        self.date = Date(timeIntervalSince1970: epoch / 1000.0)
        self.trend = trend
        self.direction = direction
        self.device = device
        self.type = type

    }
}
<commit_msg>Support nil trend/direction in GlucoseEntry. Support both Int/String types for trend based on Dexcom 2021 format changes.<commit_after>//
//  GlucoseEntry.swift
//  NightscoutUploadKit
//
//  Created by <NAME> on 4/19/20.
//  Copyright © 2020 <NAME>. All rights reserved.
//
import Foundation

public struct GlucoseEntry {
    typealias RawValue = [String: Any]

    public var identifier: String
    public var sgv: Double
    public var date: Date
    public var trend: Int?
    public var direction: String?
    public var device: String
    public var type: String

    init?(rawValue: RawValue) {
        
        //Dexcom changed the format of trend in 2021 so we accept both String/Int types
        var trend: Int? = nil
        if let intTrend = rawValue["trend"] as? Int {
            trend = intTrend
        } else if let stringTrend = rawValue["trend"] as? String, let intTrend = Int(stringTrend) {
            trend = intTrend
        }
        
        let direction: String? = rawValue["direction"] as? String
        
        guard
            let identifier = rawValue["_id"] as? String,
            let sgv =  rawValue["sgv"] as? Double,
            let epoch = rawValue["date"] as? Double,
            let device = rawValue["device"] as? String,
            let type = rawValue["type"] as? String
        else {
            return nil
        }

        self.identifier = identifier
        self.sgv = sgv
        self.date = Date(timeIntervalSince1970: epoch / 1000.0)
        self.trend = trend
        self.direction = direction
        self.device = device
        self.type = type

    }
}

{'fraction_non_alphanumeric': 0.09285714285714286, 'fraction_numerical': 0.015714285714285715, 'mean_word_length': 2.024838012958963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '670574', 'n_tokens_mistral': 829, 'n_tokens_neox': 774, 'n_words': 268}