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

RDP's in TS OSDK #1035

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
246f893
Runtime Derived Property Syntax
nihalbhatnagar Dec 4, 2024
50ca838
Working derive clause
nihalbhatnagar Dec 4, 2024
4e17a4e
Update to use map
nihalbhatnagar Dec 4, 2024
01f5935
Update aggregation keys
nihalbhatnagar Dec 5, 2024
ebe19e4
Update
nihalbhatnagar Dec 5, 2024
4ea27dc
Update PR
nihalbhatnagar Dec 5, 2024
d2d0a0d
Initial test suite
nihalbhatnagar Dec 6, 2024
efbed3d
Working version
nihalbhatnagar Dec 9, 2024
40ee625
Cleanup
nihalbhatnagar Dec 9, 2024
3e535da
Add test case
nihalbhatnagar Dec 9, 2024
20835ea
remove use of crypto
nihalbhatnagar Dec 9, 2024
76a3ee1
Changeset
nihalbhatnagar Dec 9, 2024
73c23fe
E2E testing
nihalbhatnagar Dec 10, 2024
d605787
Osdk.Instance typing update
nihalbhatnagar Dec 10, 2024
214dad7
Rename and cleanup
nihalbhatnagar Dec 10, 2024
73fbffa
Fix UUID
nihalbhatnagar Dec 10, 2024
9070c29
Quick fixes
nihalbhatnagar Dec 13, 2024
ac7f03f
Update aggregations syntax
nihalbhatnagar Dec 16, 2024
2777334
Merge branch 'main' into nihalb/rdp-v1
nihalbhatnagar Dec 16, 2024
a8546ca
Update
nihalbhatnagar Dec 18, 2024
0cd49f5
Merge branch 'main' into nihalb/rdp-v1
nihalbhatnagar Jan 8, 2025
0a89f9c
Update names
nihalbhatnagar Jan 8, 2025
a2f331d
Updated types to produce a shorter definition and also allows us to c…
nihalbhatnagar Jan 15, 2025
bec53ca
Updated tests
nihalbhatnagar Jan 15, 2025
d8fa066
Update tests
nihalbhatnagar Jan 15, 2025
ecb0522
Merge branch 'main' into nihalb/rdp-v1
nihalbhatnagar Jan 15, 2025
abc0b2d
Update to no longer use definitionId
nihalbhatnagar Jan 16, 2025
831f890
Additional type test
nihalbhatnagar Jan 16, 2025
a270994
Rework RDP types (#1113)
ericanderson Jan 27, 2025
7a8847f
Reorganize files and rename Rdp to DerivedProperty
nihalbhatnagar Jan 28, 2025
2659b3b
Osdk.Instance<> for RDP's
nihalbhatnagar Jan 29, 2025
1167fae
Refactor tests
nihalbhatnagar Jan 29, 2025
330dac1
Merge branch 'main' into nihalb/rdp-v1
nihalbhatnagar Jan 29, 2025
eb99bab
Add tests back
nihalbhatnagar Jan 29, 2025
c5686e0
Remove comment
nihalbhatnagar Jan 30, 2025
624b662
Simplify types
ericanderson Jan 30, 2025
2812538
slim the delta more
ericanderson Jan 30, 2025
c2c675f
Update names of builders
nihalbhatnagar Jan 30, 2025
95829f8
Use types instead of interfaces to support better hover
nihalbhatnagar Feb 3, 2025
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
7 changes: 7 additions & 0 deletions .changeset/forty-eggs-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@osdk/shared.test": minor
"@osdk/client": minor
"@osdk/api": minor
---

Adds support for runtime derived properties
32 changes: 30 additions & 2 deletions etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ export interface ObjectSet<Q extends ObjectOrInterfaceDefinition = any, _UNUSED
};
readonly subtract: (...objectSets: ReadonlyArray<CompileTimeMetadata<Q>["objectSet"]>) => this;
readonly union: (...objectSets: ReadonlyArray<CompileTimeMetadata<Q>["objectSet"]>) => this;
// (undocumented)
readonly withProperties: <D extends WithPropertiesClause<Q>>(clause: D) => ObjectSetWithProperties<Q, {
[K in keyof D]: D[K] extends (baseObjectSet: any) => WithPropertyDefinition<infer P> ? PropertyDef<P["type"], "nullable", P["multiplicity"] extends true ? "array" : "single"> : never;
}>;
}

