Skip to content

Commit

Permalink
Dinosaur readme
Browse files Browse the repository at this point in the history
  • Loading branch information
lickel committed Aug 16, 2023
1 parent e6ee8d9 commit dfd3443
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,22 @@ public struct Dinosaur {
/**
* URLs with images of this dinosaur.
*/
public var picture_urls: [String]
public var picture_urls: [String] = []
public var length_meters: Double?
public var mass_kilograms: Double?
public var period: Period?
public var unknownFields: Data = .init()
public var unknownFields: Foundation.Data = .init()

public init(configure: (inout Self) -> Void = { _ in }) {
configure(&self)
}

}

#if WIRE_INCLUDE_MEMBERWISE_INITIALIZER
extension Dinosaur {

@_disfavoredOverload
public init(
name: String? = nil,
picture_urls: [String] = [],
Expand All @@ -596,6 +606,7 @@ public struct Dinosaur {
}

}
#endif

#if !WIRE_REMOVE_EQUATABLE
extension Dinosaur : Equatable {
Expand All @@ -613,13 +624,16 @@ extension Dinosaur : Sendable {
#endif

extension Dinosaur : ProtoMessage {

public static func protoMessageTypeURL() -> String {
return "type.googleapis.com/squareup.dinosaurs.Dinosaur"
}

}

extension Dinosaur : Proto2Codable {
public init(from reader: ProtoReader) throws {

public init(from reader: Wire.ProtoReader) throws {
var name: String? = nil
var picture_urls: [String] = []
var length_meters: Double? = nil
Expand All @@ -646,20 +660,22 @@ extension Dinosaur : Proto2Codable {
self.period = period
}

public func encode(to writer: ProtoWriter) throws {
public func encode(to writer: Wire.ProtoWriter) throws {
try writer.encode(tag: 1, value: self.name)
try writer.encode(tag: 2, value: self.picture_urls)
try writer.encode(tag: 3, value: self.length_meters)
try writer.encode(tag: 4, value: self.mass_kilograms)
try writer.encode(tag: 5, value: self.period)
try writer.writeUnknownFields(unknownFields)
}

}

#if !WIRE_REMOVE_CODABLE
extension Dinosaur : Codable {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringLiteralCodingKeys.self)
let container = try decoder.container(keyedBy: Wire.StringLiteralCodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: "name")
self.picture_urls = try container.decodeProtoArray(String.self, firstOfKeys: "pictureUrls", "picture_urls")
self.length_meters = try container.decodeIfPresent(Double.self, firstOfKeys: "lengthMeters", "length_meters")
Expand All @@ -668,7 +684,7 @@ extension Dinosaur : Codable {
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringLiteralCodingKeys.self)
var container = encoder.container(keyedBy: Wire.StringLiteralCodingKeys.self)
let preferCamelCase = encoder.protoKeyNameEncodingStrategy == .camelCase
let includeDefaults = encoder.protoDefaultValuesEncodingStrategy == .include

Expand All @@ -680,17 +696,18 @@ extension Dinosaur : Codable {
try container.encodeIfPresent(self.mass_kilograms, forKey: preferCamelCase ? "massKilograms" : "mass_kilograms")
try container.encodeIfPresent(self.period, forKey: "period")
}

}
#endif
```

Creating and accessing proto models is easy:

```swift
let stegosaurus = Dinosaur(
name: "Stegosaurus",
period: .JURASSIC
)
let stegosaurus = Dinosaur {
$0.name = "Stegosaurus"
$0.period = .JURASSIC
}

print("My favorite dinosaur existed in the \(stegosaurus.period) period.")
```
Expand Down

0 comments on commit dfd3443

Please sign in to comment.