Skip to content

Convert realValue method to function, update docs #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Sources/ArgumentParser/Parsable Types/ParsableArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ extension ParsableArguments {
}
}

/// Unboxes the given value if it is a `nil` value.
///
/// If the value passed is the `.none` case of any optional type, this function
/// returns `nil`.
///
/// let intAsAny = (1 as Int?) as Any
/// let nilAsAny = (nil as Int?) as Any
/// nilOrValue(intAsAny) // Optional(1) as Any?
/// nilOrValue(nilAsAny) // nil as Any?
func nilOrValue(_ value: Any) -> Any? {
if case Optional<Any>.none = value {
return nil
} else {
return value
}
}

/// Existential protocol for property wrappers, so that they can provide
/// the argument set that they define.
protocol ArgumentSetProvider {
func argumentSet(for key: InputKey) -> ArgumentSet
}
Expand Down Expand Up @@ -241,7 +260,7 @@ extension ArgumentSet {
key: InputKey(rawValue: codingKey),
kind: .default,
parser: { _ in nil },
default: Mirror.realValue(for: child),
default: nilOrValue(child.value),
completion: .default)
definition.help.help = .hidden
return ArgumentSet(definition)
Expand Down
26 changes: 0 additions & 26 deletions Sources/ArgumentParser/Utilities/MirrorExtensions.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/ArgumentParserUnitTests/MirrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension MirrorTests {
}
XCTAssertEqual(stringValue, expectedString)
} else {
XCTAssertNil(Mirror.realValue(for: child))
XCTAssertNil(nilOrValue(child.value))
// This is why we use `unwrapedOptionalValue` for optionality checks
// Even though the `value` is `nil` this returns `false`
XCTAssertFalse(child.value as Any? == nil)
Expand Down