-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial header serialization * Update tests
- Loading branch information
Showing
10 changed files
with
270 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/typespec-ts/src/modular/serialization/collectionUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CollectionFormat } from "@azure-tools/typespec-client-generator-core"; | ||
|
||
export function getCollectionSeparator( | ||
collectionFormat?: CollectionFormat | ||
): string { | ||
switch (collectionFormat) { | ||
case "csv": | ||
return ","; | ||
case "ssv": | ||
return " "; | ||
case "tsv": | ||
return "\t"; | ||
case "pipes": | ||
return "|"; | ||
case "multi": | ||
return "&"; | ||
default: | ||
return ","; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/typespec-ts/src/modular/serialization/serializeHeaders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SdkHeaderParameter } from "@azure-tools/typespec-client-generator-core"; | ||
import { | ||
SerializeTypeOptions, | ||
serializeArray, | ||
serializeType | ||
} from "./serializers.js"; | ||
import { SerializerOutput } from "./util.js"; | ||
import { getCollectionSeparator } from "./collectionUtils.js"; | ||
|
||
export function serializeHeader( | ||
options: SerializeTypeOptions< | ||
SdkHeaderParameter & { | ||
kind: "header"; | ||
} | ||
> | ||
): SerializerOutput { | ||
const { type } = options; | ||
|
||
if (type.type.kind === "array") { | ||
const serializedArray = serializeArray({ ...options, type: type.type }); | ||
const separator = JSON.stringify( | ||
getCollectionSeparator(type.collectionFormat) | ||
); | ||
return `${serializedArray}.join(${separator})`; | ||
} | ||
|
||
return serializeType({ ...options, type: type.type }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { | ||
createTestLibrary, | ||
findTestPackageRoot | ||
} from "@typespec/compiler/testing"; | ||
|
||
export const TypespecTsTestLibrary = createTestLibrary({ | ||
name: "@azure-tools/typespec-ts", | ||
// Set this to the absolute path to the root of the package. (e.g. in this case this file would be compiled to ./dist/src/testing/index.js) | ||
packageRoot: await findTestPackageRoot(import.meta.url) | ||
}); |
112 changes: 112 additions & 0 deletions
112
packages/typespec-ts/test-next/integration/serialization/serializeHeaders.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { describe, it, assert, beforeEach } from "vitest"; | ||
import { createMyTestRunner, createTcgcContext } from "./test-hots.js"; | ||
import { SdkHeaderParameter } from "@azure-tools/typespec-client-generator-core"; | ||
import { serializeHeader } from "../../../src/modular/serialization/serializeHeaders.js"; | ||
|
||
import { EmitContext, UsageFlags } from "@typespec/compiler"; | ||
import { BasicTestRunner } from "@typespec/compiler/testing"; | ||
import { SerializeTypeOptions } from "../../../src/modular/serialization/serializers.js"; | ||
|
||
describe("headerSerializer", () => { | ||
let runner: BasicTestRunner; | ||
|
||
beforeEach(async () => { | ||
runner = await createMyTestRunner(); | ||
}); | ||
|
||
it("should handle a simple header", async () => { | ||
const headerParam = await getSdkHeader(`@header param: string`, runner); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, "param"); | ||
}); | ||
|
||
it("should handle a simple array header", async () => { | ||
const headerParam = await getSdkHeader( | ||
`@header({format: "csv"}) param: string[]`, | ||
runner | ||
); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, 'param.join(",")'); | ||
}); | ||
|
||
it("should handle a numeric array header", async () => { | ||
const headerParam = await getSdkHeader( | ||
`@header({format: "csv"}) param: int32[]`, | ||
runner | ||
); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, `param.join(",")`); | ||
}); | ||
|
||
it("should handle a boolean array header", async () => { | ||
const headerParam = await getSdkHeader( | ||
`@header({format: "csv"}) param: boolean[]`, | ||
runner | ||
); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, `param.join(",")`); | ||
}); | ||
|
||
// Enable when parameter typename is fixed | ||
it.skip("should handle a datetime array header", async () => { | ||
const headerParam = await getSdkHeader( | ||
`@header({format: "csv"}) param: utcDateTime[]`, | ||
runner | ||
); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, `param.map((e: Date)=>((e).toISOString())).join(",")`); | ||
}); | ||
|
||
it("should handle a string array header with tsv format", async () => { | ||
const headerParam = await getSdkHeader( | ||
`@header({format: "tsv"}) param: string[]`, | ||
runner | ||
); | ||
const result = serializeHeader(headerParam); | ||
|
||
assert.equal(result, `param.join("\\t")`); | ||
}); | ||
}); | ||
|
||
type HeaderType = SerializeTypeOptions< | ||
SdkHeaderParameter & { | ||
kind: "header"; | ||
} | ||
>; | ||
|
||
async function getSdkHeader( | ||
code: string, | ||
runner: BasicTestRunner | ||
): Promise<HeaderType> { | ||
const template = ` @service({ | ||
title: "Widget Service", | ||
}) | ||
namespace DemoService; | ||
@route("/widgets") | ||
interface Widgets { | ||
@test op foo(${code}): string; | ||
}`; | ||
|
||
await runner.compile(template); | ||
|
||
const context: EmitContext = { | ||
program: runner.program | ||
} as any; | ||
|
||
const sdkContext = createTcgcContext(context); | ||
const headerParam = sdkContext.experimental_sdkPackage.clients[0].methods[0] | ||
.parameters[0] as unknown as SdkHeaderParameter; | ||
|
||
return { | ||
dpgContext: sdkContext, | ||
type: headerParam, | ||
valueExpr: headerParam.name, | ||
functionType: UsageFlags.Input | ||
} as any; | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/typespec-ts/test-next/integration/serialization/test-hots.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
SdkContext, | ||
createSdkContext | ||
} from "@azure-tools/typespec-client-generator-core"; | ||
import { EmitContext } from "@typespec/compiler"; | ||
import { createTestHost, createTestWrapper } from "@typespec/compiler/testing"; | ||
import { HttpTestLibrary } from "@typespec/http/testing"; | ||
|
||
export async function createMyTestHost() { | ||
return createTestHost({ | ||
libraries: [HttpTestLibrary] | ||
}); | ||
} | ||
|
||
export async function createMyTestRunner() { | ||
const host = await createMyTestHost(); | ||
return createTestWrapper(host, { autoUsings: ["TypeSpec.Http"] }); | ||
} | ||
|
||
export function createTcgcContext( | ||
context: EmitContext<Record<string, any>> | ||
): SdkContext { | ||
const tcgcSettings = { | ||
"generate-protocol-methods": true, | ||
"generate-convenience-methods": true, | ||
"flatten-union-as-enum": false, | ||
emitters: [ | ||
{ | ||
main: "@azure-tools/typespec-ts", | ||
metadata: { name: "@azure-tools/typespec-ts" } | ||
} | ||
] | ||
}; | ||
|
||
const contextForTcgc = { | ||
...context, | ||
options: { | ||
...context.options, | ||
...tcgcSettings | ||
} | ||
}; | ||
|
||
return createSdkContext(contextForTcgc); | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/typespec-ts/test-next/unit/serialization/collectionUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { getCollectionSeparator } from "../../../src/modular/serialization/collectionUtils.js"; | ||
|
||
describe("getCollectionSeparator", () => { | ||
it("should return ',' for csv", () => { | ||
const result = getCollectionSeparator("csv"); | ||
expect(result).toBe(","); | ||
}); | ||
|
||
it("should return ' ' for ssv", () => { | ||
const result = getCollectionSeparator("ssv"); | ||
expect(result).toBe(" "); | ||
}); | ||
|
||
it("should return '\\t' for tsv", () => { | ||
const result = getCollectionSeparator("tsv"); | ||
expect(result).toBe("\t"); | ||
}); | ||
|
||
it("should return '|' for pipes", () => { | ||
const result = getCollectionSeparator("pipes"); | ||
expect(result).toBe("|"); | ||
}); | ||
|
||
it("should return '&' for multi", () => { | ||
const result = getCollectionSeparator("multi"); | ||
expect(result).toBe("&"); | ||
}); | ||
|
||
it("should return ',' by default", () => { | ||
const result = getCollectionSeparator(undefined); | ||
expect(result).toBe(","); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters