Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2019-2022 Leon Linhart
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@file:Suppress("FunctionName")
package com.gw2tb.apigen.internal.dsl
import com.gw2tb.apigen.internal.impl.*
import com.gw2tb.apigen.model.*
import com.gw2tb.apigen.model.v2.*
import com.gw2tb.apigen.schema.*
/** Alias for [SchemaBoolean]. */
internal val BOOLEAN: DeferredPrimitiveType = DeferredPrimitiveType(SchemaBoolean())
/** Alias for [SchemaDecimal]. */
internal val DECIMAL: DeferredPrimitiveType = DeferredPrimitiveType(SchemaDecimal())
/** Alias for [SchemaInteger]. */
internal val INTEGER: DeferredPrimitiveType = DeferredPrimitiveType(SchemaInteger())
/** Alias for [SchemaString]. */
internal val STRING: DeferredPrimitiveType = DeferredPrimitiveType(SchemaString())
internal fun DeferredSchemaType(factory: (TypeRegistryScope?, Boolean) -> SchemaVersionedData): DeferredSchemaType = object : DeferredSchemaType() {
override fun get(typeRegistry: TypeRegistryScope?, interpretationHint: InterpretationHint?, isTopLevel: Boolean): SchemaVersionedData = factory(typeRegistry, isTopLevel)
}
internal abstract class DeferredSchemaType {
abstract fun get(typeRegistry: TypeRegistryScope?, interpretationHint: InterpretationHint?, isTopLevel: Boolean = false): SchemaVersionedData
fun getFlat(): T = get(typeRegistry = null, interpretationHint = null).single().data
}
internal data class DeferredPrimitiveType(
private val value: SchemaPrimitive,
) : DeferredSchemaType() {
override fun get(typeRegistry: TypeRegistryScope?, interpretationHint: InterpretationHint?, isTopLevel: Boolean): SchemaVersionedData {
return wrapVersionedSchemaData(value)
}
fun withTypeHint(typeHint: SchemaPrimitive.TypeHint?): DeferredPrimitiveType =
copy(value = value.withTypeHint(typeHint = typeHint))
}
@APIGenDSL
internal abstract class DeferredSchemaClass : DeferredSchemaType() {
abstract val name: String
abstract val apiTypeFactory: (SchemaVersionedData, InterpretationHint?, Boolean) -> T
abstract val typeRegistry: TypeRegistryScope?
open val nestedTypeRegistry: TypeRegistryScope? get() = typeRegistry?.nestedScope(name)
fun array(
items: DeferredSchemaType,
nullableItems: Boolean = false
): DeferredSchemaType =
DeferredSchemaType { typeRegistry, isTopLevel -> items.get(typeRegistry, interpretationHint = null, isTopLevel).mapData { SchemaArray(it, nullableItems, description = null) } }
fun map(
keys: DeferredSchemaType,
values: DeferredSchemaType,
nullableValues: Boolean = false
): DeferredSchemaType =
DeferredSchemaType { typeRegistry, isTopLevel -> values.get(typeRegistry, interpretationHint = null, isTopLevel).mapData { SchemaMap(keys.getFlat(), it, nullableValues, description = null) } }
fun conditional(
name: String,
description: String,
disambiguationBy: String = "type",
disambiguationBySideProperty: Boolean = false,
interpretationInNestedProperty: Boolean = false,
sharedConfigure: (AbstractSchemaRecordBuilder.() -> Unit)? = null,
configure: SchemaConditionalBuilder.() -> Unit
): DeferredSchemaClass = conditionalImpl(
name,
description,
disambiguationBy,
disambiguationBySideProperty,
interpretationInNestedProperty,
sharedConfigure,
apiTypeFactory,
nestedTypeRegistry,
configure
)
fun record(
name: String,
description: String,
block: SchemaRecordBuilder.() -> Unit
): DeferredSchemaClass = recordImpl(
name,
description,
apiTypeFactory,
nestedTypeRegistry,
block
)
}
internal class SchemaConditionalBuilder(
override val name: String,
private val description: String,
private val disambiguationBy: String,
private val disambiguationBySideProperty: Boolean,
private val interpretationInNestedProperty: Boolean,
private val sharedConfigure: (AbstractSchemaRecordBuilder.() -> Unit)?,
override val apiTypeFactory: (SchemaVersionedData, InterpretationHint?, Boolean) -> T,
override val typeRegistry: TypeRegistryScope?
) : DeferredSchemaClass() {
private val _interpretations = mutableListOf()
private fun buildInterpretations(conditionalBase: TypeLocation, typeRegistry: TypeRegistryScope?): SchemaVersionedData