在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):streem/pbandk开源软件地址(OpenSource Url):https://github.com/streem/pbandk开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):PbandkPbandk is a Kotlin code generator and runtime for Protocol Buffers. It is built to work across multiple Kotlin platforms. NOTE: This is the documentation for the version of pbandk currently in development. Documentation for the latest stable version is available at https://github.com/streem/pbandk/blob/v0.14.1/README.md. Features
Experimental
Not Yet Implemented
Read below for more information and see the examples. StatusThis project is currently in beta. It has the core set of protobuf features implemented and is being used in production. But it is still under active development and new versions might introduce backwards-incompatible changes to support new features or to improve the library's usability in Kotlin. Pull requests are welcome for any of the "Not Yet Implemented" features above. This project follows semantic versioning. After v1.0.0 is released (mid-2022 at the earliest), future versions will preserve backwards compatibility. SummaryGenerated Code SampleFor the following syntax = "proto3";
package tutorial;
import "google/protobuf/timestamp.proto";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
google.protobuf.Timestamp last_updated = 5;
}
message AddressBook {
repeated Person people = 1;
} The following file will be generated at @file:OptIn(pbandk.PublicForGeneratedCode::class)
package tutorial
public data class Person(
val name: String = "",
val id: Int = 0,
val email: String = "",
val phones: List<tutorial.Person.PhoneNumber> = emptyList(),
val lastUpdated: pbandk.wkt.Timestamp? = null,
override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
override operator fun plus(other: pbandk.Message?): Person = protoMergeImpl(other)
override val descriptor: pbandk.MessageDescriptor<Person> get() = Companion.descriptor
override val protoSize: Int by lazy { super.protoSize }
public companion object : pbandk.Message.Companion<Person> {
public val defaultInstance: Person by lazy { Person() }
override fun decodeWith(u: pbandk.MessageDecoder): Person = Person.decodeWithImpl(u)
override val descriptors: pbandk.MessageDescriptor<Person> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<Person, *>>(5).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "name",
number = 1,
type = pbandk.FieldDescriptor.Type.Primitive.String(),
jsonName = "name",
value = Person::name
)
)
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "id",
number = 2,
type = pbandk.FieldDescriptor.Type.Primitive.Int32(),
jsonName = "id",
value = Person::id
)
)
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "email",
number = 3,
type = pbandk.FieldDescriptor.Type.Primitive.String(),
jsonName = "email",
value = Person::email
)
)
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "phones",
number = 4,
type = pbandk.FieldDescriptor.Type.Repeated<tutorial.Person.PhoneNumber>(valueType = pbandk.FieldDescriptor.Type.Message(messageCompanion = tutorial.Person.PhoneNumber.Companion)),
jsonName = "phones",
value = Person::phones
)
)
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "last_updated",
number = 5,
type = pbandk.FieldDescriptor.Type.Message(messageCompanion = pbandk.wkt.Timestamp.Companion),
jsonName = "lastUpdated",
value = Person::lastUpdated
)
)
}
pbandk.MessageDescriptor(
fullName = "tutorial.Person",
messageClass = tutorial.Person::class,
messageCompanion = this,
fields = fieldsList
)
}
}
public sealed class PhoneType(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is Person.PhoneType && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "Person.PhoneType.${name ?: "UNRECOGNIZED"}(value=$value)"
public object MOBILE : PhoneType(0, "MOBILE")
public object HOME : PhoneType(1, "HOME")
public object WORK : PhoneType(2, "WORK")
public class UNRECOGNIZED(value: Int) : Person.PhoneType(value)
public companion object : pbandk.Message.Enum.Companion<Person.PhoneType> {
public val values: List<Person.PhoneType> by lazy { listOf(MOBILE, HOME, WORK) }
override fun fromValue(value: Int): Person.PhoneType = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): Person.PhoneType = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No PhoneType with name: $name")
}
}
public data class PhoneNumber(
val number: String = "",
val type: tutorial.Person.PhoneType = tutorial.Person.PhoneType.fromValue(0),
override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
override operator fun plus(other: pbandk.Message?): Person.PhoneNumber = protoMergeImpl(other)
override val descriptor: MessageDescriptor<Person.PhoneNumber> get() = Companion.descriptor
override val protoSize: Int by lazy { super.protoSize }
public companion object : pbandk.Message.Companion<Person.PhoneNumber> {
public val defaultInstance: Person.PhoneNumber by lazy { Person.PhoneNumber() }
override fun decodeWith(u: pbandk.MessageDecoder): Person.PhoneNumber = Person.PhoneNumber.decodeWithImpl(u)
override val descriptor: pbandk.MessageDescriptor<PhoneNumber> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<PhoneNumber, *>>(2).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "number",
number = 1,
type = pbandk.FieldDescriptor.Type.Primitive.String(),
jsonName = "number",
value = PhoneNumber::number
)
)
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "type",
number = 2,
type = pbandk.FieldDescriptor.Type.Enum(enumCompanion = tutorial.Person.PhoneType.Companion),
jsonName = "type",
value = PhoneNumber::type
)
)
}
pbandk.MessageDescriptor(
fullName = "tutorial.Person.PhoneNumber",
messageClass = tutorial.Person.PhoneNumber::class,
messageCompanion = this,
fields = fieldsList
)
}
}
}
}
public data class AddressBook(
val people: List<tutorial.Person> = emptyList(),
override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
override operator fun plus(other: pbandk.Message?): AddressBook = protoMergeImpl(other)
override val descriptor: MessageDescriptor<AddressBook> get() = Companion.descriptor
override val protoSize: Int by lazy { super.protoSize }
public companion object : pbandk.Message.Companion<AddressBook> {
public val defaultInstance: AddressBook by lazy { AddressBook() }
override fun decodeWith(u: pbandk.MessageDecoder): AddressBook = AddressBook.decodeWithImpl(u)
override val descriptor: pbandk.MessageDescriptor<AddressBook> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<AddressBook, *>>(1).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "people",
number = 1,
type = pbandk.FieldDescriptor.Type.Repeated<tutorial.Person>(valueType = pbandk.FieldDescriptor.Type.Message(messageCompanion = tutorial.Person.Companion)),
jsonName = "people",
value = AddressBook::people
)
)
}
pbandk.MessageDescriptor(
fullName = "tutorial.AddressBook",
messageClass = tutorial.AddressBook::class,
messageCompanion = this,
fields = fieldsList
)
}
}
}
public fun Person?.orDefault(): Person = this ?: Person.defaultInstance
public fun Person.PhoneNumber?.orDefault(): Person.PhoneNumber = this ?: Person.PhoneNumber.defaultInstance
public fun AddressBook?.orDefault(): AddressBook = this ?: AddressBook.defaultInstance
// Omitted multiple supporting private extension methods To see a full version of the file, see here. See the "Generated Code" section below under "Usage" for more details. UsageGenerating CodePbandk's code generator leverages
The file is generated as
Multiple options can be added to a single In addition to running WindowsThe self-executing jar file doesn't work on Windows. Also
And then provide the full path to
Runtime LibraryPbandk's runtime library provides a Kotlin layer over the preferred Protobuf library for each platform. The libraries are present on Maven Central. Using Gradle:
Pbandk has a dependency on the preferred Protobuf library on each platform:
In addition, support for Kotlin's
Service Code GenerationPbandk does not generate gRPC code itself, but offers a To do this, first depend on the project but it will only be needed at compile time because it's already there at runtime:
Then, the
For more details, see the custom-service-gen example. Generated CodePackageThe package is either the MessageEach Protobuf message extends Messages are immutable Kotlin data classes. This means they automatically implement For proto3, the only nullable fields are other messages and oneof fields. Other values have defaults. For proto2,
optional fields are nullable and defaulted as such. Types are basically the same as they would be in Java. However,
Regardless of OneofOneof fields are generated as nested classes of a common sealed base class. Each oneof inner field is a class that wraps a single value. The parent message also contains a nullable field for every oneof inner field. This field resolves to the oneof inner field's value when the oneof is set to that inner field. Otherwise it resolves to null. EnumEnum fields are generated as sealed classes with a nested Each enum object contains a The |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论