DebugReflect

master

Debug dump support for Swift
omochi/DebugReflect

DebugReflect

This library helps dumping your data structure in Swift. It supports classes and reference cycle.

Example

Type implementation.

import DebugReflect

class Cat : DebugReflectable {
    init(name: String) {
        self.name = name
    }
    
    var name: String
    var children: [Cat] = []
    var props: [String: String] = [:]
    var bestFriend: Cat?
    var stone: Stone?

    func debugReflect() -> DebugReflectValue {
        return .build { b in
            b.field("name", name)
            b.field("children", children)
            b.field("props", props)
            b.fieldIfPresent("bestFriend", bestFriend)
            b.fieldIfPresent("stone", stone)
        }
    }
}

struct Stone : DebugReflectable {
    func debugReflect() -> DebugReflectValue {
        return .build { b in
        }
    }
}

Using.

        let cat0 = Cat(name: "apple")
        let cat1 = Cat(name: "banana")
        let cat2 = Cat(name: "cherry")
        cat2.bestFriend = cat1
        cat0.children = [
            cat1,
            cat2,
            cat0
        ]
        cat0.props = [
            "blood": "A"
        ]
        cat0.stone = Stone()
        print(cat0.debugDescription)

Output.

Cat@0x8750e850 {
  name => apple
  children => [
    Cat@0x8750f010 {
      name => banana
      children => []
      props => {}
    }
    Cat@0x8750fb20 {
      name => cherry
      children => []
      props => {}
      bestFriend => Cat@0x8750f010 { recursive }
    }
    Cat@0x8750e850 { recursive }
  ]
  props => {
    blood => A
  }
  stone => Stone {}
}

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Apr 12 2024 12:32:45 GMT-0900 (Hawaii-Aleutian Daylight Time)