-
Notifications
You must be signed in to change notification settings - Fork 580
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
Add a PluginAPI #904
Closed
Closed
Add a PluginAPI #904
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…platform at the moment
…platform at the moment
Now that I have something a bit better even if it's still early but it doesn’t break the API and adds a PluginAPI, @stephencelis and @mbrandonw, how can this be improved? What is missing, and how do you think we can enhance the design of the API? Do you think this has any chance to be merged ? Thanks for your feedback. |
5 tasks
This PR might be too messy, so I’ve split it to better explain its content:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello again,
I’d like to share the progress on a general plugin API, along with a specific sub-plugin API for image serialization, following up on our previous discussion here.
Motivation
Some tasks, like exporting images in various formats, can't be handled easily with strategies alone. By introducing this plugin system, we can make
swift-snapshot-testing
more modular, flexible, and extensible. This allows for future enhancements without requiring changes to the core library, making it the ideal solution for cross-cutting concerns like image serialization.Key Objectives:
Why a Plugin API?
Certain functionalities like image encoding and decoding are better served by a plugin API rather than strategies. The plugin system gives
swift-snapshot-testing
a powerful mechanism for handling additional functionality without bloating the core library.Current Image Serialization Plugins:
This opens the door for future sub-plugin APIs, making
swift-snapshot-testing
a flexible and evolving ecosystem.Important Files:
Sources/ImageSerializationPlugin/ImageSerializationPlugin.swift
: Defines the Plugin API for image serialization.Sources/SnapshotTesting/Plug-ins/ImageSerializer.swift
: Manages image serialization.Sources/SnapshotTesting/Plug-ins/PluginRegistry.swift
: Abstract plugin management.Sources/SnapshotTestingPlugin/SnapshotTestingPlugin.swift
: Defines the core plugin API, whichImageSerializationPlugin
conforms to.The changes in
Sources/SnapshotTesting/Snapshotting
mainly pass through the newimageFormat
parameter.Next Steps:
withSnapshotTesting
Future Directions:
How It Works
The Core Plugin API
The plugin architecture is designed to allow developers to register plugins that provide specific functionality without modifying the core library. Plugins are registered via a
PluginRegistry
and retrieved dynamically at runtime.Core Flow:
Plugin Registration: Plugins conform to the
SnapshotTestingPlugin
protocol, providing anidentifier
and specific behavior. ThePluginRegistry
manages the available plugins.Plugin Lookup: When a plugin is needed (e.g., for image serialization), the
PluginRegistry
retrieves the plugin by its identifier. The system also supports querying all plugins of a specific type.Extensibility: The architecture is designed for future expansion, allowing other cross-cutting concerns like logging, network requests, or file management to be addressed via plugins.
Image Serialization via Plugins
The first practical use of this plugin architecture is in image encoding and decoding via the
ImageSerializationPlugin
. This allows support for various formats, including JPEG XL, HEIC, and WebP. In our repository, for example, we have a folder containing 1,000 snapshot-test images, which totals 202.1 MB when saved as PNG files. By switching to JPEG XL, this size is reduced to just 60.7 MB a 70% decrease in storage usage.ImageSerializationPlugin
: This protocol enables third parties to provide their own image encoders/decoders. Each plugin specifies theimageFormat
it supports and implementsencodeImage
anddecodeImage
for handling that format.ImageSerializer
Class: This class manages the encoding and decoding process, using the plugin system to dynamically choose the appropriate plugin for the image format.Example in Action:
ImageSerializer
queries thePluginRegistry
for all registered image serialization plugins.JXLImageSerializer
plugin, which has registered itself with the identifier "jxl".decodeImage
method ofJXLImageSerializer
is called to decode the data into aSnapImage
.This modular approach makes it easy to add new image formats by simply adding plugins.
Diagram
This demonstrates how the Plugin API serves as the backbone for managing various plugins in our case image formats via ImageSerializationPlugin.
By implementing this plugin system, we make
swift-snapshot-testing
more modular, future-proof, and easy to extend, without complicating the existing API.Looking forward to your feedback!