// @public (undocumented)
Expand Down Expand Up @@ -621,6 +625,11 @@ export interface ObjectSetQueryDataType<T_Target extends ObjectTypeDefinition =
objectSet: string;
}

// Warning: (ae-forgotten-export) The symbol "WithPropertiesObjectDefinition" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type ObjectSetWithProperties<Q extends ObjectOrInterfaceDefinition, D extends Record<string, PropertyDef<any, any, any>>> = ObjectSet<WithPropertiesObjectDefinition<Q, D>>;

// @public (undocumented)
export interface ObjectTypeDefinition {
// (undocumented)
Expand Down Expand Up @@ -956,8 +965,8 @@ export type TwoDimensionalQueryAggregationDefinition = AggregationKeyDataType<"d
// Warning: (ae-forgotten-export) The symbol "GetWirePropertyValueFromClient" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type ValidAggregationKeys<Q extends ObjectOrInterfaceDefinition> = keyof ({
[KK in AggregatableKeys<Q> as `${KK & string}:${AGG_FOR_TYPE<GetWirePropertyValueFromClient<CompileTimeMetadata<Q>["properties"][KK]["type"]>>}`]?: any;
export type ValidAggregationKeys<Q extends ObjectOrInterfaceDefinition, R extends "aggregate" | "withPropertiesAggregate" = "aggregate"> = keyof ({
[KK in AggregatableKeys<Q> as `${KK & string}:${AGG_FOR_TYPE<GetWirePropertyValueFromClient<CompileTimeMetadata<Q>["properties"][KK]["type"]>, R extends "aggregate" ? true : false>}`]?: any;
} & {
$count?: any;
});
Expand All @@ -983,10 +992,29 @@ export type WhereClause<T extends ObjectOrInterfaceDefinition> = OrWhereClause<T
// @public (undocumented)
export type WirePropertyTypes = SimpleWirePropertyTypes | Record<string, SimpleWirePropertyTypes>;

// @public (undocumented)
export type WithPropertiesClause<Q extends ObjectOrInterfaceDefinition> = {
[key: string]: (baseObjectSet: BaseWithPropertyObjectSet<Q>) => WithPropertyDefinition<ObjectMetadata.Property>;
};

// @public (undocumented)
export type WithPropertyDefinition<T extends ObjectMetadata.Property> = {
definitionId: string;
type: T;
};

// Warning: (ae-forgotten-export) The symbol "AggregatableWithPropertyObjectSet" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "SingleLinkWithPropertyObjectSet" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export interface WithPropertyObjectSet<Q extends ObjectOrInterfaceDefinition> extends BaseWithPropertyObjectSet<Q>, AggregatableWithPropertyObjectSet<Q>, SingleLinkWithPropertyObjectSet<Q> {
}

// Warnings were encountered during analysis:
//
// src/aggregate/AggregateOpts.ts:26:3 - (ae-forgotten-export) The symbol "UnorderedAggregationClause" needs to be exported by the entry point index.d.ts
// src/aggregate/AggregateOpts.ts:26:3 - (ae-forgotten-export) The symbol "OrderedAggregationClause" needs to be exported by the entry point index.d.ts
// src/derivedProperties/WithPropertiesClause.ts:33:3 - (ae-forgotten-export) The symbol "BaseWithPropertyObjectSet" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
14 changes: 11 additions & 3 deletions packages/api/src/aggregate/AggregatableKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

