{"id":29376513,"url":"https://github.com/twitter/ios-twitter-apache-thrift","last_synced_at":"2025-07-09T22:43:20.701Z","repository":{"id":43637896,"uuid":"318349449","full_name":"twitter/ios-twitter-apache-thrift","owner":"twitter","description":"A thrift encoding and decoding library for Swift","archived":false,"fork":false,"pushed_at":"2022-02-04T22:44:49.000Z","size":148,"stargazers_count":42,"open_issues_count":1,"forks_count":16,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-05-09T19:35:06.444Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twitter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-03T23:42:19.000Z","updated_at":"2024-04-29T21:40:02.000Z","dependencies_parsed_at":"2022-07-13T11:00:28.404Z","dependency_job_id":null,"html_url":"https://github.com/twitter/ios-twitter-apache-thrift","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/twitter/ios-twitter-apache-thrift","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fios-twitter-apache-thrift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fios-twitter-apache-thrift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fios-twitter-apache-thrift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fios-twitter-apache-thrift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twitter","download_url":"https://codeload.github.com/twitter/ios-twitter-apache-thrift/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fios-twitter-apache-thrift/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264504616,"owners_count":23618831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-09T22:43:20.032Z","updated_at":"2025-07-09T22:43:20.694Z","avatar_url":"https://github.com/twitter.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Twitter Apache Thrift\n\nSwift’s modern model constructs allow a 1:1 mapping to Thrift. Swift with Thrift \nallows the client and backend to use a common model for representing data.\n\n## Getting Started\n\n### Installing\n\n#### Swift Package Manager\nAdd the following to your Package.swift file\n\n```\n.package(name: \"TwitterApacheThrift\", url: \"https://github.com/twitter/ios-twitter-apache-thrift\", .upToNextMajor(from: \"1.0.0\"))\n```\n\n#### Carthage\nAdd the following to your Cartfile\n\n```\ngithub \"twitter/ios-twitter-apache-thrift\"\n```\n\n### Usage\n\n#### Encoding and Decoding\nThe thrift encoder and decoder use the Encoder and Decoder protocols. Conforming\nto these protocols allows the compiler to autogenerate the codable methods. This \nreduces the potential for errors; they also receive JSON support for free. The \nimplementation of these models deviate from the official thrift specification. The \nofficial specification was designed for languages without automatic encoding and \ndecoding. Output and input binary will be up to the official thrift specification. There \nis ThriftEncodable and ThriftDecodable protocols that confirm the respective swift \nprotocols. There is also a typealias combining them for convenience.\n ```\npublic typealias ThriftCodable = ThriftDecodable \u0026 ThriftEncodable\n\n/// A protocol for types which can be encoded from thrift.\npublic protocol ThriftEncodable: Encodable {\n    /// Encodes this value into the given thrift encoder.\n    ///\n    /// If the value fails to encode anything, `encoder` will encode an empty\n    /// keyed container in its place.\n    ///\n    /// This function throws an error if any values are invalid for the given\n    /// encoder's format.\n    ///\n    /// This function is not required to be implemented,\n    /// however `func encode(to encoder: Encoder) throws` is required\n    ///\n    /// - Parameter encoder: The encoder to write data to.\n    func thriftEncode(to encoder: ThriftEncoder) throws\n\n    /// Provides an override point to all the protocol implementer to provide\n    /// a different ThriftType such as for wrapping\n    ///\n    /// This function is not required to be implemented, defaults to struct\n    ///\n    /// - returns: The thrift type of the implementer\n    static func thriftType() -\u003e ThriftType\n\n    /// Provides a validation step before encoding to insure fields are set\n    /// to appropriate values provided by the implementer.\n    ///\n    /// This function is not required to be implemented, default is no validation\n    ///\n    /// - throws: `ThriftEncoderError.validationFailure()` if field validation fails\n    func validate() throws\n}\n\n/// A protocol for types which can be decoded from thrift.\npublic protocol ThriftDecodable: Decodable {\n\n    /// Creates a new instance by decoding from the given thrift decoder.\n    ///\n    /// This initializer throws an error if reading from the decoder fails, or\n    /// if the data read is corrupted or otherwise invalid.\n    ///\n    /// This function is not required to be implemented,\n    /// however `init(from decoder: Decoder)` is required\n    ///\n    /// - Parameter decoder: The decoder to read data from.\n    init(fromThrift decoder: ThriftDecoder) throws\n}\n```\nEncoding and decoding use the ThriftEncoder and ThriftDecoder. The usage \nwill be like the following.\n```\n//Encoder\nlet thrift = SomeThriftEncodable()\nlet encoder = ThriftEncoder()\nlet data = try thriftEncoder.encode(thrift)\n\n//Decoder\nlet decoder = ThriftDecoder()\nlet thriftObject = try decoder.decode(SomeThriftEncodable.self, from: thriftData)\n```\n#### Type Mapping\nMappings between foundation Swift types to thrift types handled by the library. \nThe following table outlines how the swift types map the thrift type.\n\nNote: \n    Data is a special case. The format for data is the same as string so they unified \n    and rely on the swift type for encoding and decoding.\n\n| Swift type | Thrift Type (UInt8 Value) |\n| ----------- | -------------------------- |\n| Bool | Bool (2) |\n| UInt8 | Byte (3) |\n| Double | Double (4) |\n| Int16 | Int16 (6) |\n| Int32 | Int32 (8) |\n| Int64 | Int64 (10) |\n| Data | String (11) |\n| String | String (11) | (UTF8 data) |\n| SomeStruct: ThriftCodable | Struct\n| Dictionary\u003cThriftCodable, ThriftCodable\u003e | Map\u003cThriftCodable, ThriftCodable\u003e (13) |\n| Set\u003cThriftCodable\u003e | Set\u003cThriftCodable\u003e (14) |\n| Array\u003cThriftCodable\u003e | List\u003cThriftCodable\u003e (15) |\n\n\nFor supported collection types, we have extensions to support the ThriftCodable \nprotocol. Thrift structs map to Swift structs. Swift structs are value types. These\nprevent misuse of the models and unexpected behavior. Structs add field ids as \ncoding keys on the structs, shown here:\n```\n/// ClassA.thrift\nstruct ClassA {\n  1: required string someString\n}\n\n/// ClassA.swift\npublic struct ClassA: ThriftCodable {\n  let someString: String\n  enum CodingKeys: Int, CodingKey {\n    case someString = 1\n  }\n}\n```\n\nThrift enums will be a Swift Enum with a Int32 raw value per the thrift specification. \nUnions will also be created as enums and will also contain coding keys for the field\nids. Unions are mutually exclusive types, making them as enums is ideal to prevent \nmisuse of the model. Here is an example:\n```\n/// MyUnion.thrift\nunion MyUnion {\n  1: UnionClassA unionCase1\n  2: UnionClassB unionCase2\n}\n\n/// MyUnion.swift\npublic enum MyUnion: ThriftCodable {\n  case unionCase1(UnionClassA)\n  case unionCase2(UnionClassB)\n\n  enum CodingKeys: Int, CodingKey {\n    case unionClassA = 1\n    case unionClassB = 2\n  }\n}\n```\n## Documentation\n https://twitter.github.io/ios-twitter-apache-thrift\n \n## Support\n\nCreate a [new issue](https://github.com/twitter/ios-twitter-apache-thrift/issues/new) on GitHub.\n\n## License\n\nCopyright 2020 Twitter, Inc.\n\nLicensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0\n\n## Resources\n\n[Thrift Specification](https://thrift.apache.org/static/files/thrift-20070401.pdf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fios-twitter-apache-thrift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwitter%2Fios-twitter-apache-thrift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fios-twitter-apache-thrift/lists"}