Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization annotations #139

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.akuleshov7.ktoml.annotations

import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Specifies comments to be applied the TOML element produced by a property during
* serialization. Has no effect on deserialization.
*
* ```kotlin
* @Serializable
* data class Data(
* @TomlComments(inline = "Inline")
* val x: Int,
* @TomlComments(
* "Descriptive comment 1",
* "Descriptive comment 2"
* )
* val y: Int
* )
*
* val data = Data(x = 3, y = 7)
* ```
*
* would produce:
*
* ```toml
* x = 3 # Inline
*
* # Descriptive comment 1
* # Descriptive comment 2
* y = 7
* ```
*
* @property lines Comment lines to be placed before the TOML element.
* @property inline A comment placed *inline* with the TOML element, at the end
* of the line. If empty (the default), no comment will be written.
*/
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(PROPERTY)
public annotation class TomlComments(
vararg val lines: String,
val inline: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.akuleshov7.ktoml.annotations

import kotlin.annotation.AnnotationTarget.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Marks a TOML element as an inline table. Has no effect on deserialization.
*
* ```kotlin
* @Serializable
* data class Data(
* @TomlInlineTable
* val inlineTable: Table,
* val tableArray: List<@TomlInlineTable Table>,
* val inlineTable2: Table2
* )
*
* @Serializable
* data class Table(val int: Int)
*
* @Serializable
* @TomlInlineTable
* data class Table2(val string: String)
*
* val data = Data(
* inlineTable = Table(int = -1),
* tableArray = listOf(
* Table(int = 3),
* Table(int = 10)
* ),
* inlineTable2 = Table2(string = "text")
* )
* ```
*
* would produce:
*
* ```toml
* inlineTable = { int = -1 }
* tableArray = [ { int = 3 }, { int = 10 } ]
* inlineTable2 = { string = "text" }
* ```
*/
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(
PROPERTY,
TYPE_PARAMETER,
CLASS
)
public annotation class TomlInlineTable
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.akuleshov7.ktoml.annotations

import com.akuleshov7.ktoml.writers.IntegerRepresentation
import com.akuleshov7.ktoml.writers.IntegerRepresentation.DECIMAL
import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Specifies how a TOML integer element is encoded. Has no effect on deserialization.
*
* ```kotlin
* @Serializable
* data class Data(
* @TomlInteger(HEX)
* val mask: Int,
* val perms: List<@TomlInteger(OCTAL) Int>
* )
*
* val data = Data(
* mask = 0x00FF,
* perms = listOf(0x1FF, 0x1ED, 0x1A4)
* )
* ```
*
* would produce:
*
* ```toml
* mask = 0x00FF
* perms = [ 0o777, 0o755, 0o644 ]
* ```
*
* @property representation How the integer is represented in TOML. The default
* behavior is [DECIMAL].
*/
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(PROPERTY, TYPE_PARAMETER)
public annotation class TomlInteger(
val representation: IntegerRepresentation = DECIMAL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.akuleshov7.ktoml.annotations

import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Marks a TOML string element as literal. Has no effect on deserialization.
*
* ```kotlin
* @Serializable
* data class Data(
* @TomlLiteral
* val text: String,
* val list: List<@TomlLiteral String>
* )
*
* val data = Data(
* text = "\"literal\"",
* list = listOf("\\[a-z]*\\")
* )
* ```
*
* would produce:
*
* ```toml
* text = '"literal"'
* list = [ '\[a-z]*\' ]
* ```
*/
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(PROPERTY, TYPE_PARAMETER)
public annotation class TomlLiteral
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.akuleshov7.ktoml.annotations

import kotlin.annotation.AnnotationTarget.PROPERTY
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Marks a TOML element and its children as multiline. Can be used on strings and
* arrays. Has no effect on deserialization.
*
* ```kotlin
* @Serializable
* data class Data(
* @TomlMultiline
* val multilineString: String,
* @TomlMultiline
* val multilineArray: List<Int>,
* val multilineStringArray: List<@TomlMultiline String>,
* @TomlMultiline
* val multilineStringArray2: List<String>
* )
*
* val data = Data(text = "Some\nText", list = listOf(3, 5, 7, 11))
* ```
*
* would produce:
*
* ```toml
* multilineString = """
* Some
* Text
* """
*
* multilineArray = [
* 3,
* 5,
* 7,
* 11
* ]
*
* multilineStringArray = [ """
* string 1
* """,
* """
* string 2
* """ ]
*
* multilineStringArray2 = [
* """
* string 1
* """,
* """
* string 2
* """
* ]
* ```
*/
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(PROPERTY, TYPE_PARAMETER)
public annotation class TomlMultiline