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

Migrate @service and @info to use valueof for the option bag #6108

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ using TypeSpec.Http;
using TypeSpec.Rest;
/** This is a pet store service. */
@service({
title: "Pet Store Service",
})
@service(#{ title: "Pet Store Service" })
@server("https://example.com", "The service endpoint")
namespace PetStore;
Expand Down
11 changes: 8 additions & 3 deletions packages/compiler/generated-defs/TypeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import type {
UnionVariant,
} from "../src/core/index.js";

export interface ServiceOptions {
readonly title?: string;
readonly version?: string;
}

export interface ExampleOptions {
readonly title?: string;
readonly description?: string;
Expand Down Expand Up @@ -216,19 +221,19 @@ export type DeprecatedDecorator = (
* ```
* @example Setting service title
* ```typespec
* @service({title: "Pet store"})
* @service(#{title: "Pet store"})
* namespace PetStore;
* ```
* @example Setting service version
* ```typespec
* @service({version: "1.0"})
* @service(#{version: "1.0"})
* namespace PetStore;
* ```
*/
export type ServiceDecorator = (
context: DecoratorContext,
target: Namespace,
options?: Type,
options?: ServiceOptions,
) => void;

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/lib/std/decorators.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ model ServiceOptions {
*
* @example Setting service title
* ```typespec
* @service({title: "Pet store"})
* @service(#{title: "Pet store"})
* namespace PetStore;
* ```
*
* @example Setting service version
* ```typespec
* @service({version: "1.0"})
* @service(#{version: "1.0"})
* namespace PetStore;
* ```
*/
extern dec service(target: Namespace, options?: ServiceOptions);
extern dec service(target: Namespace, options?: valueof ServiceOptions);

/**
* Specify that this model is an error type. Operations return error types when the operation has failed.
Expand Down
46 changes: 6 additions & 40 deletions packages/compiler/src/lib/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ServiceDecorator } from "../../generated-defs/TypeSpec.js";
import type { ServiceDecorator, ServiceOptions } from "../../generated-defs/TypeSpec.js";
import { validateDecoratorUniqueOnNode } from "../core/decorator-utils.js";
import { Type, getTypeName, reportDeprecated } from "../core/index.js";
import { reportDiagnostic } from "../core/messages.js";
import { reportDeprecated } from "../core/index.js";
import type { Program } from "../core/program.js";
import { DecoratorContext, Namespace } from "../core/types.js";
import { Realm } from "../experimental/realm.js";
Expand Down Expand Up @@ -69,50 +68,17 @@ export function addService(
export const $service: ServiceDecorator = (
context: DecoratorContext,
target: Namespace,
options?: Type,
options?: ServiceOptions,
) => {
validateDecoratorUniqueOnNode(context, target, $service);

if (options && options.kind !== "Model") {
reportDiagnostic(context.program, {
code: "invalid-argument",
format: { value: options.kind, expected: "Model" },
target: context.getArgumentTarget(0)!,
});
return;
}
const serviceDetails: ServiceDetails = {};
const title = options?.properties.get("title")?.type;
const versionProp = options?.properties.get("version");
if (title) {
if (title.kind === "String") {
serviceDetails.title = title.value;
} else {
reportDiagnostic(context.program, {
code: "unassignable",
format: { sourceType: getTypeName(title), targetType: "String" },
target: context.getArgumentTarget(0)!,
});
}
}
if (versionProp) {
const version = versionProp.type;
if (options?.version) {
reportDeprecated(
context.program,
"version: property is deprecated in @service. If wanting to describe a service versioning you can use the `@typespec/versioning` library. If wanting to describe the project version you can use the package.json version.",
versionProp,
context.getArgumentTarget(0)!,
);
if (version.kind === "String") {
// eslint-disable-next-line @typescript-eslint/no-deprecated
serviceDetails.version = version.value;
} else {
reportDiagnostic(context.program, {
code: "unassignable",
format: { sourceType: getTypeName(version), targetType: "String" },
target: context.getArgumentTarget(0)!,
});
}
}

addService(context.program, target, serviceDetails);
addService(context.program, target, options);
};
4 changes: 1 addition & 3 deletions packages/compiler/templates/rest/main.tsp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import "@typespec/http";

using TypeSpec.Http;
@service({
title: "Widget Service",
})
@service(#{ title: "Widget Service" })
namespace DemoService;

model Widget {
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler/test/decorators/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("compiler: service", () => {

it("customize service title", async () => {
const { S } = await runner.compile(`
@test @service({title: "My Service"}) namespace S {}
@test @service(#{title: "My Service"}) namespace S {}

`);

Expand All @@ -59,7 +59,7 @@ describe("compiler: service", () => {

it("emit diagnostic if service title is not a string", async () => {
const diagnostics = await runner.diagnose(`
@test @service({title: 123}) namespace S {}
@test @service(#{title: 123}) namespace S {}
`);

expectDiagnostics(diagnostics, {
Expand All @@ -71,7 +71,7 @@ describe("compiler: service", () => {

it("customize service version", async () => {
const { S } = await runner.compile(`
@test @service({
@test @service(#{
#suppress "deprecated" "test"
version: "1.2.3"
}) namespace S {}
Expand All @@ -94,7 +94,7 @@ describe("compiler: service", () => {

it("emit diagnostic if service version is not a string", async () => {
const diagnostics = await runner.diagnose(`
@test @service({
@test @service(#{
#suppress "deprecated" "test"
version: 123
}) namespace S {}
Expand Down
18 changes: 9 additions & 9 deletions packages/compiler/test/server/get-hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe("compiler: server: on hover", () => {
it("model in namespace", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

model Ani┆mal{
Expand All @@ -355,7 +355,7 @@ describe("compiler: server: on hover", () => {
it("model with one template arg", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

model Ani┆mal<T>{
Expand All @@ -376,7 +376,7 @@ describe("compiler: server: on hover", () => {
it("model with two template args", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

model Ani┆mal<T, P>{
Expand Down Expand Up @@ -433,7 +433,7 @@ describe("compiler: server: on hover", () => {
it("interface in namespace", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

interface IAct┆ions{
Expand Down Expand Up @@ -483,7 +483,7 @@ describe("compiler: server: on hover", () => {
it("operation in namespace", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

op Ea┆t(food: string): void;
Expand All @@ -500,7 +500,7 @@ describe("compiler: server: on hover", () => {
it("operation with one template arg", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

op Ea┆t<T>(food: string): void;
Expand All @@ -517,7 +517,7 @@ describe("compiler: server: on hover", () => {
it("operation with two template args", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

op Ea┆t<T, P>(food: string): void;
Expand All @@ -534,7 +534,7 @@ describe("compiler: server: on hover", () => {
it("operation in interface", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

interface IActions {
Expand All @@ -553,7 +553,7 @@ describe("compiler: server: on hover", () => {
it("operation in interface with template", async () => {
const hover = await getHoverAtCursor(
`
@service({title: "RT"})
@service(#{title: "RT"})
namespace TestNs;

interface IActions<Q> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ describe("Test Parameter Explode", () => {
it("cookie parameter is not supported", async () => {
const program = await typeSpecCompile(
`
@service({
@service(#{
title: "Azure Csharp emitter Testing",
})
@server(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function typeSpecCompile(
const namespace = `
@versioned(Versions)
${authDecorator}
@service({
@service(#{
title: "Azure Csharp emitter Testing",
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import "@azure-tools/typespec-client-generator-core";
import "@azure-tools/typespec-azure-core";

@versioned(Versions)
@service({
title: "hello world",
})
@service(#{ title: "hello world" })
@doc("This is a sample typespec project.")
@server(
"{unbrandedTypeSpecUrl}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import "@typespec/http";
import "@azure-tools/typespec-client-generator-core";
import "@azure-tools/typespec-azure-core";

@service({
title: "hello world",
version: "0.1.0",
})
@service(#{ title: "hello world", version: "0.1.0" })
@doc("This is a sample typespec project.")
@server(
"{unbrandedTypeSpecUrl}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ using TypeSpec.OpenAPI;
using Azure.ClientGenerator.Core;

@armProviderNamespace
@service({
title: "ArmStreamStyleSerialization",
})
@service(#{ title: "ArmStreamStyleSerialization" })
@versioned(Versions)
@doc("Arm Resource Provider management API.")
namespace TspTest.ArmStreamStyleSerialization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ using TypeSpec.OpenAPI;
using Azure.ClientGenerator.Core;

@armProviderNamespace
@service({
title: "ArmResource",
})
@service(#{ title: "ArmResource" })
@versioned(Versions)
@doc("Arm Resource Provider management API.")
namespace TspTest.ArmResourceProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import "@azure-tools/typespec-client-generator-core";
using TypeSpec.Http;
using Azure.ClientGenerator.Core;

@service({
title: "Builtin",
})
@service(#{ title: "Builtin" })
namespace TspTest.Builtin;

model Builtin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ using TypeSpec.Http;
using Azure.Core;
using Azure.ClientGenerator.Core;

@service({
title: "DiscriminatorEdgeCases",
})
@service(#{ title: "DiscriminatorEdgeCases" })
namespace TspTest.DiscriminatorEdgeCases;

model ParentWithRequiredProperty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ using TypeSpec.Http;
using Azure.Core;
using Azure.ClientGenerator.Core;

@service({
title: "EnumService",
})
@service(#{ title: "EnumService" })
namespace TspTest.EnumService;

#suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "For testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ using TypeSpec.Http;
using TypeSpec.Versioning;
using Azure.Core.Foundations;

@service({
title: "ErrorModel",
})
@service(#{ title: "ErrorModel" })
@useDependency(Azure.Core.Versions.v1_0_Preview_2)
namespace TspTest.ErrorModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ using TypeSpec.Http;
using TypeSpec.Versioning;
using Azure.ClientGenerator.Core;

@service({
title: "Flatten",
})
@service(#{ title: "Flatten" })
@server(
"{endpoint}/openai",
"Flatten",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ using TypeSpec.Http;
using Azure.Core;
using Azure.ClientGenerator.Core;

@service({
title: "Internal",
})
@service(#{ title: "Internal" })
namespace TspTest.Internal;

// ApiRequest and RequestInner will be generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import "@azure-tools/typespec-azure-core";
using TypeSpec.Http;
using Azure.Core;

@service({
title: "LiteralService",
})
@service(#{ title: "LiteralService" })
namespace TspTest.LiteralService;

model Model {
Expand Down
Loading
Loading