-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into amckinney/go/unknown
- Loading branch information
Showing
821 changed files
with
38,784 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6,185 changes: 6,185 additions & 0 deletions
6,185
packages/cli/generation/ir-generator/src/__test__/test-definitions/mixed-file-directory.json
Large diffs are not rendered by default.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
packages/cli/openapi-ir-to-fern/src/FernDefinitionDirectory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { RawSchemas } from "@fern-api/fern-definition-schema"; | ||
import path from "path"; | ||
|
||
export class FernDefinitionDirectory { | ||
private files: Record<RelativeFilePath, RawSchemas.DefinitionFileSchema> = {}; | ||
private directories: Record<string, FernDefinitionDirectory> = {}; | ||
|
||
public getAllFiles(): Record<RelativeFilePath, RawSchemas.DefinitionFileSchema> { | ||
const files: Record<RelativeFilePath, RawSchemas.DefinitionFileSchema> = {}; | ||
|
||
const walk = (root: FernDefinitionDirectory, currentPath?: string) => { | ||
for (const [relativeFilePath, definition] of Object.entries(root.files)) { | ||
const fullRelativeFilePath = | ||
currentPath != null | ||
? RelativeFilePath.of(`${currentPath}${path.sep}${relativeFilePath}`) | ||
: RelativeFilePath.of(relativeFilePath); | ||
files[fullRelativeFilePath] = definition; | ||
} | ||
const sortedDirectories = Object.keys(root.directories).sort(); | ||
for (const directory of sortedDirectories) { | ||
const nextPath = currentPath != null ? `${currentPath}${path.sep}${directory}` : directory; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const nextDirectory = root.directories[directory]!; | ||
walk(nextDirectory, nextPath); | ||
} | ||
}; | ||
|
||
walk(this); | ||
|
||
return files; | ||
} | ||
|
||
public getOrCreateFile(relativeFilePath: RelativeFilePath): RawSchemas.DefinitionFileSchema { | ||
return this.getOrCreateFileRecursive(relativeFilePath.split(path.sep)); | ||
} | ||
|
||
private getOrCreateFileRecursive(pathParts: string[]): RawSchemas.DefinitionFileSchema { | ||
if (pathParts.length === 1) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return (this.files[RelativeFilePath.of(pathParts[0]!)] ??= {}); | ||
} | ||
const [directory, ...remainingPath] = pathParts; | ||
if (directory == null) { | ||
throw new Error(`Internal error; cannot add file with path: ${pathParts}`); | ||
} | ||
if (!this.directories[directory]) { | ||
this.directories[directory] = new FernDefinitionDirectory(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.directories[directory]!.getOrCreateFileRecursive(remainingPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/cli/openapi-ir-to-fern/src/__test__/FernDefinitionDirectory.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { RawSchemas } from "@fern-api/fern-definition-schema"; | ||
import { RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { FernDefinitionDirectory } from "../FernDefinitionDirectory"; | ||
|
||
interface TestCase { | ||
description: string; | ||
giveFilepaths: string[]; | ||
wantFiles: Record<string, RawSchemas.DefinitionFileSchema>; | ||
} | ||
|
||
describe("FernDefinitionDirectory", () => { | ||
const testCases: TestCase[] = [ | ||
{ | ||
description: "empty", | ||
giveFilepaths: [], | ||
wantFiles: {} | ||
}, | ||
{ | ||
description: "single file", | ||
giveFilepaths: ["example.yml"], | ||
wantFiles: { | ||
"example.yml": {} | ||
} | ||
}, | ||
{ | ||
description: "single directory", | ||
giveFilepaths: ["one/a.yml"], | ||
wantFiles: { | ||
"one/a.yml": {} | ||
} | ||
}, | ||
{ | ||
description: "single directory, multiple files", | ||
giveFilepaths: ["one/b.yml", "one/a.yml"], | ||
wantFiles: { | ||
"one/a.yml": {}, | ||
"one/b.yml": {} | ||
} | ||
}, | ||
{ | ||
description: "multiple directory, multiple files", | ||
giveFilepaths: ["one/b.yml", "two/foo/d.yml", "two/foo/c.yml", "one/a.yml"], | ||
wantFiles: { | ||
"one/a.yml": {}, | ||
"one/b.yml": {}, | ||
"two/foo/c.yml": {}, | ||
"two/foo/d.yml": {} | ||
} | ||
}, | ||
{ | ||
description: "file/directory match", | ||
giveFilepaths: ["user/events/metadata.yml", "user/events.yml", "user.yml", "events.yml"], | ||
wantFiles: { | ||
"events.yml": {}, | ||
"user.yml": {}, | ||
"user/events.yml": {}, | ||
"user/events/metadata.yml": {} | ||
} | ||
} | ||
]; | ||
|
||
testCases.forEach((testCase) => { | ||
it(`"${testCase.description}"`, async () => { | ||
const root = new FernDefinitionDirectory(); | ||
for (const filepath of testCase.giveFilepaths) { | ||
root.getOrCreateFile(RelativeFilePath.of(filepath)); | ||
} | ||
expect(root.getAllFiles()).toEqual(testCase.wantFiles); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.