commonMain.pro.felixo.protobuf.schemadocument.SchemaDocument.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protobuf-kotlin-schemadocument-jvm Show documentation
Show all versions of protobuf-kotlin-schemadocument-jvm Show documentation
Protocol Buffers 3 support for Kotlin Multiplatform
The newest version!
package pro.felixo.protobuf.schemadocument
import pro.felixo.protobuf.EnumValue
import pro.felixo.protobuf.FieldNumber
import pro.felixo.protobuf.FieldRule
import pro.felixo.protobuf.Identifier
/**
* Common interface implemented by all the elements of a [SchemaDocument]. It serves to facilitate the
* generation of useful validation error messages.
*/
interface SchemaElement {
val elementType: String
val elementName: String
}
/**
* Represents a .proto file and describes the declarations therein. Note that only a subset of the .proto syntax is
* supported.
*/
data class SchemaDocument(
val types: List = emptyList()
) : SchemaElement {
override val elementType = "schema"
override val elementName: String = "root"
override fun toString(): String {
val out = StringBuilder()
SchemaDocumentWriter(out).write(this)
return out.toString()
}
}
/**
* Represents a type declaration, which may be a [Message] or an [Enum].
*/
sealed class Type : SchemaElement {
abstract val name: Identifier
override val elementName: String get() = name.toString()
override fun toString(): String {
val out = StringBuilder()
SchemaDocumentWriter(out).write(this)
return out.toString()
}
}
/**
* Represents a message declaration.
*/
data class Message(
override val name: Identifier,
val members: List = emptyList(),
val nestedTypes: List = emptyList(),
val reservedNames: List = emptyList(),
val reservedNumbers: List = emptyList()
) : Type() {
override val elementType = "message"
}
/**
* Returns all the fields of this message, including those inside oneof declarations.
*/
val Message.fields: List get() =
(members.filterIsInstance() + members.filterIsInstance().flatMap { it.fields })
/**
* Represents a member of a [Message], which may be a [Field] or a [OneOf].
*/
sealed class Member : SchemaElement {
abstract val name: Identifier
override val elementName: String get() = name.toString()
}
/**
* Represents a field declaration inside a [Message] or a [OneOf].
*/
data class Field(
override val name: Identifier,
val type: FieldType,
val number: FieldNumber,
val rule: FieldRule = FieldRule.Singular
) : Member() {
override val elementType = "field"
}
/**
* Represents a oneof declaration.
*/
data class OneOf(
override val name: Identifier,
val fields: List
) : Member() {
override val elementType = "oneof"
}
/**
* Represents an enum declaration.
*/
data class Enum(
override val name: Identifier,
val values: List,
val allowAlias: Boolean = false,
val reservedNames: List = emptyList(),
val reservedNumbers: List = emptyList()
) : Type() {
override val elementType = "enum"
}
/**
* The declared type of a [Field].
*/
sealed class FieldType {
sealed class Scalar(val name: kotlin.String) : FieldType() {
override fun toString() = name
}
sealed class Integer32(name: kotlin.String) : Scalar(name)
sealed class Integer64(name: kotlin.String) : Scalar(name)
object Double : Scalar("double")
object Float : Scalar("float")
object Int32 : Integer32("int32")
object Int64 : Integer64("int64")
object UInt32 : Integer32("uint32")
object UInt64 : Integer64("uint64")
object SInt32 : Integer32("sint32")
object SInt64 : Integer64("sint64")
object Fixed32 : Integer32("fixed32")
object Fixed64 : Integer64("fixed64")
object SFixed32 : Integer32("sfixed32")
object SFixed64 : Integer64("sfixed64")
object Bool : Scalar("bool")
object String : Scalar("string")
object Bytes : Scalar("bytes")
/**
* A reference to a message or enum type.
*/
data class Reference(val components: List) : FieldType() {
override fun toString() = components.joinToString(".")
}
}
/**
* All [FieldType]s that are [FieldType.Scalar]s.
*/
val SCALARS: List> = listOf(
FieldType.Double,
FieldType.Float,
FieldType.Int32,
FieldType.Int64,
FieldType.UInt32,
FieldType.UInt64,
FieldType.SInt32,
FieldType.SInt64,
FieldType.Fixed32,
FieldType.Fixed64,
FieldType.SFixed32,
FieldType.SFixed64,
FieldType.Bool,
FieldType.String,
FieldType.Bytes
)