Skip to content

Commit

Permalink
Update code generation for JSR slowtypes rules
Browse files Browse the repository at this point in the history
Generally, all public functions shall have explicit return types.
  • Loading branch information
danopia committed Sep 18, 2024
1 parent d6b5d77 commit f7d3012
Show file tree
Hide file tree
Showing 44 changed files with 1,141 additions and 1,101 deletions.
45 changes: 42 additions & 3 deletions generation/codegen-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export function generateModuleTypescript(surface: SurfaceMap, api: SurfaceApi):
chunks.push(` }\n`);

if (hasNamespaced) {
chunks.push(` namespace(name: string) {`);
chunks.push(` namespace(name: string): ${api.friendlyName}NamespacedApi {`);
chunks.push(` return new ${api.friendlyName}NamespacedApi(this.#client, name);`);
chunks.push(` }`);
chunks.push(` myNamespace() {`);
chunks.push(` myNamespace(): ${api.friendlyName}NamespacedApi {`);
chunks.push(` if (!this.#client.defaultNamespace) throw new Error("No current namespace is set");`);
chunks.push(` return new ${api.friendlyName}NamespacedApi(this.#client, this.#client.defaultNamespace);`);
chunks.push(` }\n`);
Expand Down Expand Up @@ -185,10 +185,49 @@ export function generateModuleTypescript(surface: SurfaceMap, api: SurfaceApi):
// return AcmeCertManagerIoV1.toOrder(resp);
// }

chunks.push(` async ${funcName}(${writeSig(args, opts, ' ')}) {`);
const isWatch = op.operationName.startsWith('watch');
const isStream = op.operationName.startsWith('stream');

let returnSig = 'unknown';
if (expectsTunnel) {
returnSig = `tunnels.${expectsTunnel}`;
} else if (isStream) {
if (accept === 'text/plain') {
returnSig = 'ReadableStream<string>';
} else {
returnSig = 'ReadableStream<Uint8Array>';
}
} else if (accept === 'text/plain') {
returnSig = 'string';
} else if (outShape) {
let shape = outShape;
// QUIRK: individual deletes can either return a Status or the 'lastExisting' resource
// More details further down in this file
if (shape.type === 'foreign' &&
shape.api.apiGroup === 'meta' &&
shape.name === 'Status' &&
op.method === 'delete') {
shape = api.shapes.shapes.get(op.operationName.replace(/^delete/, ''))!;
}

if (shape.type === 'foreign') {
returnSig = `${shape.api.friendlyName}.${shape.name}`;
} else if (shape.reference) {
returnSig = `${api.friendlyName}.${shape.reference}`;
} else throw new Error(`TODO: weird output shape on ${op.operationId}`);

if (op["x-kubernetes-action"] == 'delete') {
returnSig += ` | MetaV1.Status`;
}
if (isWatch) {
returnSig = `ReadableStream<c.WatchEvent<${returnSig.slice(0, -4)} & c.ApiKind, MetaV1.Status & c.ApiKind>>`;
}
} else {
// returnSig = 'void';
}

chunks.push(` async ${funcName}(${writeSig(args, opts, ' ')}): Promise<${returnSig}> {`);

const allOptKeys = opts.map(x => x[0].name).sort().join(',');
const knownOptShape = knownOpts[allOptKeys];
if (!knownOptShape) {
Expand Down
2 changes: 1 addition & 1 deletion generation/codegen-structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function generateStructsTypescript(surface: SurfaceMap, apiS: SurfaceApi)
const extraStruct = extraStructs.shift();
if (!extraStruct) break;

chunks.push(`export function to${extraStruct.name}(input: c.JSONValue) {`);
chunks.push(`function to${extraStruct.name}(input: c.JSONValue) {`);
chunks.push(` const obj = c.checkObj(input);`);
chunks.push(` return {`);
for (const [field, inner] of extraStruct.struct.fields) {
Expand Down
70 changes: 35 additions & 35 deletions lib/argo-cd/argoproj.io@v1alpha1/mod.ts

Large diffs are not rendered by default.

274 changes: 137 additions & 137 deletions lib/argo-cd/argoproj.io@v1alpha1/structs.ts

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions lib/builtin/admissionregistration.k8s.io@v1/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AdmissionregistrationV1Api {
this.#client = client;
}

async getMutatingWebhookConfigurationList(opts: operations.GetListOpts = {}) {
async getMutatingWebhookConfigurationList(opts: operations.GetListOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfigurationList> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}mutatingwebhookconfigurations`,
Expand All @@ -24,7 +24,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfigurationList(resp);
}

async watchMutatingWebhookConfigurationList(opts: operations.WatchListOpts = {}) {
async watchMutatingWebhookConfigurationList(opts: operations.WatchListOpts = {}): Promise<ReadableStream<c.WatchEvent<AdmissionregistrationV1.MutatingWebhookConfiguration & c.ApiKind, MetaV1.Status & c.ApiKind>>> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}mutatingwebhookconfigurations`,
Expand All @@ -36,7 +36,7 @@ export class AdmissionregistrationV1Api {
return resp.pipeThrough(new c.WatchEventTransformer(AdmissionregistrationV1.toMutatingWebhookConfiguration, MetaV1.toStatus));
}

async createMutatingWebhookConfiguration(body: AdmissionregistrationV1.MutatingWebhookConfiguration, opts: operations.PutOpts = {}) {
async createMutatingWebhookConfiguration(body: AdmissionregistrationV1.MutatingWebhookConfiguration, opts: operations.PutOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "POST",
path: `${this.#root}mutatingwebhookconfigurations`,
Expand All @@ -48,7 +48,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfiguration(resp);
}

async deleteMutatingWebhookConfigurationList(opts: operations.DeleteListOpts = {}) {
async deleteMutatingWebhookConfigurationList(opts: operations.DeleteListOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfigurationList> {
const resp = await this.#client.performRequest({
method: "DELETE",
path: `${this.#root}mutatingwebhookconfigurations`,
Expand All @@ -59,7 +59,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfigurationList(resp);
}

async getMutatingWebhookConfiguration(name: string, opts: operations.NoOpts = {}) {
async getMutatingWebhookConfiguration(name: string, opts: operations.NoOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}mutatingwebhookconfigurations/${name}`,
Expand All @@ -69,7 +69,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfiguration(resp);
}

async deleteMutatingWebhookConfiguration(name: string, opts: operations.DeleteOpts = {}) {
async deleteMutatingWebhookConfiguration(name: string, opts: operations.DeleteOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfiguration | MetaV1.Status> {
const resp = await this.#client.performRequest({
method: "DELETE",
path: `${this.#root}mutatingwebhookconfigurations/${name}`,
Expand All @@ -81,7 +81,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfiguration(resp);
}

async replaceMutatingWebhookConfiguration(name: string, body: AdmissionregistrationV1.MutatingWebhookConfiguration, opts: operations.PutOpts = {}) {
async replaceMutatingWebhookConfiguration(name: string, body: AdmissionregistrationV1.MutatingWebhookConfiguration, opts: operations.PutOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "PUT",
path: `${this.#root}mutatingwebhookconfigurations/${name}`,
Expand All @@ -93,7 +93,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfiguration(resp);
}

async patchMutatingWebhookConfiguration(name: string, type: c.PatchType, body: AdmissionregistrationV1.MutatingWebhookConfiguration | c.JsonPatch, opts: operations.PatchOpts = {}) {
async patchMutatingWebhookConfiguration(name: string, type: c.PatchType, body: AdmissionregistrationV1.MutatingWebhookConfiguration | c.JsonPatch, opts: operations.PatchOpts = {}): Promise<AdmissionregistrationV1.MutatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "PATCH",
path: `${this.#root}mutatingwebhookconfigurations/${name}`,
Expand All @@ -106,7 +106,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toMutatingWebhookConfiguration(resp);
}

async getValidatingWebhookConfigurationList(opts: operations.GetListOpts = {}) {
async getValidatingWebhookConfigurationList(opts: operations.GetListOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfigurationList> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}validatingwebhookconfigurations`,
Expand All @@ -117,7 +117,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfigurationList(resp);
}

async watchValidatingWebhookConfigurationList(opts: operations.WatchListOpts = {}) {
async watchValidatingWebhookConfigurationList(opts: operations.WatchListOpts = {}): Promise<ReadableStream<c.WatchEvent<AdmissionregistrationV1.ValidatingWebhookConfiguration & c.ApiKind, MetaV1.Status & c.ApiKind>>> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}validatingwebhookconfigurations`,
Expand All @@ -129,7 +129,7 @@ export class AdmissionregistrationV1Api {
return resp.pipeThrough(new c.WatchEventTransformer(AdmissionregistrationV1.toValidatingWebhookConfiguration, MetaV1.toStatus));
}

async createValidatingWebhookConfiguration(body: AdmissionregistrationV1.ValidatingWebhookConfiguration, opts: operations.PutOpts = {}) {
async createValidatingWebhookConfiguration(body: AdmissionregistrationV1.ValidatingWebhookConfiguration, opts: operations.PutOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "POST",
path: `${this.#root}validatingwebhookconfigurations`,
Expand All @@ -141,7 +141,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfiguration(resp);
}

async deleteValidatingWebhookConfigurationList(opts: operations.DeleteListOpts = {}) {
async deleteValidatingWebhookConfigurationList(opts: operations.DeleteListOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfigurationList> {
const resp = await this.#client.performRequest({
method: "DELETE",
path: `${this.#root}validatingwebhookconfigurations`,
Expand All @@ -152,7 +152,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfigurationList(resp);
}

async getValidatingWebhookConfiguration(name: string, opts: operations.NoOpts = {}) {
async getValidatingWebhookConfiguration(name: string, opts: operations.NoOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "GET",
path: `${this.#root}validatingwebhookconfigurations/${name}`,
Expand All @@ -162,7 +162,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfiguration(resp);
}

async deleteValidatingWebhookConfiguration(name: string, opts: operations.DeleteOpts = {}) {
async deleteValidatingWebhookConfiguration(name: string, opts: operations.DeleteOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfiguration | MetaV1.Status> {
const resp = await this.#client.performRequest({
method: "DELETE",
path: `${this.#root}validatingwebhookconfigurations/${name}`,
Expand All @@ -174,7 +174,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfiguration(resp);
}

async replaceValidatingWebhookConfiguration(name: string, body: AdmissionregistrationV1.ValidatingWebhookConfiguration, opts: operations.PutOpts = {}) {
async replaceValidatingWebhookConfiguration(name: string, body: AdmissionregistrationV1.ValidatingWebhookConfiguration, opts: operations.PutOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "PUT",
path: `${this.#root}validatingwebhookconfigurations/${name}`,
Expand All @@ -186,7 +186,7 @@ export class AdmissionregistrationV1Api {
return AdmissionregistrationV1.toValidatingWebhookConfiguration(resp);
}

async patchValidatingWebhookConfiguration(name: string, type: c.PatchType, body: AdmissionregistrationV1.ValidatingWebhookConfiguration | c.JsonPatch, opts: operations.PatchOpts = {}) {
async patchValidatingWebhookConfiguration(name: string, type: c.PatchType, body: AdmissionregistrationV1.ValidatingWebhookConfiguration | c.JsonPatch, opts: operations.PatchOpts = {}): Promise<AdmissionregistrationV1.ValidatingWebhookConfiguration> {
const resp = await this.#client.performRequest({
method: "PATCH",
path: `${this.#root}validatingwebhookconfigurations/${name}`,
Expand Down
Loading

0 comments on commit f7d3012

Please sign in to comment.