Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Unbox 2.0

Choose a tag to compare

@JohnSundell JohnSundell released this 03 Oct 23:48
· 100 commits to master since this release

Unbox 2.0 brings a new, streamlined API and reworked internals for maximum flexibility & performance.

⚠️ This release includes breaking changes for users of Unbox 1.9 or earlier. Please read these notes carefully and upgrade with caution. It's also only compatible with Swift 3 (for Swift 2 compatibility, use the swift2 branch.

All APIs have been reworked to follow the Swift 3 API naming guidelines

  • Unbox(_) is now unbox(dictionary:), unbox(data:) and unbox(dictionaries:).
  • UnboxableWithContext.ContextType is now UnboxContext.
  • UnboxableByTransform.UnboxRawValueType is now UnboxRawValue.
  • Wherever key paths were supported with the isKeyPath: argument, that has now been replaced with key: and keyPath: overloads. For example unboxer.unbox("company.title", isKeyPath: true) is now unboxer.unbox(keyPath: "company.title").
  • The UnboxError enum now uses non-captailized first letters for its cases.
  • UnboxableKey.transformUnboxedKey() is now transform(unboxedKey:).
  • UnboxableByTransform.transformUnboxedValue() is now transform(unboxedValue:).
  • UnboxFormatter.formatUnboxedValue() is now format(unboxedValue:).
  • Unboxer.performCustomUnboxingWithDictionary() is now performCustomUnboxing(dictionary:)
  • Unboxer.performCustomUnboxingWithArray() is now performCustomUnboxing(array:)
  • Unboxer.performCustomUnboxingWithData() is now performCustomUnboxing(data:)

Swift native error handling model

Unboxable and UnboxableWithContext types can now throw from their initializer. This enables you to easily manually exit from an initializer, without having to use any specific APIs on Unboxer.

This new capability is also used when performing unboxing of values. Now, all required properties are unboxed using try, meaning that as soon as an error as been encountered, the initializer will be exited. This gives us a huge performance boost when dealing with malformed data, or when allowing invalid elements.

It also means that the unboxFallbackValue() function has been removed, and is no longer required to be implemented by types.

Finally, since now only one error can be thrown from an unboxing process (since we exit early) - UnboxError and UnboxValueError have been merged, and there's no longer a hasFailed property on Unboxer.

Any combination of arrays & dictionaries are now directly unboxable

Previously, Unbox only supported combinations of types and collections that were defined as overloads on Unboxer. Now, thanks to a reworked value resolving system, any combination of types and collections are supported. You can now directly unbox even the most complex data structures with one simple call to unbox().

Removals

Some APIs that have been kept around for legacy reasons have been removed in this release.

  • You can no longer send a context when starting the unboxing process for Unboxable types. Instead, use UnboxableWithContext, which gives you strong typing for your contextual object.
  • UnboxableWithFormatter has been removed, since it wasn't needed. Formatting is still 100% supported, through the use of the UnboxFormatter API.