An Avro encoding/decoding library for Swift.
BlueSteel is part of the Cleanroom Project from Gilt Tech.
This is the master
branch. It uses Swift 2.3 and requires Xcode 8 to compile.
If you need Swift 3.0 compatibility, use the swift3
branch, which requires Xcode 8.
Branch | Build status |
---|---|
master |
|
swift3 |
Take a gander at the official documentation for Avro before reading further.
BlueSteel is distributed under the MIT license.
BlueSteel is provided for your use—free-of-charge—on an as-is basis. We make no guarantees, promises or apologies. Caveat developer.
You’ll need to integrate BlueSteel into your project in order to use the API it provides. You can choose:
- Manual integration, wherein you embed BlueSteel’s Xcode project within your own, or
- Using the Carthage dependency manager to build a framework that you then embed in your application.
Once integrated, just add the following import
statement to any Swift file where you want to use BlueSteel:
import BlueSteel
For detailed information on using BlueSteel, API documentation is available.
Since Avro data is not self describing, we're going to need to supply an Avro Schema before we can (de)serialize any data. Schema enums are constructed from a JSON schema description, in either String or NSData form.
import BlueSteel
let jsonSchema = "{ \"type\" : \"string\" }"
let schema = Schema(jsonSchema)
Using the Schema above, we can now decode some Avro binary data.
let rawBytes: [Byte] = [0x6, 0x66, 0x6f, 0x6f]
let avro = AvroValue(schema: schema, withBytes: rawBytes)
We can now get the Swift String from the Avro value above using an optional getter.
if let avroString = avro.string {
print(avroString) // Prints "foo"
}
We can use the same Schema above to serialize an AvroValue to binary.
if let serialized = avro.encode(schema) {
print(serialized) // Prints [6, 102, 111, 111]
}
By conforming to the AvroValueConvertible protocol! You just need to extend your types with one function:
func toAvro() -> AvroValue
Suppose we wanted to serialize a NSUUID with the following schema:
{
"type" : "fixed",
"name" : "UUID",
"size" : 16
}
We could extend NSUUID as follows:
extension NSUUID : AvroValueConvertible {
public func toAvro() -> AvroValue {
var uuidBytes: [Byte] = [Byte](count: 16, repeatedValue: 0)
self.getUUIDBytes(&uuidBytes)
return AvroValue.AvroFixedValue(uuidBytes)
}
}
To generate and serialize a NSUUID, we could now do:
let serialized: [Byte]? = NSUUID().toAvro().encode(uuidSchema)
Hey presto! We now have a byte array representing an NSUUID serialized to Avro according to the fixed schema provided. Okay, so the example above is maybe a little bit too simple. Let's take a look at a more complex example. Suppose we have a record schema as follows:
{
"type": "record",
"name": "test",
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"}
]
}
We could create a corresponding type Swift that might look something like this:
struct testStruct {
var a: Int64 = 0
var b: String = ""
}
To convert testStruct to AvroValue, we could extend it like this:
extension testStruct : AvroValueConvertible {
func toAvro() -> AvroValue {
return AvroValue.AvroRecordValue([
"a" : self.a.toAvro(),
"b" : self.b.toAvro()])
}
}
You might've noticed above that we called .toAvro() on Int64 and String values. We didn't have to define these ourselves because BlueSteel provides AvroValueConvertible extensions for Swift primitives.
So that just about covers a very quick introduction to BlueSteel. Please note that BlueSteel is still very early in development and may change significantly.
The Cleanroom Project began as an experiment to re-imagine Gilt’s iOS codebase in a legacy-free, Swift-based incarnation.
Since then, we’ve expanded the Cleanroom Project to include multi-platform support. Much of our codebase now supports tvOS in addition to iOS, and our lower-level code is usable on macOS and watchOS as well.
Cleanroom Project code serves as the foundation of Gilt on TV, our tvOS app featured by Apple during the launch of the new Apple TV. And as time goes on, we'll be replacing more and more of our existing Objective-C codebase with Cleanroom implementations.
In the meantime, we’ll be tracking the latest releases of Swift & Xcode, and open-sourcing major portions of our codebase along the way.
BlueSteel is in active development, and we welcome your contributions.
If you’d like to contribute to this or any other Cleanroom Project repo, please read the contribution guidelines.
API documentation for BlueSteel is generated using Realm’s jazzy project, maintained by JP Simard and Samuel E. Giddins.