Skip to content

Instantly share code, notes, and snippets.

@trupin
Last active October 10, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trupin/9438713f8fae0a5a7f424eca1976f42b to your computer and use it in GitHub Desktop.
Save trupin/9438713f8fae0a5a7f424eca1976f42b to your computer and use it in GitHub Desktop.
[
{
"dependencies" : [
{
"scope" : "graph",
"name" : "urlSession",
"object_type" : "Reference",
"type" : {
"object_type" : "Type",
"name" : "URLSession"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "ImageManager"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "MovieViewController"
}
]
},
{
"dependencies" : [
{
"scope" : "graph",
"name" : "reviewManager",
"object_type" : "Registration",
"type" : {
"object_type" : "Type",
"name" : "ReviewManager"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "WSReviewViewController"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "MovieViewController"
}
]
},
{
"dependencies" : [
{
"scope" : "graph",
"name" : "urlSession",
"object_type" : "Reference",
"type" : {
"object_type" : "Type",
"name" : "URLSession"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "MovieManager"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "HomeViewController"
}
]
},
{
"dependencies" : [
{
"scope" : "graph",
"name" : "imageManager",
"object_type" : "Registration",
"type" : {
"object_type" : "Type",
"name" : "ImageManager"
}
},
{
"scope" : "transient",
"name" : "reviewController",
"object_type" : "Registration",
"type" : {
"object_type" : "Type",
"name" : "WSReviewViewController"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "MovieViewController"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "HomeViewController"
}
]
},
{
"dependencies" : [
{
"scope" : "graph",
"name" : "urlSession",
"object_type" : "Reference",
"type" : {
"object_type" : "Type",
"name" : "URLSession"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "ReviewManager"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "WSReviewViewController"
}
]
},
{
"dependencies" : [
{
"scope" : "graph",
"name" : "movieManager",
"object_type" : "Registration",
"type" : {
"object_type" : "Type",
"name" : "MovieManager"
}
},
{
"abstractType" : {
"object_type" : "Type",
"name" : "UIViewController"
},
"scope" : "transient",
"name" : "movieController",
"object_type" : "Registration",
"type" : {
"object_type" : "Type",
"name" : "MovieViewController"
}
}
],
"type" : {
"object_type" : "Type",
"name" : "HomeViewController"
},
"object_type" : "DependencyContainer",
"sources" : [
{
"object_type" : "Type",
"name" : "AppDelegate"
}
]
},
{
"object_type" : "DependencyContainer",
"type" : {
"object_type" : "Type",
"name" : "URLSession"
},
"sources" : [
{
"object_type" : "Type",
"name" : "MovieManager"
},
{
"object_type" : "Type",
"name" : "ImageManager"
},
{
"object_type" : "Type",
"name" : "ReviewManager"
},
{
"object_type" : "Type",
"name" : "AppDelegate"
}
]
}
]
@trupin
Copy link
Author

trupin commented Oct 5, 2018

This was generated by executing the following command at the root dir of this project: https://github.com/trupin/WeaverTutorialPart1

weaver export `find Sample -name '*.swift' | xargs -0` --pretty

@trupin
Copy link
Author

trupin commented Oct 6, 2018

DependencyContainer

Object containing the dependencies of an "injectable" type. Weaver generates one per type declaration containing annotations.

Attributes:

  • type: Type with which the container is associated with.
  • dependencies: List of the resolvable dependencies for this container. Can be either a Registration, Reference, Parameter.
  • sources: List of the containers from which this container (and its associated type) is being built or referenced.

Dependency

Object representing a dependency resolvable from a container. Registered implies that the dependency is made accessible by the container it's registered in.

Attributes

  • name: Unique name throughout the app the dependency can be referenced with.
  • scope: See the doc here.
  • type: Dependency's type.
  • abstract_type: Dependency's abstract type. Can be an inherited type or a protocol.
  • object_type: Type of dependency. Can be either Registration, Reference, Parameter.
    • Registration: Dependency built by the container it's registered in.
    • Reference: Dependency coming from one of the source container of the container it's referenced in.
    • Parameter: Simple parameter. Used for non injectable types (mostly scalar types and data models).

@trupin
Copy link
Author

trupin commented Oct 9, 2018

Reshape the output with jq

Output type names only

$ weaver export `find Sample -name '*.swift' | xargs -0` | jq -c '
  .[] | 
  map(.sources[] |= .name) |
  map(.dependencies[]? |= .type.name) | 
  map(. |= {dependencies, sources, type: .type.name})
' | jq
[
  {
    "dependencies": [
      "URLSession"
    ],
    "sources": [
      "MovieViewController"
    ],
    "type": "ImageManager"
  },
  ...
  {
    "dependencies": null,
    "sources": [
      "MovieManager",
      "ImageManager",
      "ReviewManager",
      "AppDelegate"
    ],
    "type": "URLSession"
  }
]

Select only containers with type name equal to "ImageManager":

weaver export `find Sample -name '*.swift' | xargs -0` | jq -c '
  .[] |
  map(select(.type.name == "ImageManager"))
' | jq
[
  {
    "dependencies": [
      {
        "scope": "graph",
        "name": "urlSession",
        "object_type": "Reference",
        "type": {
          "object_type": "Type",
          "name": "URLSession"
        }
      }
    ],
    "type": {
      "object_type": "Type",
      "name": "ImageManager"
    },
    "object_type": "DependencyContainer",
    "sources": [
      {
        "object_type": "Type",
        "name": "MovieViewController"
      }
    ]
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment