Skip to content

Commit

Permalink
(feat): setup root and sub client instantiations (#3351)
Browse files Browse the repository at this point in the history
* fix

* (chore): setup root and sub client instantiation

* (fix): model snapshots are updated
  • Loading branch information
dsinghvi authored Apr 10, 2024
1 parent 32ae9ed commit cef2a9f
Show file tree
Hide file tree
Showing 243 changed files with 6,023 additions and 4,598 deletions.
14 changes: 3 additions & 11 deletions generators/csharp/codegen/src/asIs/RawClient.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ public class RawClient
/// </summary>
private readonly Dictionary<String, String> _headers;

/// <summary>
/// Base URL for the API.
/// </summary>
private readonly string _apiBaseUrl;

public RawClient(string apiBaseUrl, ClientOptions clientOptions, Dictionary<String, String> headers)
public RawClient(Dictionary<String, String> headers, ClientOptions clientOptions)
{
_clientOptions = clientOptions;
_headers = headers;
_apiBaseUrl = apiBaseUrl;
}

public async Task<ApiResponse> MakeRequest<T>(HttpMethod method, string path, ApiRequest request)
public async Task<ApiResponse> MakeRequestAsync(HttpMethod method, string path, ApiRequest request)
{
var httpRequest = new HttpRequestMessage(method, this.BuildUrl(path, request.Query));
if (request.ContentType != null)
Expand Down Expand Up @@ -77,8 +71,6 @@ public class ApiRequest

public Dictionary<string, string> Headers { get; init; } = new();

public ClientOptions ClientOptions { get; init; }

public object RequestOptions { get; init; }
}

Expand Down Expand Up @@ -108,7 +100,7 @@ private Dictionary<string, string> GetHeaders(ApiRequest request)

private string BuildUrl(string path, Dictionary<string, object> query)
{
var url = $"{_apiBaseUrl}/{path}";
var url = $"{_clientOptions.BaseUrl}/{path}";
if (query.Count > 0)
{
url += "?";
Expand Down
99 changes: 51 additions & 48 deletions generators/csharp/codegen/src/ast/Class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Access } from "./Access";
import { Annotation } from "./Annotation";
import { ClassInstantiation } from "./ClassInstantiation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
Expand Down Expand Up @@ -149,33 +148,15 @@ export class Class extends AstNode {
writer.writeLine("{");

writer.indent();
this.constructors.forEach((constructor) => {
writer.write(`${constructor.access} ${this.name} (`);
constructor.parameters.forEach((parameter, index) => {
parameter.write(writer);
if (index < constructor.parameters.length - 1) {
writer.write(", ");
}
});
writer.write(")");

writer.writeLine("{");
writer.indent();
constructor.body?.write(writer);
writer.dedent();
writer.writeLine("}");
});
this.writeFields({ writer, fields: this.getFieldsByAccess(Access.Private) });
writer.dedent();

writer.indent();
this.fields.forEach((field, index) => {
field.write(writer);
writer.writeNewLineIfLastLineNot();
this.writeConstructors({ writer, constructors: this.constructors });
writer.dedent();

if (index < this.fields.length - 1) {
writer.newLine();
}
});
writer.indent();
this.writeFields({ writer, fields: this.getFieldsByAccess(Access.Public) });
writer.dedent();

writer.indent();
Expand All @@ -201,42 +182,64 @@ export class Class extends AstNode {
writer.dedent();

writer.indent();
this.methods.forEach((method, index) => {
method.write(writer);
writer.writeNewLineIfLastLineNot();
this.writeMethods({ writer, methods: this.getMethodsByAccess(Access.Public) });
writer.dedent();

if (index < this.fields.length - 1) {
writer.newLine();
}
});
writer.indent();
this.writeMethods({ writer, methods: this.getMethodsByAccess(Access.Private) });
writer.dedent();

writer.writeLine("}");
}

public getFields(): Field[] {
return this.fields;
private writeConstructors({ writer, constructors }: { writer: Writer; constructors: Class.Constructor[] }): void {
constructors.forEach((constructor, index) => {
writer.write(`${constructor.access} ${this.name} (`);
constructor.parameters.forEach((parameter, index) => {
parameter.write(writer);
if (index < constructor.parameters.length - 1) {
writer.write(", ");
}
});
writer.write(")");

writer.writeLine(" {");
writer.indent();
constructor.body?.write(writer);
writer.dedent();
writer.writeLine("}");
writer.newLine();
});
}

public getInitializer(args: Map<Field, CodeBlock>): ClassInstantiation {
return new ClassInstantiation({
classReference: this.reference,
arguments_: args
private writeMethods({ writer, methods }: { writer: Writer; methods: Method[] }): void {
methods.forEach((method, index) => {
method.write(writer);
writer.writeNewLineIfLastLineNot();
writer.newLine();
});
}

public getInitializerFromExample(example: Map<string, unknown>): ClassInstantiation {
const args = new Map<Field, CodeBlock>();
for (const field of this.fields) {
const value = example.get(field.name);
if (value !== undefined) {
// TODO: actually handle these examples
args.set(field, new CodeBlock({ value: value as string }));
private getMethodsByAccess(access: Access): Method[] {
return this.methods.filter((method) => method.access === access);
}

private writeFields({ writer, fields }: { writer: Writer; fields: Field[] }): void {
fields.forEach((field, index) => {
field.write(writer);
writer.writeNewLineIfLastLineNot();

if (index < this.fields.length - 1) {
writer.newLine();
}
}
return new ClassInstantiation({
classReference: this.reference,
arguments_: args
});
}

private getFieldsByAccess(access: Access): Field[] {
return this.fields.filter((field) => field.access === access);
}

public getFields(): Field[] {
return this.fields;
}
}
35 changes: 26 additions & 9 deletions generators/csharp/codegen/src/ast/ClassInstantiation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@ import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";
import { Field } from "./Field";

export declare namespace ClassInstantiation {
interface Args {
classReference: ClassReference;
// A map of the field for the class and the value to be assigned to it.
arguments_: Map<Field, CodeBlock>;
arguments_: NamedArgument[] | UnnamedArgument[];
}

interface NamedArgument {
name: string;
assignment: CodeBlock;
}

type UnnamedArgument = CodeBlock;
}

export class ClassInstantiation extends AstNode {
public readonly classReference: ClassReference;
public readonly arguments: Map<Field, CodeBlock>;
public readonly arguments_: ClassInstantiation.NamedArgument[] | ClassInstantiation.UnnamedArgument[];

constructor({ classReference, arguments_ }: ClassInstantiation.Args) {
super();
this.classReference = classReference;
this.arguments = arguments_;
this.arguments_ = arguments_;
}

public write(writer: Writer): void {
writer.write(`new ${this.classReference.name}(`);

writer.newLine();
writer.indent();
[...this.arguments.entries()].forEach(([field, assignment], idx) => {
writer.write(`${field.name}: `);
assignment.write(writer);
if (idx < this.arguments.size - 1) {
this.arguments_.forEach((argument, idx) => {
if (isNamedArgument(argument)) {
writer.write(`${argument.name}: `);
argument.assignment.write(writer);
} else {
argument.write(writer);
}
if (idx < this.arguments_.length - 1) {
writer.write(", ");
}
});
writer.dedent();

writer.write(")");
writer.writeLine(");");
}
}

function isNamedArgument(
argument: ClassInstantiation.NamedArgument | ClassInstantiation.UnnamedArgument
): argument is ClassInstantiation.NamedArgument {
return (argument as ClassInstantiation.NamedArgument)?.name != null;
}
9 changes: 4 additions & 5 deletions generators/csharp/codegen/src/ast/CodeBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";

export declare namespace CodeBlock {
interface Args {
/* Write arbitrary code */
value: string | ((writer: Writer) => void);
}

/* Write arbitrary code */
type Arg = string | ((writer: Writer) => void);
}

export class CodeBlock extends AstNode {
private value: string | ((writer: Writer) => void);

constructor({ value }: CodeBlock.Args) {
constructor(value: CodeBlock.Arg) {
super();
this.value = value;
}
Expand Down
49 changes: 49 additions & 0 deletions generators/csharp/codegen/src/ast/Dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";
import { Type } from "./Type";

export declare namespace Dictionary {
interface Args {
keyType: Type;
valueType: Type;
entries: MapEntry[];
}

interface MapEntry {
key: CodeBlock;
value: CodeBlock;
}
}

export class Dictionary extends AstNode {
private keyType: Type;
private valueType: Type;
private entries: Dictionary.MapEntry[];

constructor({ keyType, valueType, entries }: Dictionary.Args) {
super();
this.keyType = keyType;
this.valueType = valueType;
this.entries = entries;
}

public write(writer: Writer): void {
writer.write("new Dictionary<");
this.keyType.write(writer);
writer.write(", ");
this.valueType.write(writer);
writer.write("> {");
writer.newLine();
writer.indent();
for (const { key, value } of this.entries) {
writer.write("{ ");
key.write(writer);
writer.write(", ");
value.write(writer);
writer.writeLine(" }, ");
}
writer.dedent();
writer.write("}");
}
}
5 changes: 4 additions & 1 deletion generators/csharp/codegen/src/ast/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export declare namespace Field {

export class Field extends AstNode {
public readonly name: string;
public readonly access: Access;
private type: Type;
private get: boolean;
private init: boolean;
private access: Access;
private annotations: Annotation[];
private initializer: CodeBlock | undefined;
private summary: string | undefined;
Expand Down Expand Up @@ -86,6 +86,7 @@ export class Field extends AstNode {
writer.write(`${this.access} `);
writer.writeNode(this.type);
writer.write(` ${this.name}`);

if (this.get || this.init) {
writer.write(" { ");
if (this.get) {
Expand All @@ -101,6 +102,8 @@ export class Field extends AstNode {
writer.write(" = ");
this.initializer.write(writer);
writer.writeLine(";");
} else if (!this.get && !this.init) {
writer.writeLine(";");
}
}
}
Loading

0 comments on commit cef2a9f

Please sign in to comment.