Skip to content

Commit d53588a

Browse files
authored
Actualize Protobuf documentation (#2355)
Fixes #2352
1 parent b0af403 commit d53588a

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

dokka/moduledoc.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can learn about "Human-Optimized Config Object Notation" or HOCON from libra
2121
Allows converting arbitrary hierarchy of Kotlin classes to a flat key-value structure à la Java Properties.
2222

2323
# Module kotlinx-serialization-protobuf
24-
Protocol buffers serialization format implementation, mostly compliant to [proto2](https://developers.google.com/protocol-buffers/docs/proto) specification.
24+
[Protocol buffers](https://protobuf.dev/) serialization format implementation.
2525

2626
# Package kotlinx.serialization
2727
Basic core concepts and annotations that set up serialization process.
@@ -50,7 +50,10 @@ and JSON-specific serializers.
5050
Extensions for kotlinx.serialization.json.Json for integration with the popular [Okio](https://square.github.io/okio/) library.
5151

5252
# Package kotlinx.serialization.protobuf
53-
Protocol buffers serialization format implementation, mostly compliant to [proto2](https://developers.google.com/protocol-buffers/docs/proto) specification.
53+
[Protocol buffers](https://protobuf.dev/) serialization format implementation.
54+
55+
# Package kotlinx.serialization.protobuf.schema
56+
Experimental generator of ProtoBuf schema from Kotlin classes.
5457

5558
# Package kotlinx.serialization.properties
5659
Properties serialization format implementation that represents the input data as a plain map of properties.

formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/ProtoBuf.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import kotlin.js.*
1111

1212
/**
1313
* Implements [encoding][encodeToByteArray] and [decoding][decodeFromByteArray] classes to/from bytes
14-
* using [Proto2][https://developers.google.com/protocol-buffers/docs/proto] specification.
15-
* It is typically used by constructing an application-specific instance, with configured specific behaviour
14+
* using [Protocol buffers](https://protobuf.dev/) specification.
15+
* It is typically used by constructing an application-specific instance, with configured specific behavior
1616
* and, if necessary, registered custom serializers (in [SerializersModule] provided by [serializersModule] constructor parameter).
17+
* Default encoding is proto2, although proto3 can be used with a number of tweaks (see the section below for details).
18+
*
1719
*
1820
* ### Correspondence between Protobuf message definitions and Kotlin classes
19-
* Given a ProtoBuf definition with one required field, one optional field and one optional field with a custom default
21+
* Given a ProtoBuf definition with one required field, one optional field, and one optional field with a custom default
2022
* value:
2123
* ```
2224
* message MyMessage {
@@ -32,27 +34,29 @@ import kotlin.js.*
3234
* data class MyMessage(val first: Int, val second: Int = 0, val third: Int = 42)
3335
* ```
3436
*
35-
* By default, protobuf fields ids are being assigned to Kotlin properties in incremental order, i.e.
36-
* the first property in the class has id 1, the second has id 2, and so forth.
37-
* If you need a more stable order (e.g. to avoid breaking changes when reordering properties),
38-
* provide custom ids using [ProtoNumber] annotation.
37+
* By default, protobuf fields numbers are being assigned to Kotlin properties in incremental order, i.e.,
38+
* the first property in the class has number 1, the second has number 2, and so forth.
39+
* If you need a more stable order (e.g., to avoid breaking changes when reordering properties),
40+
* provide custom numbers using [ProtoNumber] annotation.
3941
*
40-
* By default, all integer numbers are encoded using [varint][https://developers.google.com/protocol-buffers/docs/encoding#varints]
41-
* encoding. This behaviour can be changed via [ProtoType] annotation.
42+
* By default, all integer values are encoded using [varint](https://protobuf.dev/programming-guides/encoding/#varints)
43+
* encoding. This behavior can be changed via [ProtoType] annotation.
4244
*
4345
* ### Known caveats and limitations
4446
* Lists are represented as repeated fields. Because format spec says that if the list is empty,
45-
* there are no elements in the stream with such tag, you must explicitly mark any
46-
* field of list type with default = emptyList(). Same for maps.
47-
* There's no special support for `oneof` protobuf fields. However, this implementation
47+
* there are no elements in the stream with such tag, you have to explicitly add to any
48+
* property of `List` type a default value equals to `emptyList()`. Same for maps.
49+
* There is no special support for `oneof` protobuf fields. However, this implementation
4850
* supports standard kotlinx.serialization's polymorphic and sealed serializers,
49-
* using their default form (message of serialName: string and other embedded message with actual content).
51+
* using their default form (message consisting of `serialName: string` and other embedded message with actual content).
5052
*
5153
* ### Proto3 support
52-
* This implementation does not support repeated packed fields, so you won't be able to deserialize
53-
* Proto3 lists. However, other messages could be decoded. You have to remember that since fields in Proto3
54-
* messages by default are implicitly optional,
55-
* corresponding Kotlin properties have to be nullable with default value `null`.
54+
*
55+
* proto2 and proto3 specifications use the same encoding, so you can use this class to decode Proto3 messages.
56+
* However, the message structure is slightly different, so you should remember the following:
57+
*
58+
* - In proto3, fields by default are implicitly optional, so corresponding Kotlin properties have to be nullable and have a default value `null`.
59+
* - In proto3, all lists use packed encoding by default. To be able to decode them, annotation [ProtoPacked] should be used on all properties with type `List`.
5660
*
5761
* ### Usage example
5862
* ```
@@ -112,6 +116,9 @@ import kotlin.js.*
112116
* @param encodeDefaults specifies whether default values are encoded.
113117
* False by default; meaning that properties with values equal to defaults will be elided.
114118
* @param serializersModule application-specific [SerializersModule] to provide custom serializers.
119+
* @see ProtoNumber
120+
* @see ProtoType
121+
* @see ProtoPacked
115122
*/
116123
@ExperimentalSerializationApi
117124
public sealed class ProtoBuf(

formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/ProtoTypes.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ import kotlinx.serialization.descriptors.*
1111
* Specifies protobuf field number (a unique number for a field in the protobuf message)
1212
* assigned to a Kotlin property.
1313
*
14-
* See [https://developers.google.com/protocol-buffers/docs/proto#assigning-field-numbers]
14+
* See [Assigning field numbers](https://protobuf.dev/programming-guides/proto2/#assigning) for details.
1515
*/
1616
@SerialInfo
1717
@Target(AnnotationTarget.PROPERTY)
1818
@ExperimentalSerializationApi
1919
public annotation class ProtoNumber(public val number: Int)
2020

2121
/**
22-
* Represents a number format in protobuf encoding.
22+
* Represents a number format in protobuf encoding set by [ProtoType] annotation.
2323
*
2424
* [DEFAULT] is default varint encoding (intXX),
2525
* [SIGNED] is signed ZigZag representation (sintXX), and
2626
* [FIXED] is fixedXX type.
2727
* uintXX and sfixedXX are not supported yet.
2828
*
29-
* See [https://developers.google.com/protocol-buffers/docs/proto#scalar]
30-
* @see ProtoType
29+
* See [Scalar value types](https://protobuf.dev/programming-guides/proto2/#scalar) for details.
3130
*/
3231
@Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING")
3332
@ExperimentalSerializationApi
@@ -48,7 +47,7 @@ public annotation class ProtoType(public val type: ProtoIntegerType)
4847

4948

5049
/**
51-
* Instructs that a particular collection should be written as [packed array](https://developers.google.com/protocol-buffers/docs/encoding#packed)
50+
* Instructs that a particular collection should be written as a [packed array](https://protobuf.dev/programming-guides/encoding/#packed).
5251
*/
5352
@SerialInfo
5453
@Target(AnnotationTarget.PROPERTY)

0 commit comments

Comments
 (0)