Skip to content

Commit

Permalink
[FER-2474] Test rendering literals as single enum values (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Jul 20, 2024
1 parent 9781242 commit a2c9ca8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ export const InternalTypeDefinition = memo<InternalTypeDefinition.Props>(functio
elementNameSingular: "enum value",
elementNamePlural: "enum values",
}),
literal: (literal) => ({
elements: [
visitDiscriminatedUnion(literal.value, "type")._visit({
stringLiteral: (value) => <Chip key={value.value} name={value.value} />,
booleanLiteral: (value) => (
<Chip key={value.value.toString()} name={value.value.toString()} />
),
_other: () => <span>{"<unknown>"}</span>,
}),
],
elementNameSingular: "literal",
elementNamePlural: "literals",
}),
alias: () => undefined,
unknown: () => undefined,
_other: () => undefined,
Expand Down Expand Up @@ -151,6 +164,17 @@ export const InternalTypeDefinition = memo<InternalTypeDefinition.Props>(functio
}

if (!isCollapsible) {
// TODO: (rohin) Refactor this
if (collapsableContent.elementNameSingular === "literal") {
return (
<div className="t-muted flex items-baseline gap-2">
<span className="shrink-0 text-sm"> Allowed value: </span>
<FernTooltipProvider>
<span className="inline-flex flex-wrap gap-2">{collapsableContent.elements}</span>
</FernTooltipProvider>
</div>
);
}
return (
<FernErrorBoundary component="InternalTypeDefinition">
<FernTooltipProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ export const InternalTypeDefinitionError: React.FC<InternalTypeDefinitionError.P
elementNameSingular: "enum value",
elementNamePlural: "enum values",
}),
literal: (literal) => ({
elements: [
<EnumValue
key={literal.value.value.toString()}
enumValue={{
value: visitDiscriminatedUnion(literal.value, "type")._visit({
stringLiteral: (value) => `${value.value}`,
booleanLiteral: (value) => `${value.value ? "true" : "false"}`,
_other: () => "<unknown>",
}),
description: undefined,
availability: undefined,
}}
/>,
],
elementNameSingular: "literal",
elementNamePlural: "literals",
}),
_other: () => undefined,
alias: () => undefined,
unknown: () => undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function hasInternalTypeReference(
optional: (optional) => hasInternalTypeReference(optional.shape, types),
map: (map) => hasInternalTypeReference(map.keyShape, types) || hasInternalTypeReference(map.valueShape, types),
primitive: () => false,
literal: () => false,
literal: () => true,
unknown: () => false,
_other: () => false,
reference: (reference) =>
Expand Down Expand Up @@ -168,7 +168,16 @@ export const InternalTypeReferenceDefinitions: React.FC<InternalTypeReferenceDef
</MapTypeContextProvider>
),
primitive: () => null,
literal: () => null,
literal: (literal) => (
<InternalShapeRenderer
typeShape={literal}
isCollapsible={false}
anchorIdParts={anchorIdParts}
route={route}
defaultExpandAll={defaultExpandAll}
types={types}
/>
),
unknown: () => null,
_other: () => null,
alias: (alias) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ export function renderTypeShorthand(
)} to ${renderTypeShorthand(map.valueShape, { plural: true }, types)}`,

// literals
literal: (literal) =>
visitDiscriminatedUnion(literal.value, "type")._visit({
stringLiteral: (value) => `"${value.value}"`,
booleanLiteral: (value) => `${value.value}`,
literal: (_literal) =>
visitDiscriminatedUnion(_literal.value, "type")._visit({
stringLiteral: () => "string literal",
booleanLiteral: () => "boolean literal",
_other: () => "<unknown>",
}),

// other
unknown: () => "any",
_other: () => "<unknown>",
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/app/src/resolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ export type ResolvedTypeDefinition =
| ResolvedDiscriminatedUnionShape
| ResolvedEnumShape
| ResolvedAliasShape
| (APIV1Read.TypeReference.Unknown & WithMetadata);
| (APIV1Read.TypeReference.Unknown & WithMetadata)
| (APIV1Read.TypeReference.Literal & WithMetadata);

interface ResolvedEnumShape extends WithMetadata {
name: string | undefined;
Expand Down

0 comments on commit a2c9ca8

Please sign in to comment.