fwcd / fwcd/d2

Investigate providing statically typed commands using `Codable` structs and custom `Encoder`/`Decoder`

Open
#103 0 comments 0 reactions 0 assignees View on GitHub
improvement refactoring
Dominant language
Swift
Stars
17
Forks
5
PR merge metrics
No merged PRs in 30d

Description

While `ArgCommand` provides an abstraction for statically typed parameters, using it is unwieldy due to chains of `.left`/`.right` when e.g. using `ArgPair`s, which are not very descriptive. Due to this and the comparative lack in flexibility, many commands still use `StringCommand` and their own input parsers.

Clearly, it would be great to have a strongly typed abstraction for command inputs that doesn't compromise on readability. `Codable` structs might let us do something like this:

```swift
class MyCommand: TypedCommand {
struct Input: Codable {
let aString: String
let anInt: Int
}

func invoke(with input: Input, output: CommandOutput, context: CommandContext) {
// Do something with e.g. input.aString
}
}
```

This would however require `Codable` to traverse the keys deterministically in their order of declaration, which might not be given. If this is not given, we could also explicitly declare the order of `CodingKey`s in a static array:

```swift
extension Input {
static let argOrder = [.aString, .aInt]
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.