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
Changes from 1 commit
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Comment updates
mikemilla committed Jul 26, 2024
commit 423c72835311310f2a616f25c4c37594e0686e5b
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));
}

}
3 changes: 2 additions & 1 deletion generators/swift/codegen/src/ast/EnumCase.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ export class EnumCase extends AstNode {
public write(writer: Writer): void {

if (this.comment) {
writer.write(`/// ${this.comment}`);
writer.newLine();
writer.writeNode(Swift.makeComment({ comment: this.comment }));
}

const statement = this.value ? `case ${this.name} = ${this.value}` : `case ${this.name}`;
33 changes: 16 additions & 17 deletions generators/swift/codegen/src/ast/Field.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import { AstNode, Writer } from "@fern-api/generator-commons";
import Swift, { AccessLevel, SwiftClass } from "..";
import { Optional } from "./Optional";
import { VariableType } from "./VariableType";

export declare namespace Field {
interface Args {
comment?: string;
accessLevel?: AccessLevel;
variableType?: VariableType;
name: string;
class: SwiftClass,
defaultValue?: string,
class: SwiftClass | Optional,
}
}

export class Field extends AstNode {
public readonly comment?: string;
public readonly accessLevel?: AccessLevel;
public readonly variableType?: VariableType;
public readonly name: string;
public readonly class: SwiftClass;
public readonly defaultValue?: string; // TODO

constructor({
accessLevel,
variableType,
name,
class: valueType,
defaultValue,
}: Field.Args) {
public readonly class: SwiftClass | Optional;

constructor(args: Field.Args) {
super(Swift.indentSize);
this.accessLevel = accessLevel;
this.variableType = variableType;
this.name = name;
this.class = valueType;
this.defaultValue = defaultValue;
this.comment = args.comment;
this.accessLevel = args.accessLevel;
this.variableType = args.variableType;
this.name = args.name;
this.class = args.class;
}

public write(writer: Writer): void {

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

// e.g. public static let name: String
const title = [this.accessLevel, this.variableType ?? VariableType.Let, `${this.name}:`, this.class.name].join(" ");
writer.write(title);
4 changes: 1 addition & 3 deletions generators/swift/codegen/src/ast/Primative.ts
Original file line number Diff line number Diff line change
@@ -34,9 +34,7 @@ export class Primative extends SwiftClass {
public readonly key?: PrimativeKey;

constructor(args: Primative.Args) {
super({
name: Primative.getNameForKey(args.key)
});
super({ name: Primative.getNameForKey(args.key) });
this.key = args.key;
}

41 changes: 39 additions & 2 deletions generators/swift/codegen/src/ast/Struct.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AstNode, Writer } from "@fern-api/generator-commons";
import Swift, { AccessLevel, Enum, Field, SwiftClass } from "..";
import { Optional } from "./Optional";

/*

@@ -22,6 +23,8 @@ Breakdown:

export declare namespace Struct {
interface Args {
/* Comment that appear above the class */
comment?: string;
/* The access level of the type */
accessLevel?: AccessLevel;
/* The name of the struct */
@@ -32,26 +35,32 @@ export declare namespace Struct {
subclasses?: (Enum | SwiftClass)[];
/* The field variables in the class */
fields?: Field[];
/* Build initializer */
addInitializer?: boolean;
}
}

export class Struct extends AstNode {
public readonly comment?: string;
public readonly accessLevel?: AccessLevel;
public readonly name: string;
public readonly fields?: Field[];
public readonly inheritance?: SwiftClass[];
public readonly subclasses?: (Enum | SwiftClass)[];
public readonly addInitializer: boolean;

constructor(args: Struct.Args) {
super(Swift.indentSize);
this.comment = args.comment;
this.accessLevel = args.accessLevel;
this.name = args.name;
this.inheritance = args.inheritance;
this.subclasses = args.subclasses;
this.fields = args.fields;
this.addInitializer = args.addInitializer ?? false;
}

private buildTitle(): string | undefined {
private buildStructTitle(): string | undefined {

if (!this.inheritance) {
return this.name;
@@ -62,10 +71,21 @@ export class Struct extends AstNode {

}

private buildInitalizerTitle(): string | undefined {

const args = this.fields?.map(obj => {
const arg = `${obj.name}: ${obj.class.name}`;
return obj.class instanceof Optional ? `${arg} = nil` : arg;
}).join(", ");

return `init(${args ?? ""})`;

}

public write(writer: Writer): void {

// example: public struct Name {
writer.openBlock([this.accessLevel, "struct", this.buildTitle()], "{", () => {
writer.openBlock([this.accessLevel, "struct", this.buildStructTitle()], "{", () => {

if (this.subclasses) {
writer.newLine();
@@ -77,6 +97,7 @@ export class Struct extends AstNode {

if (this.fields) {
this.fields.forEach(field => {
if (field.comment) { writer.newLine(); }
writer.writeNode(field);
});
}
@@ -85,6 +106,22 @@ export class Struct extends AstNode {
writer.newLine();
}

if (this.addInitializer) {

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

writer.openBlock([this.accessLevel, this.buildInitalizerTitle()], "{", () => {
this.fields?.forEach(field => {
writer.write(`self.${field.name} = ${field.name}`);
});
}, "}");

writer.newLine();

}

}, "}");

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

/*

Builds Swift Types (Classes, Structs, etc)
==========================================

Example:

private class Mike: Person {

class Snowboard {
...
}

func wave() {
print("👋")
}

}

Breakdown:

{accessLevel} {classLevel} {name}: {inheritance} {

{subclasses}

{functions}

}

*/

export declare namespace SwiftDictionary {
interface Args {
class: SwiftClass;
/* Key Type */
keyClass: SwiftClass | Enum;
/* Value Type */
valueClass: SwiftClass | Enum;
// TODO: Handle default value
}
}

export class SwiftDictionary extends SwiftClass {

constructor(args: SwiftDictionary.Args) {
super({
accessLevel: args.class.accessLevel,
name: `[${args.keyClass.name}: ${args.valueClass.name}]`,
subclasses: args.class.subclasses,
fields: args.class.fields,
functions: args.class.functions,
inheritance: args.class.inheritance,
});
}

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

}
16 changes: 15 additions & 1 deletion generators/swift/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */
// Swift Imports
import { AccessLevel } from "./ast/AccessLevel";
import { Comment } from "./ast/Comment";
import { Enum } from "./ast/Enum";
import { EnumCase } from "./ast/EnumCase";
import { Field } from "./ast/Field";
@@ -14,10 +15,12 @@ import { Primative } from "./ast/Primative";
import { Struct } from "./ast/Struct";
import { SwiftArray } from "./ast/SwiftArray";
import { SwiftClass } from "./ast/SwiftClass";
import { SwiftDictionary } from "./ast/SwiftDictionary";
import { TypeAlias } from "./ast/TypeAlias";

// Swift Exports
export { AccessLevel } from "./ast/AccessLevel";
export { Comment } from "./ast/Comment";
export { Enum } from "./ast/Enum";
export { EnumCase } from "./ast/EnumCase";
export { Field } from "./ast/Field";
@@ -33,6 +36,7 @@ export type { PrimativeKey } from "./ast/Primative";
export { Struct } from "./ast/Struct";
export { SwiftArray } from "./ast/SwiftArray";
export { SwiftClass } from "./ast/SwiftClass";
export { SwiftDictionary as SwiftMap } from "./ast/SwiftDictionary";
export { VariableType } from "./ast/VariableType";
export { SwiftFile } from "./project/SwiftFile";

@@ -52,6 +56,10 @@ export default class Swift {
return new FileHeader(args);
}

public static makeComment(args: Comment.Args): Comment {
return new Comment(args);
}

public static makeTypeAlias(args: TypeAlias.Args): TypeAlias {
return new TypeAlias(args);
}
@@ -96,6 +104,10 @@ export default class Swift {
return new SwiftArray(args);
}

public static makeDictionary(args: SwiftDictionary.Args): SwiftDictionary {
return new SwiftDictionary(args);
}

public static makeOptional(args: Optional.Args): Optional {
return new Optional(args);
}
@@ -240,14 +252,15 @@ export default class Swift {
*
* }
*/
makeCodableStruct(props: { safeName: string, codingArgs: EnumCase.Args[], fields: Field[] }): File {
makeCodableStruct(props: { safeName: string, codingArgs: EnumCase.Args[], fields: Field[], comment?: string }): File {

return Swift.makeFile({
fileHeader: Swift.factories.fileHeaders.makeHeaderWithFernStub(props.safeName),
imports: [
Swift.factories.imports.makeFoundation()
],
node: Swift.makeStruct({
comment: props.comment,
accessLevel: AccessLevel.Public,
name: props.safeName,
inheritance: [
@@ -257,6 +270,7 @@ export default class Swift {
Swift.factories.enums.makeCodingKeys(props.codingArgs),
],
fields: props.fields,
addInitializer: true,
})
});
}
Loading