Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaida12345 authored Nov 17, 2024
1 parent eee79ea commit 6b3160a
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,90 @@ https://github.com/Vaida12345/DetailedDescription
## Documentation

Full Documentation in DocC. [View on Github Pages](https://vaida12345.github.io/DetailedDescription/documentation/detaileddescription)

## More Examples

### Recursive

Arguably the best use case is when dealing with recursive structures.

| Definition | Result |
| ---------- | ------ |
| <pre lang="swift">struct SCNNodeDescriptor: CustomDetailedStringConvertible {&#13; &#13; let node: SCNNode&#13; &#13; func detailedDescription(&#13; using descriptor: DetailedDescription.Descriptor<SCNNodeDescriptor>&#13; ) -> any DescriptionBlockProtocol {&#13; descriptor.container {&#13; descriptor.value("name", of: node.name)&#13; descriptor.sequence("children", &#13; of: node.childNodes.map(SCNNodeDescriptor.init)&#13; )&#13; }&#13; }&#13; &#13;}</pre> | <pre>SCNNodeDescriptor&#13; ├─name: nil&#13; ╰─children: <1 element>&#13; ╰─[0]: SCNNodeDescriptor&#13; ├─name: "scene"&#13; ╰─children: <1 element>&#13; ╰─[0]: SCNNodeDescriptor&#13; ├─name: "Meshes"&#13; ╰─children: ...</pre> |

With the above definition, you can now inspect a complex `SCNNode` by `detailedPrint(SCNNodeDescriptor(node: node))`.

### DSL

Similar to `SwiftUI`, the `detailedDescription` function also supports builing conditional blocks, and the use of loops.

```swift
func detailedDescription(using descriptor: DetailedDescription.Descriptor<Component>) -> any DescriptionBlockProtocol {
descriptor.container(showType: false) {
if !content.isEmpty {
descriptor.value(for: \.content)
}
descriptor.sequence(for: \.metadata, hideEmptySequence: true)
descriptor.value(for: \.boundary)
}
}
```
In the above example, the existence of `content` in its output is conditional, and appears only when it is not empty.

<br>

It also supports complex block-building. The following is a portion of code for exploring the PDF structure using `PDFKit`

```swift
descriptor.container("CGPDFArray") {
descriptor.forEach(0..<count) { index in
if let innerArray = source._arrayGetValue(using: CGPDFArrayGetArray, index: index) {
descriptor.value("", of: CGPDFArrayWrapper(source: innerArray))
} else if let name = source._arrayGetValue(using: CGPDFArrayGetName, index: index) {
descriptor.value("", of: String(cString: name))
} else if let stream = source._arrayGetValue(using: CGPDFArrayGetStream, index: index) {
descriptor.value("", of: stream.dictionary)
} else if let dictionary = source._arrayGetValue(using: CGPDFArrayGetDictionary, index: index) {
descriptor.value("", of: dictionary)
} else {
descriptor.string("(unknown)")
}
}
}
```

## Showing Types

The [`container`](https://vaida12345.github.io/DetailedDescription/documentation/detaileddescription/detaileddescription/descriptor/container(_:showtype:blocks:)) comes with ways to configure how you want to describe the children, including `showType`.

```swift
struct BasicModel: CustomDetailedStringConvertible {

let name: String

let age: Int

func detailedDescription(using descriptor: DetailedDescription.Descriptor<BasicModel>) -> any DescriptionBlockProtocol {
descriptor.container(showType: true) {
descriptor.container("details", showType: false) {
descriptor.value(for: \.name)
descriptor.value(for: \.age)
}

descriptor.value(for: \.name)
}
}
}
```

Similar to `SwiftUI` environment values, values are effected by the innermost definition of `showType`, and child containers inherit parent configuration if not specified.

```swift
BasicModel
├─details
├─name: "dog"
╰─age: 11
╰─name: "dog" <String>
```


0 comments on commit 6b3160a

Please sign in to comment.