Skip to content

Repository files navigation

ProtobufKit

codecovLicense: MIT

ProtobufKit is a lightweight1 replacement of swift-protobuf for working with Protocol Buffers serialization technology in Swift.

Overview

ProtobufKit is a cross-platform Swift package on Darwin platform (without OS version limitation) and Linux.2

WorkflowCI Status
Compatibility TestCompatibility tests
macOS Unit TestmacOS
iOS Unit TestiOS
Ubuntu 22.04 Unit TestUbuntu

ProtobufKit is compatible3 with the latest SwiftUI's internal Protobuf implementation so that you can use it to decode some internal binary data of SwiftUI (eg. ArchivedView).

The core design of ProtobufKit is ProtobufMessage which is similar to Codable and easy to customize.

publicprotocolProtobufEncodableMessage{func encode(to encoder:inoutProtobufEncoder)throws}publicprotocolProtobufDecodableMessage{init(from decoder:inoutProtobufDecoder)throws}publictypealiasProtobufMessage=ProtobufDecodableMessage&ProtobufEncodableMessage

You can also use it with Codable by conforming your message type to CodaleByProtobuf or annoate your message instance with ProtobufCodable propertyWrapper.

Getting Started

In your Package.swift file, add the following dependency to your dependencies argument:

.package(url:"https://github.com/OpenSwiftUIProject/ProtobufKit.git", from:"0.1.0"),

Then add the dependency to any targets you've declared in your manifest:

.target(
name:"MyTarget", dependencies:[.product(name:"ProtobufKit",package:"ProtobufKit"),]),

To make a type conform to ProtobufMessage, you need to implement the init(from:) and encode(to:) methods the same as Codable usage.

But instead of using CodingKeys, we use ProtobufTag or UInt here to define the field number.

import ProtobufKit
structSimpleMessage:ProtobufMessage{letvalue:Boolinit(from decoder:inoutProtobufDecoder)throws{whilelet field =try decoder.nextField(){switch field.tag {case1:
value =try decoder.boolField(field)returndefault:try decoder.skipField(field)}}
value =false}func encode(to encoder:inoutProtobufEncoder)throws{
encoder.boolField(1, value)}}

Please see ProtobufKit documentation site for more detailed information about the library.

Future work

ProtobufKit does not have compiler build-in support like Codable, so you have to write the encode and decode logic by yourself.

This can be improved by future macro API like the following.

structSimpleMessage:ProtobufMessage{@Field(tag:1, type:.bool)varvalue:Bool}

License

See LICENSE file - MIT

Related Projects

Star History

Star History Chart

Footnotes

  1. Under macOS + Build for profiling option, ProtobufKit's ProtobufKit.o is 142KB while swift-protobuf's SwiftProtobuf.o is 5.7MB

  2. WASI support will be added in the future

  3. Compatibility with SwiftUI may break in the future SwiftUI release

About

ProtobufKit is a lightweight replacement of swift-protobuf for working with Protocol Buffers serialization technology in Swift.

Resources

Stars

91 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages