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

Allow encode on a union #2992

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions .chronus/changes/allow-encode-union-2024-2-6-17-42-29.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

Enable the use of `@encode` for model properties that have a union type. This supports cases like `@encode("rfc3339") prop: utcDateTime | null`
21 changes: 8 additions & 13 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,25 +756,20 @@ export function $encode(
type: encodeAs ?? context.program.checker.getStdType("string"),
};
const targetType = getPropertyType(target);
if (targetType.kind !== "Scalar") {
return;
}
validateEncodeData(context, targetType, encodeData);
context.program.stateMap(encodeKey).set(target, encodeData);
}

function validateEncodeData(context: DecoratorContext, target: Scalar, encodeData: EncodeData) {
function validateEncodeData(context: DecoratorContext, target: Type, encodeData: EncodeData) {
function check(validTargets: StdTypeName[], validEncodeTypes: StdTypeName[]) {
const checker = context.program.checker;
const isTargetValid = validTargets.some((validTarget) => {
return ignoreDiagnostics(
checker.isTypeAssignableTo(
target.projectionBase ?? target,
checker.getStdType(validTarget),
target
)
);
});
const isTargetValid = isTypeIn(target.projectionBase ?? target, (type) =>
validTargets.some((validTarget) => {
return ignoreDiagnostics(
checker.isTypeAssignableTo(type, checker.getStdType(validTarget), target)
);
})
);

if (!isTargetValid) {
reportDiagnostic(context.program, {
Expand Down
24 changes: 24 additions & 0 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,30 @@ describe("compiler: built-in decorators", () => {
strictEqual(getEncode(runner.program, s)?.encoding, "rfc3339");
});

it(`set encoding on model property`, async () => {
const { prop } = (await runner.compile(`
model Foo {
@encode("rfc3339")
@test
prop: utcDateTime;
}
`)) as { prop: ModelProperty };

strictEqual(getEncode(runner.program, prop)?.encoding, "rfc3339");
});

it(`set encoding on model property of union type`, async () => {
const { prop } = (await runner.compile(`
model Foo {
@encode("rfc3339")
@test
prop: utcDateTime | null;
}
`)) as { prop: ModelProperty };

strictEqual(getEncode(runner.program, prop)?.encoding, "rfc3339");
});

it(`encode type default to string`, async () => {
const { s } = (await runner.compile(`
@encode("rfc3339")
Expand Down
Loading