import type {
NumericWithPropAggregateOption,
StringWithPropAggregateOption,
} from "../derivedProperties/WithPropertyObjectSet.js";
import type {
GetWirePropertyValueFromClient,
} from "../mapping/PropertyValueMapping.js";
Expand All @@ -32,19 +36,23 @@ export type NumericAggregateOption =
| "approximateDistinct"
| "exactDistinct";

type AGG_FOR_TYPE<T> = number extends T ? NumericAggregateOption
: string extends T ? StringAggregateOption
type AGG_FOR_TYPE<T, U extends boolean> = number extends T
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename U so its clear what that boolean generic is for

? U extends true ? NumericAggregateOption : NumericWithPropAggregateOption
: string extends T
? U extends true ? StringAggregateOption : StringWithPropAggregateOption
: never;

export type ValidAggregationKeys<
Q extends ObjectOrInterfaceDefinition,
R extends "aggregate" | "withPropertiesAggregate" = "aggregate",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssanjay1 Is this what you had in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that works

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though maybe keeping parity with the other names it should be aggregate | derivedPropertiesAggregate or something along those lines, basically including the word derive

> = keyof (
& {
[
KK in AggregatableKeys<Q> as `${KK & string}:${AGG_FOR_TYPE<
GetWirePropertyValueFromClient<
CompileTimeMetadata<Q>["properties"][KK]["type"]
>
>,
R extends "aggregate" ? true : false
>}`
]?: any;
}
Expand Down
36 changes: 36 additions & 0 deletions packages/api/src/derivedProperties/WithPropertiesClause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type {
ObjectOrInterfaceDefinition,
} from "../ontology/ObjectOrInterface.js";
import type { ObjectMetadata } from "../ontology/ObjectTypeDefinition.js";
import type { BaseWithPropertyObjectSet } from "./WithPropertyObjectSet.js";

export type WithPropertyDefinition<
T extends ObjectMetadata.Property,
> = {
definitionId: string;
type: T;
};

export type WithPropertiesClause<
Q extends ObjectOrInterfaceDefinition,
> = {
[key: string]: (
baseObjectSet: BaseWithPropertyObjectSet<Q>,
) => WithPropertyDefinition<ObjectMetadata.Property>;
};
130 changes: 130 additions & 0 deletions packages/api/src/derivedProperties/WithPropertyObjectSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { ValidAggregationKeys } from "../aggregate/AggregatableKeys.js";
import type { WhereClause } from "../aggregate/WhereClause.js";
import type {
ObjectOrInterfaceDefinition,
PropertyKeys,
} from "../ontology/ObjectOrInterface.js";
import type {
CompileTimeMetadata,
PropertyDef,
} from "../ontology/ObjectTypeDefinition.js";
import type { LinkedType, LinkNames } from "../util/LinkUtils.js";
import type { WithPropertyDefinition } from "./WithPropertiesClause.js";

Copy link
Contributor Author

@nihalbhatnagar nihalbhatnagar Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use inheritance here to make sure we enforce what methods are available and to reduce duplication

export interface WithPropertyObjectSet<Q extends ObjectOrInterfaceDefinition>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets be consistent with naming, see some withProperty and derive here.

extends
BaseWithPropertyObjectSet<Q>,
AggregatableWithPropertyObjectSet<Q>,
SingleLinkWithPropertyObjectSet<Q>
{}

export interface BaseWithPropertyObjectSet<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object set only has filter and pivot as aggregations and selectProperty is not available on the base object set.

Q extends ObjectOrInterfaceDefinition,
> extends FilterableWithPropertyObjectSet<Q> {
readonly pivotTo: <L extends LinkNames<Q>>(
type: L,
) => NonNullable<CompileTimeMetadata<Q>["links"][L]["multiplicity"]> extends
false ? SingleLinkWithPropertyObjectSet<LinkedType<Q, L>>
: ManyLinkWithPropertyObjectSet<LinkedType<Q, L>>;
}

interface FilterableWithPropertyObjectSet<
Q extends ObjectOrInterfaceDefinition,
> {
readonly where: (
clause: WhereClause<Q>,
) => this;
}
export type CollectWithPropAggregations = "collectSet" | "collectList";

export type BaseWithPropAggregations =
| "approximateDistinct"
| "exactDistinct"
| "approximatePercentile";

export type StringWithPropAggregateOption =
| BaseWithPropAggregations
| CollectWithPropAggregations;

export type NumericWithPropAggregateOption =
| "min"
| "max"
| "sum"
| "avg"
| BaseWithPropAggregations
| CollectWithPropAggregations;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am thinking about whats ideal we need to add some type tests. I'm okay if its in a follow up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will follow this up, this PR is a little bloated with tests

interface AggregatableWithPropertyObjectSet<
Q extends ObjectOrInterfaceDefinition,
> {
readonly aggregate: <
V extends ValidAggregationKeys<
Q,
"withPropertiesAggregate"
>,
>(
aggregationSpecifier: V,
opts?: V extends `${any}:${infer P}`
? P extends CollectWithPropAggregations ? { limit: number }
: P extends "approximatePercentile" ? { percentile: number }
: never
: never,
) => WithPropertyDefinition<
V extends `${infer N}:${infer P}`
? P extends CollectWithPropAggregations ? PropertyDef<
CompileTimeMetadata<Q>["properties"][N]["type"],
"nullable",
"array"
>
: P extends "approximateDistinct" | "exactDistinct" | "$count"
? PropertyDef<"integer">
: PropertyDef<"double">
: V extends "$count" ? PropertyDef<"integer">
: never
>;
}

interface SingleLinkWithPropertyObjectSet<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseObjectSet extends SingleLinkWithPropertyObjectSet. This makes the selectProperty method available only for chains of single links, which correlates with the backend. The pivotTo operation in BaseObjectSet enforces that if we pivot to a many link, we from then on use ManyLinkWithPropertyObjectSet which does not have selectProperty available and only returns further ManyLink object sets.

Q extends ObjectOrInterfaceDefinition,
> extends
AggregatableWithPropertyObjectSet<Q>,
FilterableWithPropertyObjectSet<Q>,
BaseWithPropertyObjectSet<Q>
{
readonly selectProperty: <R extends PropertyKeys<Q>>(
propertyName: R,
) => WithPropertyDefinition<CompileTimeMetadata<Q>["properties"][R]>;
}

/*
* The ManyLinkWithPropertyObjectSet overrides the pivotTo operation because once we traverse a single link,
* we cannot use the "selectProperty" operation again for the entire chain. The parent pivotTo class will create
* this object set once the user pivots to a many link/
*/

interface ManyLinkWithPropertyObjectSet<
Q extends ObjectOrInterfaceDefinition,
> extends
AggregatableWithPropertyObjectSet<Q>,
FilterableWithPropertyObjectSet<Q>
{
readonly pivotTo: <L extends LinkNames<Q>>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just clarifying, the reason we need to duplicate pivotTo here is because if there's a many link anywhere in the clause, you can't select anymore? Even if you go from a many to a single link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's correct, which makes sense because for this to work you have to basically be navigating to exactly one object

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably document that here too.

type: L,
) => ManyLinkWithPropertyObjectSet<LinkedType<Q, L>>;
}
12 changes: 11 additions & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export type {
OsdkObjectLinksObject,
SingleLinkAccessor,
} from "./definitions/LinkDefinitions.js";
export type {
WithPropertiesClause,
WithPropertyDefinition,
} from "./derivedProperties/WithPropertiesClause.js";
export type {
WithPropertyObjectSet,
} from "./derivedProperties/WithPropertyObjectSet.js";
export { DurationMapping } from "./groupby/GroupByClause.js";
export type {
AllGroupByValues,
Expand Down Expand Up @@ -73,7 +80,10 @@ export type {
export { isOk } from "./object/Result.js";
export type { Result } from "./object/Result.js";
export type { BaseObjectSet } from "./objectSet/BaseObjectSet.js";
export type { ObjectSet } from "./objectSet/ObjectSet.js";
export type {
ObjectSet,
ObjectSetWithProperties,
} from "./objectSet/ObjectSet.js";
export type { ObjectSetListener } from "./objectSet/ObjectSetListener.js";
export type { ObjectSetListenerOptions } from "./objectSet/ObjectSetListener.js";
export type {
Expand Down
48 changes: 48 additions & 0 deletions packages/api/src/objectSet/ObjectSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import type { AggregateOpts } from "../aggregate/AggregateOpts.js";
import type { AggregateOptsThatErrorsAndDisallowsOrderingWithMultipleGroupBy } from "../aggregate/AggregateOptsThatErrors.js";
import type { AggregationsResults } from "../aggregate/AggregationsResults.js";
import type { WhereClause } from "../aggregate/WhereClause.js";
import type {
WithPropertiesClause,
WithPropertyDefinition,
} from "../derivedProperties/WithPropertiesClause.js";
import type { GetWirePropertyValueFromClient } from "../mapping/PropertyValueMapping.js";
import type {
AsyncIterArgs,
Augments,
Expand All @@ -34,7 +39,9 @@ import type {
} from "../ontology/ObjectOrInterface.js";
import type {
CompileTimeMetadata,
ObjectMetadata,
ObjectTypeDefinition,
PropertyDef,
} from "../ontology/ObjectTypeDefinition.js";
import type { PrimaryKeyType } from "../OsdkBase.js";
import type { ExtractOptions, Osdk } from "../OsdkObjectFrom.js";
Expand Down Expand Up @@ -254,4 +261,45 @@ export interface ObjectSet<
listener: ObjectSetListener<Q, P>,
opts?: ObjectSetListenerOptions<Q, P>,
) => { unsubscribe: () => void };

readonly withProperties: <
D extends WithPropertiesClause<Q>,
>(
clause: D,
) => ObjectSetWithProperties<
Q,
{
[K in keyof D]: D[K] extends
(baseObjectSet: any) => WithPropertyDefinition<infer P> ? PropertyDef<
P["type"],
Copy link
Contributor Author

@nihalbhatnagar nihalbhatnagar Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user facing type expects a PropertyDef instead of an ObjectMetadata.Property. I chose to localize it here instead of 2 places when extracting the type from the object type definition

"nullable",
P["multiplicity"] extends true ? "array" : "single"
>
: never;
}
>;
}

export type ObjectSetWithProperties<
Copy link
Contributor Author

@nihalbhatnagar nihalbhatnagar Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name needs to be discussed. It is very similar to the WithPropertiesObjectSet type that we already export. I like this name for this type, so I think we should likely rename the WithPropertiesObjectSet to PropertyDefinitionObjectSet. Maybe this name needs to describe that you can define the type of your object set more? Thoughts?

Q extends ObjectOrInterfaceDefinition,
D extends Record<string, PropertyDef<any, any, any>>,
> = ObjectSet<WithPropertiesObjectDefinition<Q, D>>;

type WithPropertiesObjectDefinition<
K extends ObjectOrInterfaceDefinition,
D extends Record<string, ObjectMetadata.Property>,
> = {
__DefinitionMetadata: {
properties: {
nihalbhatnagar marked this conversation as resolved.
Show resolved Hide resolved
[T in keyof D]: D[T];
};
props: {
[T in keyof D]: D[T] extends PropertyDef<infer A, any, any> ?
| GetWirePropertyValueFromClient<
A
>
| undefined
: never;
};
};
} & K;
Loading
Loading