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

Fix $rid for interfaces #517

Merged
merged 1 commit into from
Jul 24, 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
5 changes: 5 additions & 0 deletions .changeset/rich-bobcats-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/client": patch
---

Fixes rids in interfaces
13 changes: 7 additions & 6 deletions etc/client.api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ export interface BaseObjectSet<Q extends ObjectOrInterfaceDefinition> {

// Warning: (ae-forgotten-export) The symbol "ApiNameAsString" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "DropDollarOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-incompatible-release-tags) The symbol "ConvertProps" is marked as @public, but its signature references "UnionIfTrue" which is marked as @internal
// Warning: (ae-internal-missing-underscore) The name "ConvertProps" should be prefixed with an underscore because the declaration is marked as @internal
//
// @public
export type ConvertProps<FROM extends ObjectTypeDefinition<any> | InterfaceDefinition<any, any>, TO extends ValidToFrom<FROM>, P extends string = "$all"> = TO extends FROM ? P : TO extends ObjectTypeDefinition<any> ? ((UnionIfTrue<NonNullable<TO["interfaceMap"]>[ApiNameAsString<FROM>][P extends "$all" ? (keyof FROM["properties"] extends NonNullable<keyof TO["interfaceMap"]>[ApiNameAsString<FROM>] ? keyof FROM["properties"] : never) : DropDollarOptions<P>], P extends "$notStrict" ? true : false, "$notStrict">)) : UnionIfTrue<TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all" : FROM extends ObjectTypeDefinition<any> ? DropDollarOptions<P> extends keyof NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>][DropDollarOptions<P>] : never : never : never, P extends "$notStrict" ? true : false, "$notStrict">;
// @internal
export type ConvertProps<FROM extends ObjectTypeDefinition<any> | InterfaceDefinition<any, any>, TO extends ValidToFrom<FROM>, P extends string = "$all"> = TO extends FROM ? P : TO extends ObjectTypeDefinition<any> ? ((UnionIfTrue<UnionIfTrue<NonNullable<TO["interfaceMap"]>[ApiNameAsString<FROM>][P extends "$all" ? (keyof FROM["properties"] extends NonNullable<keyof TO["interfaceMap"]>[ApiNameAsString<FROM>] ? keyof FROM["properties"] : never) : DropDollarOptions<P>], P extends "$notStrict" ? true : false, "$notStrict">, P extends "$rid" ? true : false, "$rid">)) : UnionIfTrue<UnionIfTrue<TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all" : FROM extends ObjectTypeDefinition<any> ? DropDollarOptions<P> extends keyof NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>][DropDollarOptions<P>] : never : never : never, P extends "$notStrict" ? true : false, "$notStrict">, P extends "$rid" ? true : false, "$rid">;

// @public
export interface DataValueClientToWire {
Expand Down Expand Up @@ -791,9 +791,10 @@ export type WhereClause<T extends ObjectOrInterfaceDefinition<any, any>> = OrWhe

// Warnings were encountered during analysis:
//
// src/OsdkObjectFrom.ts:93:4 - (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// src/OsdkObjectFrom.ts:94:4 - (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// src/OsdkObjectFrom.ts:150:5 - (ae-forgotten-export) The symbol "UnderlyingProps" needs to be exported by the entry point index.d.ts
// src/OsdkObjectFrom.ts:102:4 - (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// src/OsdkObjectFrom.ts:103:4 - (tsdoc-param-tag-missing-hyphen) The @param block should be followed by a parameter name and then a hyphen
// src/OsdkObjectFrom.ts:159:5 - (ae-forgotten-export) The symbol "UnderlyingProps" needs to be exported by the entry point index.d.ts
// src/OsdkObjectFrom.ts:159:5 - (ae-incompatible-release-tags) The symbol "$as" is marked as @public, but its signature references "ConvertProps" which is marked as @internal

// (No @packageDocumentation comment for this package)

Expand Down
55 changes: 32 additions & 23 deletions packages/client.api/src/OsdkObjectFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ApiNameAsString<T extends ObjectOrInterfaceDefinition> = NonNullable<
* @param FROM - the interface or object type to convert from
* @param TO - the interface or object type to convert to
* @param P - the property(s) to convert
* @internal
*/
export type ConvertProps<
FROM extends ObjectTypeDefinition<any> | InterfaceDefinition<any, any>,
Expand All @@ -53,34 +54,42 @@ export type ConvertProps<
: TO extends ObjectTypeDefinition<any> ? (
(
UnionIfTrue<
NonNullable<TO["interfaceMap"]>[ApiNameAsString<FROM>][
P extends "$all" ? (
keyof FROM["properties"] extends
NonNullable<keyof TO["interfaceMap"]>[ApiNameAsString<FROM>]
? keyof FROM["properties"]
: never
)
: DropDollarOptions<P>
],
P extends "$notStrict" ? true : false,
"$notStrict"
UnionIfTrue<
NonNullable<TO["interfaceMap"]>[ApiNameAsString<FROM>][
P extends "$all" ? (
keyof FROM["properties"] extends
NonNullable<keyof TO["interfaceMap"]>[ApiNameAsString<FROM>]
? keyof FROM["properties"]
: never
)
: DropDollarOptions<P>
],
P extends "$notStrict" ? true : false,
"$notStrict"
>,
P extends "$rid" ? true : false,
"$rid"
>
)
)
: UnionIfTrue<
TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all"
: FROM extends ObjectTypeDefinition<any>
? DropDollarOptions<P> extends
keyof NonNullable<FROM["inverseInterfaceMap"]>[
ApiNameAsString<TO>
] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>][
DropDollarOptions<P>
]
UnionIfTrue<
TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all"
: FROM extends ObjectTypeDefinition<any>
? DropDollarOptions<P> extends
keyof NonNullable<FROM["inverseInterfaceMap"]>[
ApiNameAsString<TO>
] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>][
DropDollarOptions<P>
]
: never
: never
: never
: never,
P extends "$notStrict" ? true : false,
"$notStrict"
: never,
P extends "$notStrict" ? true : false,
"$notStrict"
>,
P extends "$rid" ? true : false,
"$rid"
>;

