Skip to content

Commit

Permalink
Fix resolved type generation (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachkirsch authored Sep 16, 2022
1 parent b5387aa commit b6c380a
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 14 deletions.
2 changes: 0 additions & 2 deletions fern/ir-types/definition/types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ types:
properties:
aliasOf: TypeReference
resolvedType: ResolvedTypeReference

ResolvedTypeReference:
union:
container: ContainerType
named: ResolvedNamedType
primitive: PrimitiveType
unknown: {}
void: {}

ResolvedNamedType:
properties:
name: DeclaredTypeName
Expand Down
178 changes: 178 additions & 0 deletions packages/cli/ete-tests/src/tests/ir/__snapshots__/ir.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@ exports[`ir simple 1`] = `
"schemes": []
},
"types": [
{
"name": {
"fernFilepath": [
{
"originalValue": "director",
"camelCase": "director",
"snakeCase": "director",
"pascalCase": "Director",
"screamingSnakeCase": "DIRECTOR"
}
],
"name": "Director"
},
"shape": {
"extends": [],
"properties": [
{
"name": {
"originalValue": "name",
"camelCase": "name",
"snakeCase": "name",
"pascalCase": "Name",
"screamingSnakeCase": "NAME",
"wireValue": "name"
},
"valueType": {
"primitive": "STRING",
"_type": "primitive"
}
}
],
"_type": "object"
}
},
{
"name": {
"fernFilepath": [
Expand All @@ -33,6 +67,31 @@ exports[`ir simple 1`] = `
"_type": "alias"
}
},
{
"name": {
"fernFilepath": [
{
"originalValue": "imdb",
"camelCase": "imdb",
"snakeCase": "imdb",
"pascalCase": "Imdb",
"screamingSnakeCase": "IMDB"
}
],
"name": "ActorId"
},
"shape": {
"aliasOf": {
"primitive": "STRING",
"_type": "primitive"
},
"resolvedType": {
"primitive": "STRING",
"_type": "primitive"
},
"_type": "alias"
}
},
{
"name": {
"fernFilepath": [
Expand Down Expand Up @@ -151,6 +210,125 @@ exports[`ir simple 1`] = `
],
"_type": "object"
}
},
{
"name": {
"fernFilepath": [
{
"originalValue": "imdb",
"camelCase": "imdb",
"snakeCase": "imdb",
"pascalCase": "Imdb",
"screamingSnakeCase": "IMDB"
}
],
"name": "Person"
},
"shape": {
"discriminant": "type",
"types": [
{
"discriminantValue": {
"originalValue": "actor",
"camelCase": "actor",
"snakeCase": "actor",
"pascalCase": "Actor",
"screamingSnakeCase": "ACTOR",
"wireValue": "actor"
},
"valueType": {
"name": "ActorId",
"fernFilepath": [
{
"originalValue": "imdb",
"camelCase": "imdb",
"snakeCase": "imdb",
"pascalCase": "Imdb",
"screamingSnakeCase": "IMDB"
}
],
"_type": "named"
},
"shape": {
"name": {
"originalValue": "value",
"camelCase": "value",
"snakeCase": "value",
"pascalCase": "Value",
"screamingSnakeCase": "VALUE",
"wireValue": "value"
},
"type": {
"name": "ActorId",
"fernFilepath": [
{
"originalValue": "imdb",
"camelCase": "imdb",
"snakeCase": "imdb",
"pascalCase": "Imdb",
"screamingSnakeCase": "IMDB"
}
],
"_type": "named"
},
"_type": "singleProperty"
}
},
{
"discriminantValue": {
"originalValue": "director",
"camelCase": "director",
"snakeCase": "director",
"pascalCase": "Director",
"screamingSnakeCase": "DIRECTOR",
"wireValue": "director"
},
"valueType": {
"name": "Director",
"fernFilepath": [
{
"originalValue": "director",
"camelCase": "director",
"snakeCase": "director",
"pascalCase": "Director",
"screamingSnakeCase": "DIRECTOR"
}
],
"_type": "named"
},
"shape": {
"name": "Director",
"fernFilepath": [
{
"originalValue": "director",
"camelCase": "director",
"snakeCase": "director",
"pascalCase": "Director",
"screamingSnakeCase": "DIRECTOR"
}
],
"_type": "samePropertiesAsObject"
}
},
{
"discriminantValue": {
"originalValue": "producer",
"camelCase": "producer",
"snakeCase": "producer",
"pascalCase": "Producer",
"screamingSnakeCase": "PRODUCER",
"wireValue": "producer"
},
"valueType": {
"_type": "void"
},
"shape": {
"_type": "noProperties"
}
}
],
"_type": "union"
}
}
],
"errors": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
types:
Director:
properties:
name: string
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
imports:
director: director.yml
ids:
- MovieId
- ActorId
types:
Movie:
properties:
Expand All @@ -10,6 +13,11 @@ types:
properties:
title: string
rating: double
Person:
union:
actor: ActorId
director: director.Director
producer: {}
errors:
NotFoundError:
http:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { RelativeFilePath } from "@fern-api/core-utils";
import { Workspace } from "@fern-api/workspace-loader";
import { visitRawTypeDeclaration } from "@fern-api/yaml-schema";
import { RawSchemas, RAW_DEFAULT_ID_TYPE, ServiceFileSchema, visitRawTypeDeclaration } from "@fern-api/yaml-schema";
import { ResolvedTypeReference, ShapeType, TypeReference } from "@fern-fern/ir-model/types";
import path from "path";
import { constructFernFileContext, FernFileContext } from "../FernFileContext";
import { parseInlineType } from "../utils/parseInlineType";
import { parseReferenceToTypeName } from "../utils/parseReferenceToTypeName";
Expand All @@ -23,23 +21,19 @@ export class TypeResolverImpl implements TypeResolver {
void: ResolvedTypeReference.void,
named: (typeName) => {
const reference = parseReferenceToTypeName({
reference: typeName.name,
referencedIn: RelativeFilePath.of(
typeName.fernFilepath.map((part) => part.originalValue).join(path.sep)
),
reference: type,
referencedIn: file.relativeFilepath,
imports: file.imports,
});
if (reference == null) {
throw new Error("Cannot find type: " + typeName.name);
throw new Error("Cannot find type: " + type);
}
const serviceFile = this.workspace.serviceFiles[reference.relativeFilePath];
if (serviceFile == null) {
throw new Error("Cannot find file: " + reference.relativeFilePath);
}
const declaration = serviceFile.types?.[reference.typeName];
if (declaration == null) {
throw new Error("Cannot find type declaration: " + reference.typeName);
}

const declaration = getDeclaration(serviceFile, reference.typeName);
return visitRawTypeDeclaration(declaration, {
alias: (aliasDeclaration) => {
return this.resolveType({
Expand Down Expand Up @@ -73,3 +67,22 @@ export class TypeResolverImpl implements TypeResolver {
});
}
}

function getDeclaration(serviceFile: ServiceFileSchema, typeName: string): RawSchemas.TypeDeclarationSchema {
const declaration = serviceFile.types?.[typeName];
if (declaration != null) {
return declaration;
}

const idDeclaration = serviceFile.ids?.find((id) =>
typeof id === "string" ? id === typeName : id.name === typeName
);
if (idDeclaration != null) {
if (typeof idDeclaration !== "string" && idDeclaration.type != null) {
return idDeclaration.type;
}
return RAW_DEFAULT_ID_TYPE;
}

throw new Error("Cannot find type declaration: " + typeName);
}

0 comments on commit b6c380a

Please sign in to comment.