Skip to content

Improve declaration of untagged unions to allow for generator optimizations #2548

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

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Improve declaration of untagged unions to allow for generator optimiz…
…ations
  • Loading branch information
flobernd committed Jun 28, 2024
commit 28e78916c04703e5ae073cef938438147bf56dca
13 changes: 10 additions & 3 deletions compiler/src/model/metamodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export abstract class BaseType {
specLocation: string
}

export type Variants = ExternalTag | InternalTag | Container
export type Variants = ExternalTag | InternalTag | Container | Untagged

export class VariantBase {
/**
Expand All @@ -207,6 +207,10 @@ export class Container extends VariantBase {
kind: 'container'
}

export class Untagged extends VariantBase {
kind: 'untagged'
}

/**
* Inherits clause (aka extends or implements) for an interface or request
*/
Expand Down Expand Up @@ -364,8 +368,11 @@ export class TypeAlias extends BaseType {
type: ValueOf
/** generic parameters: either concrete types or open parameters from the enclosing type */
generics?: TypeName[]
/** Only applicable to `union_of` aliases: identify typed_key unions (external) and variant inventories (internal) */
variants?: InternalTag | ExternalTag
/**
* Only applicable to `union_of` aliases: identify typed_key unions (external), variant inventories (internal)
* and untagged variants
*/
variants?: InternalTag | ExternalTag | Untagged
}

// ------------------------------------------------------------------------------------------------
Expand Down
11 changes: 9 additions & 2 deletions compiler/src/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ export function modelTypeAlias (declaration: TypeAliasDeclaration): model.TypeAl
if (variants != null) {
assert(
declaration.getJsDocs(),
variants.kind === 'internal_tag' || variants.kind === 'external_tag',
'Type Aliases can only have internal or external variants'
variants.kind === 'internal_tag' || variants.kind === 'external_tag' || variants.kind === 'untagged',
'Type Aliases can only have internal, external or untagged variants'
)
typeAlias.variants = variants
}
Expand Down Expand Up @@ -1110,6 +1110,13 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
}
}

if (type === 'untagged') {
return {
kind: 'untagged',
nonExhaustive: nonExhaustive
}
}

assert(jsDoc, type === 'internal', `Bad variant type: ${type}`)

const pairs = parseKeyValues(jsDoc, values, 'tag', 'default')
Expand Down
49 changes: 48 additions & 1 deletion compiler/src/steps/validate-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
}
}

