Skip to content

Commit

Permalink
Update ontology as code to use new shapes (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored Jun 27, 2024
1 parent 8b1e578 commit 2789341
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .changeset/ninety-avocados-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
90 changes: 90 additions & 0 deletions packages/client.unstable/src/blockDataIr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 {
InterfaceTypeBlockDataV2,
OntologyBlockDataV2,
SharedPropertyTypeBlockDataV2,
} from "./generated/ontology-metadata/api/blockdata/index.js";
import type { InterfaceLinkType } from "./generated/ontology-metadata/api/InterfaceLinkType.js";
import type { InterfaceType } from "./generated/ontology-metadata/api/InterfaceType.js";
import type { SharedPropertyType } from "./generated/ontology-metadata/api/SharedPropertyType.js";

export type InterfaceTypeApiName = string;
export type ObjectTypeFieldApiName = string;
export type InterfaceLinkTypeApiName = string;

export interface OntologyIrOntologyBlockDataV2 extends
ReplaceKeys<
Omit<
OntologyBlockDataV2,
| "knownIdentifiers"
| "objectTypes"
| "linkTypes"
| "ruleSets"
| "actionTypes"
| "blockOutputCompassLocations"
>,
{
interfaceTypes: Record<string, OntologyIrInterfaceTypeBlockDataV2>;
sharedPropertyTypes: Record<
string,
OntologyIrSharedPropertyTypeBlockDataV2
>;
}
>
{
}

export interface OntologyIrInterfaceType
extends
ReplaceKeys<Omit<InterfaceType, "rid">, {
properties: OntologyIrSharedPropertyType[];
allProperties: OntologyIrSharedPropertyType[];
extendsInterfaces: string[];
allExtendsInterfaces: string[];
links: OntologyIrInterfaceLinkType[];
allLinks: OntologyIrInterfaceLinkType[];
}>
{}

export interface OntologyIrSharedPropertyType
extends Omit<SharedPropertyType, "rid">
{}

export interface OntologyIrInterfaceLinkType
extends Omit<InterfaceLinkType, "rid">
{}

export type OntologyIrInterfaceTypeBlockDataV2 = ReplaceKeys<
InterfaceTypeBlockDataV2,
{
interfaceType: OntologyIrInterfaceType;
}
>;

export interface OntologyIrSharedPropertyTypeBlockDataV2 extends
ReplaceKeys<
SharedPropertyTypeBlockDataV2,
{
sharedPropertyType: OntologyIrSharedPropertyType;
}
>
{}

type ReplaceKeys<T, Z extends { [K in keyof T]?: unknown }> = {
[K in keyof T]: K extends keyof Z ? Z[K] : T[K];
};
8 changes: 8 additions & 0 deletions packages/client.unstable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ export { getBulkLinksPage } from "./generated/object-set-service/api/ObjectSetSe
export { getLinkTypesForObjectTypes } from "./generated/ontology-metadata/api/OntologyMetadataService/getLinkTypesForObjectTypes.js";
export { loadAllOntologies } from "./generated/ontology-metadata/api/OntologyMetadataService/loadAllOntologies.js";
export { loadOntologyEntities } from "./generated/ontology-metadata/api/OntologyMetadataService/loadOntologyEntities.js";

export type {
OntologyIrInterfaceType,
OntologyIrInterfaceTypeBlockDataV2,
OntologyIrOntologyBlockDataV2,
OntologyIrSharedPropertyType,
OntologyIrSharedPropertyTypeBlockDataV2,
} from "./blockDataIr.js";
1 change: 1 addition & 0 deletions packages/maker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@osdk/client.unstable": "workspace:^",
"@types/yargs": "^17.0.32",
"typescript": "^5.5.2",
"vitest": "^1.6.0"
Expand Down
173 changes: 122 additions & 51 deletions packages/maker/src/api/defineOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
* limitations under the License.
*/

import type * as Gateway from "@osdk/gateway/types";
import type { Ontology, SharedPropertyType } from "./types.js";
import type {
OntologyIrInterfaceType,
OntologyIrInterfaceTypeBlockDataV2,
OntologyIrOntologyBlockDataV2,
OntologyIrSharedPropertyType,
OntologyIrSharedPropertyTypeBlockDataV2,
Type,
} from "@osdk/client.unstable";
import type {
Ontology,
PropertyTypeType,
SharedPropertyType,
} from "./types.js";

/** @internal */
export let ontologyDefinition: Ontology;
Expand All @@ -26,30 +37,13 @@ export let namespace: string;
export async function defineOntology(
ns: string,
body: () => void | Promise<void>,
): Promise<
{
sharedPropertyTypes: { [k: string]: Gateway.SharedPropertyType };
interfaceTypes: { [k: string]: Gateway.InterfaceType };
objectTypes: Record<
Gateway.ObjectTypeApiName,
Gateway.ObjectTypeFullMetadata
>;
actionTypes: Record<Gateway.ActionTypeApiName, Gateway.ActionTypeV2>;
queryTypes: Record<Gateway.QueryApiName, Gateway.QueryTypeV2>;
ontology: {
apiName: string;
description: string;
displayName: string;
rid: string;
};
}
> {
): Promise<OntologyIrOntologyBlockDataV2> {
namespace = ns;
ontologyDefinition = {
actionTypes: {},
interfaceTypes: {},
objectTypes: {},
queryTypes: {},
interfaceTypes: {},
sharedPropertyTypes: {},
};

Expand All @@ -67,63 +61,140 @@ export async function defineOntology(
return convertToWireOntology(ontologyDefinition);
}

function convertToWireOntology(ontology: Ontology) {
function convertToWireOntology(
ontology: Ontology,
): OntologyIrOntologyBlockDataV2 {
return {
ontology: {
apiName: "IDK",
description: "IDK",
displayName: "IDK",
rid: "ri.ontology.main.generated-object.foo",
},
...ontology,
sharedPropertyTypes: Object.fromEntries(
Object.entries(
ontology.sharedPropertyTypes,
)
.map<[string, Gateway.SharedPropertyType]>((
.map<[string, OntologyIrSharedPropertyTypeBlockDataV2]>((
[apiName, spt],
) => [apiName, convertSpt(spt)]),
) => [apiName, { sharedPropertyType: convertSpt(spt) }]),
),
interfaceTypes: Object.fromEntries(
Object.entries(
ontology.interfaceTypes,
)
.map<[string, Gateway.InterfaceType]>(
.map<[string, OntologyIrInterfaceTypeBlockDataV2]>(
([apiName, { displayName, description, properties }]) => {
return [apiName, {
rid: "ri.ontology.main.generated-object.foo",
apiName,
displayName: displayName ?? apiName,
description,
extendsInterfaces: [],
links: {},
properties: Object.fromEntries(
Object.entries(properties)
.map<[string, Gateway.SharedPropertyType]>(
([apiName, spt]) => [apiName, convertSpt(spt)],
),
interfaceType: convertInterface(
description,
displayName,
apiName,
properties,
),
}];
},
),
),
blockPermissionInformation: {
actionTypes: {},
linkTypes: {},
objectTypes: {},
},
};
}

function convertInterface(
description: string | undefined,
displayName: string,
apiName: string,
properties: Record<string, SharedPropertyType>,
): OntologyIrInterfaceType {
return {
displayMetadata: {
description,
displayName: displayName ?? apiName,
icon: undefined,
},
apiName,
extendsInterfaces: [],
links: [],
status: {
active: true,
type: "active",
},
allExtendsInterfaces: [],
allLinks: [],
allProperties: [],
properties: Object.values(properties)
.map<OntologyIrSharedPropertyType>((spt) => convertSpt(spt)),
};
}

export function dumpOntologyFullMetadata(): Gateway.OntologyFullMetadata {
export function dumpOntologyFullMetadata(): OntologyIrOntologyBlockDataV2 {
return convertToWireOntology(ontologyDefinition);
}

function convertSpt(
{ type, array, description, apiName, displayName }: SharedPropertyType,
): Gateway.SharedPropertyType {
): OntologyIrSharedPropertyType {
return {
rid: "ri.ontology.main.generated-object.foo",
apiName,
displayName: displayName ?? apiName,
description,
dataType: array
? { type: "array" as const, subType: { type } }
: { type },
displayMetadata: {
displayName: displayName ?? apiName,
visibility: "NORMAL",
description,
},
type: array
? {
type: "array" as const,
array: {
subtype: convertType(type),
},
}
: convertType(type),
aliases: [],
baseFormatter: undefined,
dataConstraints: undefined,
gothamMapping: undefined,
indexedForSearch: true,
provenance: undefined,
typeClasses: [],
valueType: undefined,
};
}

function convertType(
type: PropertyTypeType,
): Type {
switch (type) {
case "marking":
return { type, marking: { markingType: "MANDATORY" } };

case "geopoint":
return { type: "geohash", geohash: {} };

case "decimal":
return { type, [type]: { precision: undefined, scale: undefined } };

case "string":
return {
type,
[type]: {
analyzerOverride: undefined,
enableAsciiFolding: undefined,
isLongText: false,
supportsExactMatching: true,
},
};

default:
// use helper function to distribute `type` properly
return asdf(type);
}
}

/**
* Helper function to avoid duplication
* @param type
* @returns
*/
function asdf<T extends string>(
type: T,
): T extends any ? { type: T } & { [K in T]: {} } : never {
return { type, [type]: {} } as any; // any cast to match conditional return type
}
Loading

0 comments on commit 2789341

Please sign in to comment.