Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Commit

Permalink
- Dropped klaxon, using kotlinx.serialization instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Doomsdayrs committed Dec 19, 2020
1 parent cdeda8d commit 4c28f7f
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 192 deletions.
10 changes: 6 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "Kotlin library for shosetsu"
plugins {
kotlin("jvm") version "1.4.20"
id("org.jetbrains.dokka") version "0.10.0"
kotlin("plugin.serialization") version "1.4.20"
maven
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { kotlinOptions.jvmTarget = "1.8" }
Expand All @@ -20,7 +21,6 @@ tasks.dokka {
val dokkaJar by tasks.creating(Jar::class) {
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "Assembles Kotlin docs with Dokka"
classifier = "javadoc"
}

repositories {
Expand All @@ -33,13 +33,15 @@ dependencies {
implementation(kotlin("stdlib"))
implementation("org.jsoup:jsoup:1.12.1")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.20")

// java only
implementation("org.luaj:luaj-jse:3.0.1")
implementation("com.beust:klaxon:5.0.1")
implementation("com.squareup.okhttp3:okhttp:4.2.1")
implementation("com.google.guava:guava:30.0-jre")
testImplementation("junit:junit:4.12")

// implementation("org.jetbrains.kolin:kotlin-test:v1.3.61")
// Cross platform confirmed
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")
testImplementation("org.jetbrains.kotlin:kotlin-test:1.4.20")
}

val compileTestKotlin: KotlinCompile by tasks
Expand Down
23 changes: 19 additions & 4 deletions src/main/kotlin/app/shosetsu/lib/IExtension.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package app.shosetsu.lib

import app.shosetsu.lib.json.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/*
* This file is part of shosetsu-services.
* shosetsu-services is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -32,16 +36,27 @@ interface IExtension {
}

/**
* @param id ID
* @param version 3 index array
*
* @param libVersion Version of the library this extension was made for
* @param author Author / Creator of this extension
* @param repo Repository URL that this extension is connected with
* @param dependencies
*/
@Serializable
data class ExMetaData(
@SerialName(J_ID)
val id: Int,
@SerialName(J_VERSION)
val version: Version,
@SerialName(J_LIB_VERSION)
val libVersion: Version,
val author: String,
val repo: String,
val dependencies: Array<Pair<String, Version>>
@SerialName(J_AUTHOR)
val author: String = "",
@SerialName(J_REPO)
val repo: String = "",
@SerialName(J_DEP)
val dependencies: Map<String, Version> = mapOf()
)

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/app/shosetsu/lib/Version.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app.shosetsu.lib

import kotlinx.serialization.Serializable


/**
* Version of the kotlin-lib shosetsu is currently using
Expand All @@ -16,11 +18,14 @@ val KOTLIN_LIB_VERSION = Version(1, 0, 0)
* @param minor Incremented with feature releases that don't destroy backwards compatibility
* @param patch Incremented with bug fixes that don't destroy backwards compatibility
*/
@Serializable(with = VersionSerializer::class)
data class Version(
val major: Int,
val minor: Int,
val patch: Int
) : Comparable<Version> {


constructor(array: Array<Int>) : this(array[0], array[1], array[2])

constructor(
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/app/shosetsu/lib/VersionSerializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app.shosetsu.lib

import app.shosetsu.lib.json.J_VERSION
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind.STRING
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* [KSerializer] for [Version], to convert it from and to string
*/
internal class VersionSerializer : KSerializer<Version> {
override fun deserialize(decoder: Decoder): Version = Version(decoder.decodeString())

override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(J_VERSION, STRING)

override fun serialize(encoder: Encoder, value: Version) {
encoder.encodeString(value.toString())
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/app/shosetsu/lib/json/RepoData.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package app.shosetsu.lib.json

import app.shosetsu.lib.Version
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* 17 / 10 / 2020
*
* Represents an extension on the repository,
* listed as "scripts" in the index
*
* @param id Identification of this extension, this should always be unique
* @param name Name of the extension that is presentable to the user
* @param fileName FileName of the extension
* @param imageURL Image URL for this extension, for visual identification
* @param lang String identification of the language via (ISO 639-1) standard
* @param version Version of this extension
* @param libVersion Version the extension is compatible with
* @param md5 MD5 coding of the extension, for security checking
*/
@Serializable
data class RepoExtension internal constructor(
@SerialName(J_ID)
val id: Int,
@SerialName(J_NAME)
val name: String,
@SerialName(J_FILE_NAME)
val fileName: String,
@SerialName(J_IMAGE_URL)
val imageURL: String,
@SerialName(J_LANGUAGE)
val lang: String,
@SerialName(J_VERSION)
val version: Version,
@SerialName(J_LIB_VERSION)
val libVersion: Version,
@SerialName(J_MD5)
val md5: String
)

Expand All @@ -24,8 +44,15 @@ data class RepoExtension internal constructor(
* 17 / 10 / 2020
*
* Represents a library listed in `libraries` within the repository index
*
* @param name Name of the library, this is always the same as the filename
*
* @param version Version of the library
*/
@Serializable
data class RepoLibrary internal constructor(
@SerialName(J_NAME)
val name: String,
@SerialName(J_VERSION)
val version: Version
)
85 changes: 10 additions & 75 deletions src/main/kotlin/app/shosetsu/lib/json/RepoIndex.kt
Original file line number Diff line number Diff line change
@@ -1,94 +1,29 @@
package app.shosetsu.lib.json

import app.shosetsu.lib.Version
import app.shosetsu.lib.exceptions.JsonMissingKeyException
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.io.File
import java.io.FileReader
import java.io.StringReader

/**
* shosetsu-kotlin-lib
* 17 / 10 / 2020
* @param libraries Libraries used by the repository
* @param extensions Extensions listed in this repository
*/
@Serializable
data class RepoIndex internal constructor(
@SerialName(LIBS_KEY)
val libraries: List<RepoLibrary>,

@SerialName(SCPS_KEY)
val extensions: List<RepoExtension>
) {
companion object {
const val LIBS_KEY = "libraries"
const val SCPS_KEY = "scripts"
}

constructor(jsonFile: File) : this(
Parser.default().parse(FileReader(jsonFile)) as JsonObject
)

constructor(jsonString: String) : this(
Parser.default().parse(StringReader(jsonString)) as JsonObject
)

// Using .toAndroid() to provide android compatiblity
internal constructor(json: JsonObject) : this(
json.array<JsonObject>(LIBS_KEY)?.map {
RepoLibrary(
name = it.string(J_NAME) ?: throw JsonMissingKeyException(
LIBS_KEY,
J_NAME
),
version = Version(
it.string(J_VERSION) ?: throw JsonMissingKeyException(
LIBS_KEY,
J_VERSION
)
)
)
} ?: emptyList(),
json.array<JsonObject>(SCPS_KEY)?.map {
RepoExtension(
id = it.int(J_ID) ?: throw JsonMissingKeyException(
SCPS_KEY,
J_ID
),
name = it.string(J_NAME) ?: throw JsonMissingKeyException(
SCPS_KEY,
J_NAME
),
fileName = it.string(J_FILE_NAME)
?: throw JsonMissingKeyException(
SCPS_KEY,
J_FILE_NAME
),
imageURL = it.string(J_IMAGE_URL)
?: throw JsonMissingKeyException(
SCPS_KEY,
J_IMAGE_URL
),
lang = it.string(J_LANGUAGE) ?: throw JsonMissingKeyException(
SCPS_KEY,
J_LANGUAGE
),
version = Version(
it.string(J_VERSION) ?: throw JsonMissingKeyException(
SCPS_KEY,
J_VERSION
)
),
libVersion = Version(
it.string(J_LIB_VERSION)
?: throw JsonMissingKeyException(
SCPS_KEY,
J_LIB_VERSION
)
),
md5 = it.string(J_MD5) ?: throw JsonMissingKeyException(
SCPS_KEY,
J_MD5
)
)
} ?: emptyList()
)
fun fromString(json: String): RepoIndex = Json.decodeFromString(json)
}
}
24 changes: 3 additions & 21 deletions src/main/kotlin/app/shosetsu/lib/lua/LuaExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package app.shosetsu.lib.lua
import app.shosetsu.lib.*
import app.shosetsu.lib.exceptions.InvalidFilterIDException
import app.shosetsu.lib.exceptions.MissingOrInvalidKeysException
import app.shosetsu.lib.json.*
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.luaj.vm2.LuaString.EMPTYSTRING
import org.luaj.vm2.LuaTable
import org.luaj.vm2.LuaValue
import org.luaj.vm2.LuaValue.*
import org.luaj.vm2.lib.jse.CoerceJavaToLua.coerce
import org.luaj.vm2.lib.jse.CoerceLuaToJava
import java.io.File
import java.io.StringReader

/*
* This file is part of shosetsu-services.
Expand Down Expand Up @@ -99,23 +97,7 @@ class LuaExtension(
*/
@Suppress("unused")
override val exMetaData: IExtension.ExMetaData by lazy {
val metaString = content.lines().first()
.replace("--", "").trim()
val json =
Parser.default().parse(StringReader(metaString)) as JsonObject
IExtension.ExMetaData(
id = json.int(J_ID)!!,
version = Version(json.string(J_VERSION)!!),
libVersion = Version(json.string(J_LIB_VERSION)!!),
author = json.string(J_AUTHOR)!!,
repo = json.string(J_REPO) ?: "",
// Using .toAndroid() to provide android compatiblity
dependencies = json.array<String>(J_DEP)?.map {
it.split(">=").let { split ->
split[0] to Version(split[1])
}
}?.toTypedArray() ?: arrayOf()
)
Json.decodeFromString(content.lines().first().replaceFirst("--", "").trim())
}

private val source: LuaTable
Expand Down
Loading

0 comments on commit 4c28f7f

Please sign in to comment.