function validateTaggedUnion (valueOf: model.UnionOf, variants: model.InternalTag | model.ExternalTag): void {
function validateTaggedUnion (valueOf: model.UnionOf, variants: model.InternalTag | model.ExternalTag | model.Untagged): void {
if (variants.kind === 'external_tag') {
// All items must have a 'variant' attribute
const items = flattenUnionMembers(valueOf)
Expand Down Expand Up @@ -610,6 +610,53 @@ export default async function validateModel (apiModel: model.Model, restSpec: Ma
}

validateValueOf(valueOf, new Set())
} else if (variants.kind === 'untagged') {
const items = flattenUnionMembers(valueOf)

const baseTypes = new Set<string>()

for (const item of items) {
if (item.kind !== 'instance_of') {
modelError('Items of type untagged unions must be type references')
} else {
validateTypeRef(item.type, undefined, new Set<string>())
const type = getTypeDef(item.type)
if (type == null) {
modelError(`Type ${fqn(item.type)} not found`)
} else {
if (type.kind !== 'interface') {
modelError(`Type ${fqn(item.type)} must be an interface to be used in an untagged union`)
continue
}

if (type.inherits == null) {
modelError(`Type ${fqn(item.type)} must derive from a base type to be used in an untagged union`)
continue
}

baseTypes.add(fqn(type.inherits.type))

const baseType = getTypeDef(type.inherits.type)
if (baseType == null) {
modelError(`Type ${fqn(type.inherits.type)} not found`)
continue
}

if (baseType.kind !== 'interface') {
modelError(`Type ${fqn(type.inherits.type)} must be an interface to be used as the base class of another interface`)
continue
}

if (baseType.generics == null || baseType.generics.length === 0) {
modelError('The common base type of an untagged union must accept at least one generic type argument')
}
}
}
}

if (baseTypes.size !== 1) {
modelError('All items of an untagged union must derive from the same common base type')
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion specification/_types/query_dsl/specialized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export class DateDistanceFeatureQuery extends DistanceFeatureQueryBase<
Duration
> {}

/** @codegen_names geo, date */
/**
* @codegen_names geo, date
* @variants untagged
*/
// Note: deserialization depends on value types
export type DistanceFeatureQuery =
| GeoDistanceFeatureQuery
Expand Down
68 changes: 16 additions & 52 deletions specification/_types/query_dsl/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,30 @@ export class PrefixQuery extends QueryBase {
case_insensitive?: boolean
}

export class RangeQueryBase extends QueryBase {
export class RangeQueryBase<T> extends QueryBase {
/**
* Indicates how the range query matches values for `range` fields.
* @server_default intersects
*/
relation?: RangeRelation
}

export class DateRangeQuery extends RangeQueryBase {
/**
* Greater than.
*/
gt?: DateMath
gt?: T
/**
* Greater than or equal to.
*/
gte?: DateMath
gte?: T
/**
* Less than.
*/
lt?: DateMath
lt?: T
/**
* Less than or equal to.
*/
lte?: DateMath
from?: DateMath | null
to?: DateMath | null
lte?: T
from?: T | null
to?: T | null
/**
* Date format used to convert `date` values in the query.
*/
Expand All @@ -142,51 +139,18 @@ export class DateRangeQuery extends RangeQueryBase {
time_zone?: TimeZone
}

export class NumberRangeQuery extends RangeQueryBase {
/**
* Greater than.
*/
gt?: double
/**
* Greater than or equal to.
*/
gte?: double
/**
* Less than.
*/
lt?: double
/**
* Less than or equal to.
*/
lte?: double
from?: double | null
to?: double | null
}
export class DateRangeQuery extends RangeQueryBase<DateMath> {}

export class TermsRangeQuery extends RangeQueryBase {
/**
* Greater than.
*/
gt?: string
/**
* Greater than or equal to.
*/
gte?: string
/**
* Less than.
*/
lt?: string
/**
* Less than or equal to.
*/
lte?: string
from?: string | null
to?: string | null
}
export class NumberRangeQuery extends RangeQueryBase<double> {}

/** @codegen_names date, number, terms */
export class TermRangeQuery extends RangeQueryBase<string> {}

/**
* @codegen_names date, number, term
* @variants untagged
*/
// Note: deserialization depends on value types
export type RangeQuery = DateRangeQuery | NumberRangeQuery | TermsRangeQuery
export type RangeQuery = DateRangeQuery | NumberRangeQuery | TermRangeQuery

export enum RangeRelation {
/**
Expand Down
13 changes: 10 additions & 3 deletions typescript-generator/src/metamodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export abstract class BaseType {
specLocation: string
}

export type Variants = ExternalTag | InternalTag | Container
export type Variants = ExternalTag | InternalTag | Container | Untagged

export class VariantBase {
/**
Expand All @@ -207,6 +207,10 @@ export class Container extends VariantBase {
kind: 'container'
}

export class Untagged extends VariantBase {
kind: 'untagged'
}

/**
* Inherits clause (aka extends or implements) for an interface or request
*/
Expand Down Expand Up @@ -364,8 +368,11 @@ export class TypeAlias extends BaseType {
type: ValueOf
/** generic parameters: either concrete types or open parameters from the enclosing type */
generics?: TypeName[]
/** Only applicable to `union_of` aliases: identify typed_key unions (external) and variant inventories (internal) */
variants?: InternalTag | ExternalTag
/**
* Only applicable to `union_of` aliases: identify typed_key unions (external), variant inventories (internal)
* and untagged variants
*/
variants?: InternalTag | ExternalTag | Untagged
}

// ------------------------------------------------------------------------------------------------
Expand Down