Skip to content
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

Undiscriminated Unions #4186

Closed
wants to merge 77 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
ebf3a54
Simple Unit Test Generator
mikemilla Jul 8, 2024
328fc3d
Generator Code
mikemilla Jul 8, 2024
9594155
Generator LANGUAGE_NAME
mikemilla Jul 8, 2024
2e5b737
Swift Init
mikemilla Jul 8, 2024
25513e7
Writer and Astnode as commons
mikemilla Jul 8, 2024
01f00dd
More function functionality
mikemilla Jul 8, 2024
a2b76f3
Lang Gen v2
mikemilla Jul 8, 2024
49fda54
Lang Gen v3
mikemilla Jul 8, 2024
3b40044
Oops
mikemilla Jul 8, 2024
409eff7
Polish
mikemilla Jul 8, 2024
50b4a70
Writer indentation
mikemilla Jul 8, 2024
f775291
Polished imports
mikemilla Jul 8, 2024
5fc82d0
Refined imports
mikemilla Jul 8, 2024
a7ccd13
Template generator notes
mikemilla Jul 8, 2024
b8049da
Writer Polish
mikemilla Jul 9, 2024
11d5e41
File gen and merge
mikemilla Jul 9, 2024
4a74c93
Generator Generator Updates
mikemilla Jul 9, 2024
06f3d0d
MERGE
mikemilla Jul 9, 2024
e8a0c21
Generator Polish
mikemilla Jul 9, 2024
2c6c685
Yarn lock fix
mikemilla Jul 9, 2024
3f53b5b
Merge branch 'main' into feature/swift
mikemilla Jul 9, 2024
dabb4d3
Lodash dep removed
mikemilla Jul 9, 2024
05de907
Struct and import changes
mikemilla Jul 9, 2024
e436edc
Template rename
mikemilla Jul 9, 2024
34943ac
MERGE
mikemilla Jul 9, 2024
8495e09
Rust test removed
mikemilla Jul 9, 2024
57a7411
Swift IR dep update
mikemilla Jul 9, 2024
4e53ffc
wip
mikemilla Jul 9, 2024
167dfe3
Types
mikemilla Jul 9, 2024
7f09715
Types
mikemilla Jul 9, 2024
1fb8e42
Types
mikemilla Jul 10, 2024
836865d
Func polish
mikemilla Jul 10, 2024
df34c63
Comments
mikemilla Jul 10, 2024
e315fe7
MERGE
mikemilla Jul 10, 2024
00af631
PNP MERGE
mikemilla Jul 10, 2024
8e49cb7
Model init
mikemilla Jul 10, 2024
8097e7d
TODO comment in script
mikemilla Jul 10, 2024
6daaba3
Merge branch 'main' into feature/swift-model
mikemilla Jul 10, 2024
e8e682d
Seed test issues
mikemilla Jul 10, 2024
f20840b
Clean
mikemilla Jul 10, 2024
70ad3f9
Yarn fix
mikemilla Jul 10, 2024
5df4b2f
Docker Image working
mikemilla Jul 10, 2024
96a7965
Files are generating
mikemilla Jul 10, 2024
b9c54e8
Rough Swift Model Generator
mikemilla Jul 11, 2024
21f1552
Merge branch 'main' into feature/swift-model
mikemilla Jul 11, 2024
94d816f
Structs
mikemilla Jul 11, 2024
d36a75e
Primatives
mikemilla Jul 11, 2024
22ea910
Swift Seed Init
mikemilla Jul 11, 2024
8bdafc5
Factories
mikemilla Jul 12, 2024
6dac791
Backup
mikemilla Jul 12, 2024
e71dda7
Backup
mikemilla Jul 12, 2024
8b815e7
MERGE complete
mikemilla Jul 12, 2024
574065f
Fix
mikemilla Jul 12, 2024
d9b78d5
MERGE
mikemilla Jul 12, 2024
b4f2886
Optionals and polish
mikemilla Jul 12, 2024
afbd772
Swift Generation Backup
mikemilla Jul 19, 2024
fb0fefc
MERGE
mikemilla Jul 23, 2024
06675d6
Arrays and optionals
mikemilla Jul 24, 2024
e338279
Renamed classes
mikemilla Jul 24, 2024
968652b
Merge remote-tracking branch 'origin/main' into feature/swift-generat…
mikemilla Jul 24, 2024
3fe8512
MERGE
mikemilla Jul 24, 2024
b11fa76
Import fix
mikemilla Jul 24, 2024
3938d12
Enums
mikemilla Jul 24, 2024
3add53d
Merge branch 'main' into feature/swift-generator-seed
mikemilla Jul 24, 2024
7ae88c4
Merge branch 'feature/swift-generator-seed' into swift/enums
mikemilla Jul 25, 2024
68be8a1
Wire values
mikemilla Jul 25, 2024
fb8efc8
Merge remote-tracking branch 'origin/main' into swift/enums
mikemilla Jul 26, 2024
295ccd9
MERGE
mikemilla Jul 26, 2024
423c728
Comment updates
mikemilla Jul 26, 2024
8b191eb
Reverted factories made code gen closer to csharp
mikemilla Aug 1, 2024
8762eb7
Generation
mikemilla Aug 1, 2024
8f52841
Access Level
mikemilla Aug 1, 2024
b3c0802
MERGE
mikemilla Aug 1, 2024
2df6aa3
Updated IR
mikemilla Aug 2, 2024
8020813
v53 support
mikemilla Aug 6, 2024
a949f4f
Todo polish
mikemilla Aug 6, 2024
8bad1be
Builder renaming
mikemilla Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 30 additions & 1 deletion .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generators/commons/src/AstNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class AstNode {
/**
* Writes the node to a string.
*/
public toString(): string {
public render(): string {
const writer = new Writer(this.indentSize);
this.write(writer);
return writer.toString();
Expand Down
8 changes: 5 additions & 3 deletions generators/commons/src/GeneratedFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { join } from "path";
import { AstNode } from "./AstNode";

export class GeneratedFile {

public name: string;
public extension: string;
public outputDirectory: string;
public contents: AstNode;

get filename(): string {
return `${this.name}.${this.extension}`;
get filename(): string {
return `${this.name}.${this.extension}`;
}

constructor(fileName: string, fileExtension: string, outputDirectory: string, contents: AstNode) {
Expand All @@ -23,7 +24,7 @@ export class GeneratedFile {
public async generate(): Promise<string> {
await mkdir(this.outputDirectory, { recursive: true });
const outputFile = `${this.outputDirectory}/${this.filename}`;
await writeFile(outputFile, this.contents.toString());
await writeFile(outputFile, this.contents.render());
return outputFile;
}

Expand All @@ -38,4 +39,5 @@ export class GeneratedFile {
console.error(`Error reading file: ${error}`);
}
}

}
17 changes: 4 additions & 13 deletions generators/commons/src/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export class Writer {
callback: () => void,
closingCharacter: string | undefined = "}"
): void {
const filteredTitles = titles.filter((title) => title !== undefined).join(" ");
const filteredTitles = titles.filter(title => title !== undefined).join(" ");
if (filteredTitles) {
this.write(`${filteredTitles} ${openingCharacter ?? ""}`);
} else {
this.write(openingCharacter ?? "");
}

try {
this.indent(callback);
} finally {
Expand All @@ -72,17 +72,8 @@ export class Writer {
return this.buffer;
}

private calculateIndentationDistance(example: string): number {
const trimmedExample = example.trim();
if (trimmedExample.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return example.indexOf(trimmedExample[0]!);
}

return 0;
}

private getIndentString(tabSize: number): string {
return " ".repeat(this.indentLevel * tabSize);
}
}

}
1 change: 1 addition & 0 deletions generators/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./GeneratorNotificationService";
export * from "./parseGeneratorConfig";
export * from "./PersistedProject";
export * from "./Writer";

1 change: 1 addition & 0 deletions generators/csharp/codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * as csharp from "./csharp";
export { FileGenerator } from "./FileGenerator";
export * from "./project";
export { TestFileGenerator } from "./TestFileGenerator";

52 changes: 26 additions & 26 deletions generators/swift/codegen/src/ast/AccessLevel.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
/**
/**
* An enum representing different access levels, similar to Swift's access control levels.
* This enum can be used to define and check access permissions for various parts of a TypeScript application.
*/
export enum AccessLevel {
/**
* Open: Accessible and subclassable/overridable outside the module.
* This is the highest (most permissive) access level.
*/
Open = "open",
/**
* Open: Accessible and subclassable/overridable outside the module.
* This is the highest (most permissive) access level.
*/
Open = "open",

/**
* Public: Accessible outside the module but not subclassable/overridable.
* Suitable for exposing an API to be used by other modules.
*/
Public = "public",
/**
* Public: Accessible outside the module but not subclassable/overridable.
* Suitable for exposing an API to be used by other modules.
*/
Public = "public",

/**
* Internal: Accessible within the same module.
* The default access level for most parts of a module’s internal implementation.
*/
Internal = "internal",
/**
* Internal: Accessible within the same module.
* The default access level for most parts of a module’s internal implementation.
*/
Internal = "internal",

/**
* Fileprivate: Accessible within the same file.
* Useful for encapsulating implementation details that are shared across multiple types within a file but should not be accessible from other files.
*/
Fileprivate = "fileprivate",
/**
* Fileprivate: Accessible within the same file.
* Useful for encapsulating implementation details that are shared across multiple types within a file but should not be accessible from other files.
*/
Fileprivate = "fileprivate",

/**
* Private: Accessible within the same enclosing declaration (class, struct, or extension) and their extensions within the same file.
* Used for tightly encapsulating implementation details.
*/
Private = "private"
/**
* Private: Accessible within the same enclosing declaration (class, struct, or extension) and their extensions within the same file.
* Used for tightly encapsulating implementation details.
*/
Private = "private"
}
4 changes: 0 additions & 4 deletions generators/swift/codegen/src/ast/ClassLevel.ts

This file was deleted.

29 changes: 29 additions & 0 deletions generators/swift/codegen/src/ast/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AstNode, Writer } from "@fern-api/generator-commons";
import Swift from "..";

export declare namespace Comment {
interface Args {
comment: string;
}
}

export class Comment extends AstNode {

public readonly comment: string;

constructor(args: Comment.Args) {
super(Swift.indentSize);
this.comment = args.comment;
}

private buildComment(comment: string): string {
const lines = comment.split("\n");
const formattedLines = lines.map(line => `/// ${line}`).join("\n");
return formattedLines;
}

public write(writer: Writer): void {
writer.write(this.buildComment(this.comment));
}

}
60 changes: 36 additions & 24 deletions generators/swift/codegen/src/ast/Enum.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,63 @@
import { AstNode, Writer } from "@fern-api/generator-commons";
import Swift, { AccessLevel, EnumCase, Type } from "..";
import Swift, { AccessLevel, EnumCase, SwiftClass } from "..";
import { EnumCaseAssociatedValue } from "./EnumCaseAssociatedValue";

export declare namespace Enum {
interface Args {
accessLevel?: AccessLevel;
name: string;
inheritance?: Type[];
enumCases: EnumCase[];
inheritance?: SwiftClass[],
enumCases: EnumCase[] | EnumCaseAssociatedValue[],
comment?: string,
}
}

export class Enum extends AstNode {

public readonly accessLevel?: AccessLevel;
public readonly name: string;
public readonly inheritance?: Type[];
public readonly enumCases: EnumCase[];
public readonly inheritance?: SwiftClass[];
public readonly enumCases: EnumCase[] | EnumCaseAssociatedValue[];
public readonly comment?: string;

constructor({ accessLevel, name, inheritance, enumCases }: Enum.Args) {
constructor(args: Enum.Args) {
super(Swift.indentSize);
this.accessLevel = accessLevel;
this.name = name;
this.inheritance = inheritance;
this.enumCases = enumCases;
this.accessLevel = args.accessLevel;
this.name = args.name;
this.inheritance = args.inheritance;
this.enumCases = args.enumCases;
this.comment = args.comment;
}

private buildTitle(): string | undefined {

if (!this.inheritance) {
return this.name;
}

const names = this.inheritance.map((obj) => obj.name).join(", ");
const names = this.inheritance.map(obj => obj.name).join(", ");
return `${this.name}: ${names}`;

}

public write(writer: Writer): void {
// example: enum CodingKeys: String, CodingKey {
writer.openBlock(
["enum", this.buildTitle()],
"{",
() => {
if (this.enumCases) {
this.enumCases.forEach((value) => {
writer.writeNode(value);
});
}
},
"}"
);

if (this.comment) {
writer.newLine();
writer.writeNode(Swift.makeComment({ comment: this.comment }));
}

// example: public enum CodingKeys: String, CodingKey {
writer.openBlock([this.accessLevel, "enum", this.buildTitle()], "{", () => {

if (this.enumCases) {
this.enumCases.forEach(value => {
writer.writeNode(value);
});
}

}, "}");

}

}
24 changes: 18 additions & 6 deletions generators/swift/codegen/src/ast/EnumCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ import Swift from "..";
export declare namespace EnumCase {
interface Args {
name: string;
key?: string;
value?: string;
comment?: string;
}
}

export class EnumCase extends AstNode {

public readonly name: string;
public readonly key?: string;
public readonly value?: string;
public readonly comment?: string;

constructor({ name, key }: EnumCase.Args) {
constructor(args: EnumCase.Args) {
super(Swift.indentSize);
this.name = name;
this.key = key;
this.name = args.name;
this.value = args.value;
this.comment = args.comment;
}

public write(writer: Writer): void {
const statement = this.key ? `case ${this.name} = ${this.key}` : `case ${this.name}`;

if (this.comment) {
writer.newLine();
writer.writeNode(Swift.makeComment({ comment: this.comment }));
}

const statement = this.value ? `case ${this.name} = ${this.value}` : `case ${this.name}`;
writer.write(statement);

}

}
Loading
Loading