Skip to content

Commit

Permalink
Add support for struct mirroring (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-metaplex authored Nov 15, 2022
1 parent f467c6a commit 6c19f5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Sources/Beet/Beets/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ public func mirrored(value: Any) -> (label: String, params: [String: Any]) {
return ("\(value)", dictionary)
}

if reflection.displayStyle == .struct {
var dictionary = [String: Any]()
for child in reflection.children {
if let label = child.label {
dictionary[label] = child.value
}
}
return ("\(value)", dictionary)
}

if reflection.displayStyle == .enum, let associated = reflection.children.first {
let values = Mirror(reflecting: associated.value).children
var valuesArray = [String: Any]()
Expand Down
25 changes: 14 additions & 11 deletions Sources/Beet/Struct.Fixable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,21 @@ public class FixableBeetStruct<Class>: FixableBeet {
}

public func toFixedFromValue(val: Any) -> FixedSizeBeet {
let value = val as! Args
let argsKeys = value.keys
var fixedFields: [FixedBeetField] = []
let mirror = mirrored(value: val)

for i in 0..<self.fields.count {
let (key, beet) = self.fields[i]
assert(argsKeys.contains {
$0 == key
}, "Value with keys [ \(argsKeys) ] should include struct key '\(key)' but doesn't.")
let argValue = value[key]
let fixedBeet = fixBeetFromValue(beet: beet, val: argValue)
fixedFields.append((key, fixedBeet))
var dictionary: [AnyHashable: Any] = [:]
for param in mirror.params {
dictionary[param.key] = param.value
}

var fixedFields: [FixedBeetField] = []
for f in fields {
switch f.beet {
case .fixedBeet(let type):
fixedFields.append((f.type, type))
case .fixableBeat(let type):
fixedFields.append((f.type, type.toFixedFromValue(val: dictionary[f.type]!)))
}
}

if self.description != FixableBeetStruct_TYPE {
Expand Down

0 comments on commit 6c19f5c

Please sign in to comment.