Skip to content

Commit

Permalink
Split OpenEnum, and AnyOf. Flatten Union.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jun 10, 2024
1 parent f246b02 commit feace36
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,59 +168,35 @@ sealed interface Model {
}
}

sealed interface Union : Model {
data class Union(
val schema: Schema,
val context: NamingContext,
val schemas: List<Entry>,
val inline: List<Model>,
val default: String?
) : Model {
data class Entry(val context: NamingContext, val model: Model)
}

sealed interface Enum : Model {
val schema: Schema
val context: NamingContext
val schemas: List<UnionEntry>
val inline: List<Model>

fun isOpenEnumeration(): Boolean =
this is AnyOf &&
schemas.size == 2 &&
schemas.count { it.model is Enum } == 1 &&
schemas.count { it.model is Primitive.String } == 1

data class UnionEntry(val context: NamingContext, val model: Model)
val values: List<String>
val default: String?

/** [OneOf] is an untagged union. This is in Kotlin represented by a `sealed interface`. */
data class OneOf(
override val schema: Schema,
override val context: NamingContext,
override val schemas: List<UnionEntry>,
override val inline: List<Model>,
val default: String?
) : Union

/**
* [AnyOf] is an untagged union, with overlapping schema.
*
* Typically:
* - open enumeration: anyOf a `string`, and [Enum] (also type: `string`).
* - [Object] with a [FreeFormJson], where [FreeFormJson] has overlapping schema with the
* [Object].
*/
data class AnyOf(
data class Closed(
override val schema: Schema,
override val context: NamingContext,
override val schemas: List<UnionEntry>,
override val inline: List<Model>,
val default: String?
) : Union
val inner: Model,
override val values: List<String>,
override val default: String?
) : Enum

/** [TypeArray] */
data class TypeArray(
data class Open(
override val schema: Schema,
override val context: NamingContext,
override val schemas: List<UnionEntry>,
override val inline: List<Model>
) : Union
override val values: List<String>,
override val default: String?
) : Enum
}

data class Enum(
val schema: Schema,
val context: NamingContext,
val inner: Model,
val values: List<String>,
val default: String?
) : Model
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ sealed interface NamingContext {

data class Named(override val name: String) : NamingContext

data class Nested(override val name: String, val outer: NamingContext) : NamingContext

data class RouteParam(override val name: String, val operationId: String?, val postfix: String) :
NamingContext

sealed interface Nested : NamingContext {
val outer: NamingContext
}

data class Inline(override val name: String, override val outer: NamingContext) : Nested

data class Ref(override val name: String, override val outer: NamingContext) : Nested
}
Loading

0 comments on commit feace36

Please sign in to comment.