Skip to content

Commit

Permalink
several sdk fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo committed Feb 2, 2024
1 parent f58f17b commit 27d97c9
Show file tree
Hide file tree
Showing 195 changed files with 737 additions and 639 deletions.
10 changes: 10 additions & 0 deletions generators/ruby/codegen/src/ast/Parameter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Argument } from "./Argument";
import { ClassReference, NilValue } from "./classes/ClassReference";
import { AstNode } from "./core/AstNode";
import { Import } from "./Import";
Expand Down Expand Up @@ -58,4 +59,13 @@ export class Parameter extends AstNode {
this.type.forEach((cr) => (imports = new Set([...imports, ...cr.getImports()])));
return imports;
}

public toArgument(value: Variable | string): Argument {
return new Argument({
name: this.name,
type: this.type,
value,
isNamed: this.isNamed
});
}
}
12 changes: 7 additions & 5 deletions generators/ruby/codegen/src/ast/Yardoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class Yardoc extends AstNode {
this.addText({ stringContent: parameter.name, templateString: "# @param %s [Hash] ", startingTabSpaces });
this.addText({
stringContent:
parameter.documentation ??
parameter.documentation?.replaceAll("\n", "") ??
`Request of type ${parameter.type.map((param) => param.typeHint).join(", ")}, as a Hash`,
appendToLastString: true
});
Expand All @@ -99,15 +99,15 @@ export class Yardoc extends AstNode {
private writeParameterAsClass(parameter: Parameter, startingTabSpaces: number): void {
if (parameter.isBlock) {
this.addText({ stringContent: parameter.name, templateString: "# @yield", startingTabSpaces });
this.addText({ stringContent: parameter.documentation, appendToLastString: true });
this.addText({ stringContent: parameter.documentation?.replaceAll("\n", ""), appendToLastString: true });
} else {
this.addText({ stringContent: parameter.name, templateString: "# @param %s", startingTabSpaces });
this.addText({
stringContent: parameter.type.map((param) => param.typeHint).join(", "),
templateString: " [%s] ",
appendToLastString: true
});
this.addText({ stringContent: parameter.documentation, appendToLastString: true });
this.addText({ stringContent: parameter.documentation?.replaceAll("\n", ""), appendToLastString: true });
}
}

Expand All @@ -121,9 +121,11 @@ export class Yardoc extends AstNode {
? this.reference.type.typeHint
: this.reference.type;
const documentation =
this.reference.type instanceof Property ? this.reference.type.documentation : undefined;
this.reference.type instanceof Property
? this.reference.type.documentation?.replaceAll("\n", "")
: undefined;
this.addText({ stringContent: typeName, templateString: "# @type [%s] ", startingTabSpaces });
this.addText({ stringContent: documentation, appendToLastString: true });
this.addText({ stringContent: documentation?.replaceAll("\n", ""), appendToLastString: true });
} else {
this.reference.parameters.forEach((parameter) => {
if (parameter.describeAsHashInYardoc) {
Expand Down
3 changes: 2 additions & 1 deletion generators/ruby/codegen/src/ast/classes/Class_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class Class_ extends AstNode {
isAssignment: true,
yardoc
});
})
}),
invocationName: "new"
});
functions = [this.initializer, ...functions];
} else if (initializerOverride !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export class FunctionInvocation extends AstNode {
templateString: this.optionalSafeCall ? "%s&." : "%s.",
startingTabSpaces
});
this.addText({ stringContent: this.baseFunction?.name, startingTabSpaces, appendToLastString: true });
this.addText({
stringContent: this.baseFunction?.invocationName ?? this.baseFunction?.name,
startingTabSpaces,
appendToLastString: true
});
this.addText({ stringContent: this.writeArgmuments(), appendToLastString: true });
this.writeBlock(startingTabSpaces);
}
Expand Down
5 changes: 5 additions & 0 deletions generators/ruby/codegen/src/ast/functions/Function_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export declare namespace Function_ {
returnValue?: ClassReference | ClassReference[];
flattenedProperties?: Map<TypeId, ObjectProperty[]>;
crf?: ClassReferenceFactory;
invocationName?: string;
}
}
export class Function_ extends AstNode {
Expand All @@ -29,6 +30,8 @@ export class Function_ extends AstNode {
public isStatic: boolean;
public yardoc: Yardoc;

public invocationName: string | undefined;

constructor({
name,
functionBody,
Expand All @@ -38,6 +41,7 @@ export class Function_ extends AstNode {
isAsync = false,
isStatic = false,
returnValue,
invocationName,
...rest
}: Function_.Init) {
super(rest);
Expand All @@ -47,6 +51,7 @@ export class Function_ extends AstNode {
this.functionBody = functionBody;
this.isAsync = isAsync;
this.isStatic = isStatic;
this.invocationName = invocationName;

this.yardoc = new Yardoc({
reference: { name: "docString", parameters, returnValue: this.returnValue },
Expand Down
14 changes: 13 additions & 1 deletion generators/ruby/codegen/src/ast/gem/Gemspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Gemspec extends FunctionInvocation {
globalDependencies.push(
...[
new ExternalDependency({ packageName: "mini_mime", specifier: "~>", version: "1.1" }),
new ExternalDependency({ packageName: "farady-multipart", specifier: "~>", version: "1.0" })
new ExternalDependency({ packageName: "faraday-multipart", specifier: "~>", version: "1.0" })
]
);
}
Expand All @@ -53,6 +53,18 @@ export class Gemspec extends FunctionInvocation {
isAssignment: true
})
);
} else {
// Allow for people to use the gemconfig if no version is found
gemBlock.push(
new Expression({
leftSide: "spec.version",
rightSide: new ClassReference({
name: `${clientName}::Gemconfig::VERSION`,
import_: new Import({ from: "lib/gemconfig" })
}),
isAssignment: true
})
);
}

super({
Expand Down
5 changes: 5 additions & 0 deletions generators/ruby/codegen/src/utils/RubyUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export function getLocationFromFernFilepath(fernFilepath: FernFilepath): string
return [...fernFilepath.allParts.map((pathPart) => pathPart.snakeCase.safeName)].join("/");
}

export function getBreadcrumbsFromFilepath(fernFilepath: FernFilepath): string[] {
return fernFilepath.allParts.map((pathPart) => pathPart.pascalCase.safeName);
}

export function generateGemspec(
clientName: string,
gemName: string,
Expand Down Expand Up @@ -71,6 +75,7 @@ export function generateGemConfig(clientName: string): GeneratedRubyFile {
child: new Module_({
name: "Gemconfig",
child: [
new Expression({ leftSide: "VERSION", rightSide: '""', isAssignment: true }),
new Expression({ leftSide: "AUTHORS", rightSide: '[""].freeze', isAssignment: true }),
new Expression({ leftSide: "EMAIL", rightSide: '""', isAssignment: true }),
new Expression({ leftSide: "SUMMARY", rightSide: '""', isAssignment: true }),
Expand Down
63 changes: 44 additions & 19 deletions generators/ruby/sdk/src/AbstractionUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Function_,
GeneratedRubyFile,
GenericClassReference,
getBreadcrumbsFromFilepath,
getLocationForServiceDeclaration,
getLocationFromFernFilepath,
HashInstance,
Expand Down Expand Up @@ -170,6 +171,9 @@ export function generateRootPackage(
});
const clientClass = new Class_({
classReference,
properties: syncSubpackages.map(
(sp) => new Property({ name: snakeCase(sp.classReference.name), type: sp.classReference })
),
functions: rootService
? generateEndpoints(
crf,
Expand All @@ -187,13 +191,14 @@ export function generateRootPackage(
includeInitializer: false,
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
functionBody: [
new Expression({
leftSide: requestClientVariable,
rightSide: new FunctionInvocation({
onObject: requestClient.classReference,
baseFunction: requestClient.initializer,
arguments_: requestClient.properties.map((prop) => prop.toArgument(prop.name, true))
arguments_: requestClient.initializer?.parameters.map((param) => param.toArgument(param.name))
}),
isAssignment: true
}),
Expand All @@ -214,7 +219,8 @@ export function generateRootPackage(
});
})
],
parameters: requestClient.initializer?.parameters
parameters: requestClient.initializer?.parameters,
returnValue: classReference
})
});

Expand All @@ -224,11 +230,15 @@ export function generateRootPackage(
type: asyncRequestClient.classReference,
variableType: VariableType.LOCAL
});
const asyncClassReference = new ClassReference({
name: "AsyncClient",
import_: new Import({ from: gemName, isExternal: false })
});
const asyncClientClass = new Class_({
classReference: new ClassReference({
name: "AsyncClient",
import_: new Import({ from: gemName, isExternal: false })
}),
classReference: asyncClassReference,
properties: asyncSubpackages.map(
(sp) => new Property({ name: snakeCase(sp.classReference.name), type: sp.classReference })
),
functions: rootService
? generateEndpoints(
crf,
Expand All @@ -246,6 +256,7 @@ export function generateRootPackage(
includeInitializer: false,
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
functionBody: [
new Expression({
leftSide: asyncRequestClientVariable,
Expand Down Expand Up @@ -273,7 +284,8 @@ export function generateRootPackage(
});
})
],
parameters: asyncRequestClient.initializer?.parameters
parameters: asyncRequestClient.initializer?.parameters,
returnValue: asyncClassReference
})
});

Expand Down Expand Up @@ -303,18 +315,22 @@ export function generateSubpackage(
asyncSubpackages: Class_[] = []
): ClientClassPair {
const location = getLocationFromFernFilepath(package_.fernFilepath) + "client";
const moduleBreadcrumbs = getBreadcrumbsFromFilepath(package_.fernFilepath);

// Add Client class
const requestClientProperty = new Property({ name: "request_client", type: requestClientCr });
const syncClassReference = new ClassReference({
name: "Client",
import_: new Import({ from: location, isExternal: false }),
moduleBreadcrumbs
});
const syncClientClass = new Class_({
classReference: new ClassReference({
name: "Client",
import_: new Import({ from: location, isExternal: false })
}),
classReference: syncClassReference,
properties: [requestClientProperty],
includeInitializer: false,
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: subpackages.map((sp) => {
const subpackageClassVariable = new Variable({
Expand All @@ -337,21 +353,25 @@ export function generateSubpackage(
isAssignment: true
});
}),
parameters: [new Parameter({ name: "client", type: requestClientCr })]
parameters: [new Parameter({ name: "client", type: requestClientCr })],
returnValue: syncClassReference
})
});

// Add Async Client class
const asyncRequestClientProperty = new Property({ name: "request_client", type: asyncRequestClientCr });
const asyncClassReference = new ClassReference({
name: "AsyncClient",
import_: new Import({ from: location, isExternal: false }),
moduleBreadcrumbs
});
const asyncClientClass = new Class_({
classReference: new ClassReference({
name: "AsyncClient",
import_: new Import({ from: location, isExternal: false })
}),
classReference: asyncClassReference,
properties: [new Property({ name: "client", type: asyncRequestClientCr })],
includeInitializer: false,
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: asyncSubpackages.map((sp) => {
const subpackageClassVariable = new Variable({
Expand All @@ -374,7 +394,8 @@ export function generateSubpackage(
isAssignment: true
});
}),
parameters: [new Parameter({ name: "client", type: asyncRequestClientCr })]
parameters: [new Parameter({ name: "client", type: asyncRequestClientCr })],
returnValue: asyncClassReference
})
});

Expand All @@ -394,14 +415,16 @@ export function generateService(
): ClientClassPair {
const serviceName = service.name.fernFilepath.file?.pascalCase.safeName ?? "";
const import_ = new Import({ from: getLocationForServiceDeclaration(service.name), isExternal: false });
const moduleBreadcrumbs = getBreadcrumbsFromFilepath(service.name.fernFilepath);

// Add Client class
const serviceBasePath = generateRubyPathTemplate(service.pathParameters, service.basePath);
const requestClientProperty = new Property({ name: "request_client", type: requestClientCr });
const syncClientClass = new Class_({
classReference: new ClassReference({
name: `${serviceName}Client`,
import_
import_,
moduleBreadcrumbs
}),
properties: [requestClientProperty],
includeInitializer: true,
Expand All @@ -424,7 +447,8 @@ export function generateService(
const asyncClientClass = new Class_({
classReference: new ClassReference({
name: `Async${serviceName}Client`,
import_
import_,
moduleBreadcrumbs
}),
properties: [asyncRequestClientProperty],
includeInitializer: true,
Expand Down Expand Up @@ -657,6 +681,7 @@ function generateRequestClientInitializer(

return new Function_({
name: "initialize",
invocationName: "new",
parameters: [
...functionParams,
// Select sample of the request overrides object properties
Expand Down
1 change: 1 addition & 0 deletions seed/ruby-sdk/alias/lib/gemconfig.rb

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

8 changes: 4 additions & 4 deletions seed/ruby-sdk/alias/lib/seed_alias_client.rb

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

1 change: 1 addition & 0 deletions seed/ruby-sdk/alias/seed_alias_client.gemspec

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

1 change: 1 addition & 0 deletions seed/ruby-sdk/api-wide-base-path/lib/gemconfig.rb

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

Loading

0 comments on commit 27d97c9

Please sign in to comment.