/** DO NOT EXPORT FROM PACKAGE */
Expand Down
16 changes: 10 additions & 6 deletions packages/client/src/OsdkObjectFrom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
*/

import type { ObjectOrInterfaceDefinition } from "@osdk/api";
import type {
ConvertProps,
FetchPageResult,
Osdk,
ValidToFrom,
} from "@osdk/client.api";
import type { ConvertProps, Osdk, ValidToFrom } from "@osdk/client.api";
import type { Employee, FooInterface } from "@osdk/client.test.ontology";
import { describe, expectTypeOf, it } from "vitest";

Expand Down Expand Up @@ -52,6 +47,10 @@ describe("ConvertProps", () => {
>()
.toEqualTypeOf<"fullName" | "$notStrict">();
});
it("handles $all | $rid", () => {
expectTypeOf<ConvertProps<FooInterface, Employee, "$all" | "$rid">>()
.toEqualTypeOf<"fullName" | "$rid">();
});
});

describe("converts from a concrete to an interface", () => {
Expand Down Expand Up @@ -112,6 +111,11 @@ describe("ConvertProps", () => {
expectTypeOf<ConvertProps<Employee, FooInterface, "$all">>()
.toEqualTypeOf<"$all">();
});

it("handles $all | $rid", () => {
expectTypeOf<ConvertProps<Employee, FooInterface, "$all" | "$rid">>()
.toEqualTypeOf<"$all" | "$rid">();
});
});
});

Expand Down
52 changes: 51 additions & 1 deletion packages/client/src/object/convertWireToOsdkObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe("convertWireToOsdkObjects", () => {
expect(objAsFoo).toBe(obj.$as(FooInterface));
});

it("reconstitutes interfaces properly", async () => {
it("reconstitutes interfaces properly without rid", async () => {
const clientCtx = createMinimalClient(
MockOntology.metadata,
"https://stack.palantir.com",
Expand Down Expand Up @@ -254,6 +254,56 @@ describe("convertWireToOsdkObjects", () => {
`);
});

it("reconstitutes interfaces properly with rid", async () => {
const clientCtx = createMinimalClient(
MockOntology.metadata,
"https://stack.palantir.com",
async () => "myAccessToken",
);

let objectFromWire = {
__apiName: "Employee" as const,
__primaryKey: 0,
__title: "Steve",
__rid: "hiMom",
fooSpt: "Steve",
} satisfies OntologyObjectV2;

const [objAsFoo] = (await convertWireToOsdkObjects(
clientCtx,
[objectFromWire],
FooInterface.apiName,
)) as unknown as Osdk<FooInterface, "$rid" | "$all">[];

expect(objAsFoo).toMatchInlineSnapshot(`
{
"$apiName": "FooInterface",
"$objectType": "Employee",
"$primaryKey": 0,
"$rid": "hiMom",
"$title": "Steve",
"fooSpt": "Steve",
}
`);
expect(objAsFoo.$rid).toEqual("hiMom");

const obj = objAsFoo.$as(Employee);
expect(obj.fullName).toEqual("Steve");

expect(obj).toMatchInlineSnapshot(`
{
"$apiName": "Employee",
"$objectType": "Employee",
"$primaryKey": 0,
"$rid": "hiMom",
"$title": "Steve",
"employeeId": 0,
"fullName": "Steve",
}
`);
expect(obj.$rid).toEqual("hiMom");
});

describe("selection keys", () => {
it("throws when required is missing", async () => {
let object = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ function createInterfaceProxyHandler(
case "$primaryKey":
case "$title":
case "$objectType":
return Reflect.getOwnPropertyDescriptor(underlying, p);
case "$rid":
return underlying[p] != null
? Reflect.getOwnPropertyDescriptor(underlying, p)
: undefined;

case "$apiName":
return {
Expand All @@ -92,10 +95,12 @@ function createInterfaceProxyHandler(
},

ownKeys(target) {
const underlying = target[UnderlyingOsdkObject];
return [
"$apiName",
"$objectType",
"$primaryKey",
...(underlying["$rid"] ? ["$rid"] : []),
"$title",
...Object.keys(newDef.properties),
];
Expand All @@ -113,6 +118,7 @@ function createInterfaceProxyHandler(
case "$primaryKey":
case "$title":
case "$objectType":
case "$rid":
return underlying[p as string];
}

Expand Down