Skip to content

Commit

Permalink
metadata update
Browse files Browse the repository at this point in the history
  • Loading branch information
ItachiEU committed Sep 6, 2024
1 parent 3eb2d75 commit 179dbbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 95 deletions.
92 changes: 7 additions & 85 deletions Sources/VitalCore/Core/Encodable/AnyEncodable.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import Foundation

public protocol VitalAnyEncodableProtocol: Encodable {
var wrappedValue: Any { get }
}

public struct VitalAnyEncodable: VitalAnyEncodableProtocol {
public let wrappedValue: Any
private let encodeFunction: (Encoder) throws -> Void
public struct VitalAnyEncodable: Encodable {
private let encode: (Encoder) throws -> Void

public init<T: Encodable>(_ value: T) {
self.wrappedValue = value
self.encodeFunction = { encoder in
try value.encode(to: encoder)
public init(_ encodable: Encodable) {
self.encode = { encoder in
try encodable.encode(to: encoder)
}
}

public func encode(to encoder: Encoder) throws {
try encodeFunction(encoder)
try encode(encoder)
}

public var dictionary: [String: AnyHashable]? {
Expand All @@ -25,80 +19,8 @@ public struct VitalAnyEncodable: VitalAnyEncodableProtocol {
}
}


public struct VitalAnyEncodableEquatable: VitalAnyEncodableProtocol, Equatable {
public let wrappedValue: Any
private let encodeFunction: (Encoder) throws -> Void
private let equalityFunction: (VitalAnyEncodableEquatable) -> Bool

public init<T: Encodable & Equatable>(_ value: T) {
self.wrappedValue = value
self.encodeFunction = { encoder in
try value.encode(to: encoder)
}
self.equalityFunction = { other in
guard let otherValue = other.wrappedValue as? T else { return false }
return value == otherValue
}
}

public func encode(to encoder: Encoder) throws {
try encodeFunction(encoder)
}

public static func == (lhs: VitalAnyEncodableEquatable, rhs: VitalAnyEncodableEquatable) -> Bool {
return lhs.equalityFunction(rhs)
}
}


public struct VitalAnyEncodableHashable: VitalAnyEncodableProtocol, Equatable, Hashable {
public let wrappedValue: Any
private let encodeFunction: (Encoder) throws -> Void
private let equalityFunction: (VitalAnyEncodableHashable) -> Bool
private let hashFunction: (inout Hasher) -> Void

public init<T: Encodable & Equatable & Hashable>(_ value: T) {
self.wrappedValue = value
self.encodeFunction = { encoder in
try value.encode(to: encoder)
}
self.equalityFunction = { other in
guard let otherValue = other.wrappedValue as? T else { return false }
return value == otherValue
}
self.hashFunction = { hasher in
hasher.combine(value)
}
}

public func encode(to encoder: Encoder) throws {
try encodeFunction(encoder)
}

public static func == (lhs: VitalAnyEncodableHashable, rhs: VitalAnyEncodableHashable) -> Bool {
return lhs.equalityFunction(rhs)
}

public func hash(into hasher: inout Hasher) {
hashFunction(&hasher)
}
}

public extension Encodable {
func eraseToAnyEncodable() -> VitalAnyEncodable {
return VitalAnyEncodable(self)
}
}

public extension Encodable where Self: Equatable {
func eraseToAnyEncodable() -> VitalAnyEncodableEquatable {
return VitalAnyEncodableEquatable(self)
}
}

public extension Encodable where Self: Equatable & Hashable {
func eraseToAnyEncodable() -> VitalAnyEncodableHashable {
return VitalAnyEncodableHashable(self)
return .init(self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,12 @@ extension LocalQuantitySample {

var metadata: [String: String] = [:]

if let meal = sample.metadata?["Meal"] as? String {
metadata["meal"] = meal
}

if let meal = sample.metadata?["HKFoodMeal"] as? String {
metadata["meal"] = meal
}

if let foodItem = sample.metadata?["HKFoodType"] as? String {
metadata["food_item"] = foodItem
if let metadataDict = sample.metadata {
for (key, value) in metadataDict {
if let stringValue = value as? String {
metadata[key] = stringValue
}
}
}

self.init(
Expand Down

0 comments on commit 179dbbb

Please sign in to comment.