From 27893417f041d3cee9d47f0c77f76d58cc06c9ca Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 27 Jun 2024 17:26:23 -0400 Subject: [PATCH] Update ontology as code to use new shapes (#422) --- .changeset/ninety-avocados-unite.md | 2 + packages/client.unstable/src/blockDataIr.ts | 90 ++++++++ packages/client.unstable/src/index.ts | 8 + packages/maker/package.json | 1 + packages/maker/src/api/defineOntology.ts | 173 +++++++++----- packages/maker/src/api/overall.test.ts | 242 ++++++++++++++------ pnpm-lock.yaml | 3 + scripts/getConjureIr.sh | 2 + 8 files changed, 397 insertions(+), 124 deletions(-) create mode 100644 .changeset/ninety-avocados-unite.md create mode 100644 packages/client.unstable/src/blockDataIr.ts diff --git a/.changeset/ninety-avocados-unite.md b/.changeset/ninety-avocados-unite.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/ninety-avocados-unite.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/client.unstable/src/blockDataIr.ts b/packages/client.unstable/src/blockDataIr.ts new file mode 100644 index 000000000..40a79258d --- /dev/null +++ b/packages/client.unstable/src/blockDataIr.ts @@ -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; + sharedPropertyTypes: Record< + string, + OntologyIrSharedPropertyTypeBlockDataV2 + >; + } + > +{ +} + +export interface OntologyIrInterfaceType + extends + ReplaceKeys, { + properties: OntologyIrSharedPropertyType[]; + allProperties: OntologyIrSharedPropertyType[]; + extendsInterfaces: string[]; + allExtendsInterfaces: string[]; + links: OntologyIrInterfaceLinkType[]; + allLinks: OntologyIrInterfaceLinkType[]; + }> +{} + +export interface OntologyIrSharedPropertyType + extends Omit +{} + +export interface OntologyIrInterfaceLinkType + extends Omit +{} + +export type OntologyIrInterfaceTypeBlockDataV2 = ReplaceKeys< + InterfaceTypeBlockDataV2, + { + interfaceType: OntologyIrInterfaceType; + } +>; + +export interface OntologyIrSharedPropertyTypeBlockDataV2 extends + ReplaceKeys< + SharedPropertyTypeBlockDataV2, + { + sharedPropertyType: OntologyIrSharedPropertyType; + } + > +{} + +type ReplaceKeys = { + [K in keyof T]: K extends keyof Z ? Z[K] : T[K]; +}; diff --git a/packages/client.unstable/src/index.ts b/packages/client.unstable/src/index.ts index 71d461249..6141868ea 100644 --- a/packages/client.unstable/src/index.ts +++ b/packages/client.unstable/src/index.ts @@ -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"; diff --git a/packages/maker/package.json b/packages/maker/package.json index fde614500..7331e8d50 100644 --- a/packages/maker/package.json +++ b/packages/maker/package.json @@ -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" diff --git a/packages/maker/src/api/defineOntology.ts b/packages/maker/src/api/defineOntology.ts index eee59dbd8..c080122cd 100644 --- a/packages/maker/src/api/defineOntology.ts +++ b/packages/maker/src/api/defineOntology.ts @@ -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; @@ -26,30 +37,13 @@ export let namespace: string; export async function defineOntology( ns: string, body: () => void | Promise, -): Promise< - { - sharedPropertyTypes: { [k: string]: Gateway.SharedPropertyType }; - interfaceTypes: { [k: string]: Gateway.InterfaceType }; - objectTypes: Record< - Gateway.ObjectTypeApiName, - Gateway.ObjectTypeFullMetadata - >; - actionTypes: Record; - queryTypes: Record; - ontology: { - apiName: string; - description: string; - displayName: string; - rid: string; - }; - } -> { +): Promise { namespace = ns; ontologyDefinition = { actionTypes: {}, - interfaceTypes: {}, objectTypes: {}, queryTypes: {}, + interfaceTypes: {}, sharedPropertyTypes: {}, }; @@ -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, +): 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((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( + type: T, +): T extends any ? { type: T } & { [K in T]: {} } : never { + return { type, [type]: {} } as any; // any cast to match conditional return type +} diff --git a/packages/maker/src/api/overall.test.ts b/packages/maker/src/api/overall.test.ts index 386088905..22e0dab40 100644 --- a/packages/maker/src/api/overall.test.ts +++ b/packages/maker/src/api/overall.test.ts @@ -59,45 +59,86 @@ describe("Ontology Defining", () => { expect(dumpOntologyFullMetadata()).toMatchInlineSnapshot(` { - "actionTypes": {}, + "blockPermissionInformation": { + "actionTypes": {}, + "linkTypes": {}, + "objectTypes": {}, + }, "interfaceTypes": { "Foo": { - "apiName": "Foo", - "description": "Foo", - "displayName": "Foo", - "extendsInterfaces": [], - "links": {}, - "properties": { - "foo": { - "apiName": "foo", - "dataType": { - "type": "string", + "interfaceType": { + "allExtendsInterfaces": [], + "allLinks": [], + "allProperties": [], + "apiName": "Foo", + "displayMetadata": { + "description": "Foo", + "displayName": "Foo", + "icon": undefined, + }, + "extendsInterfaces": [], + "links": [], + "properties": [ + { + "aliases": [], + "apiName": "foo", + "baseFormatter": undefined, + "dataConstraints": undefined, + "displayMetadata": { + "description": undefined, + "displayName": "foo", + "visibility": "NORMAL", + }, + "gothamMapping": undefined, + "indexedForSearch": true, + "provenance": undefined, + "type": { + "string": { + "analyzerOverride": undefined, + "enableAsciiFolding": undefined, + "isLongText": false, + "supportsExactMatching": true, + }, + "type": "string", + }, + "typeClasses": [], + "valueType": undefined, }, - "description": undefined, - "displayName": "foo", - "rid": "ri.ontology.main.generated-object.foo", + ], + "status": { + "active": true, + "type": "active", }, }, - "rid": "ri.ontology.main.generated-object.foo", }, }, - "objectTypes": {}, - "ontology": { - "apiName": "IDK", - "description": "IDK", - "displayName": "IDK", - "rid": "ri.ontology.main.generated-object.foo", - }, - "queryTypes": {}, "sharedPropertyTypes": { "foo": { - "apiName": "foo", - "dataType": { - "type": "string", + "sharedPropertyType": { + "aliases": [], + "apiName": "foo", + "baseFormatter": undefined, + "dataConstraints": undefined, + "displayMetadata": { + "description": undefined, + "displayName": "foo", + "visibility": "NORMAL", + }, + "gothamMapping": undefined, + "indexedForSearch": true, + "provenance": undefined, + "type": { + "string": { + "analyzerOverride": undefined, + "enableAsciiFolding": undefined, + "isLongText": false, + "supportsExactMatching": true, + }, + "type": "string", + }, + "typeClasses": [], + "valueType": undefined, }, - "description": undefined, - "displayName": "foo", - "rid": "ri.ontology.main.generated-object.foo", }, }, } @@ -111,25 +152,39 @@ describe("Ontology Defining", () => { expect(dumpOntologyFullMetadata()).toMatchInlineSnapshot(` { - "actionTypes": {}, - "interfaceTypes": {}, - "objectTypes": {}, - "ontology": { - "apiName": "IDK", - "description": "IDK", - "displayName": "IDK", - "rid": "ri.ontology.main.generated-object.foo", + "blockPermissionInformation": { + "actionTypes": {}, + "linkTypes": {}, + "objectTypes": {}, }, - "queryTypes": {}, + "interfaceTypes": {}, "sharedPropertyTypes": { "foo": { - "apiName": "foo", - "dataType": { - "type": "string", + "sharedPropertyType": { + "aliases": [], + "apiName": "foo", + "baseFormatter": undefined, + "dataConstraints": undefined, + "displayMetadata": { + "description": undefined, + "displayName": "foo", + "visibility": "NORMAL", + }, + "gothamMapping": undefined, + "indexedForSearch": true, + "provenance": undefined, + "type": { + "string": { + "analyzerOverride": undefined, + "enableAsciiFolding": undefined, + "isLongText": false, + "supportsExactMatching": true, + }, + "type": "string", + }, + "typeClasses": [], + "valueType": undefined, }, - "description": undefined, - "displayName": "foo", - "rid": "ri.ontology.main.generated-object.foo", }, }, } @@ -184,45 +239,86 @@ describe("Ontology Defining", () => { expect(dumpOntologyFullMetadata()).toMatchInlineSnapshot(` { - "actionTypes": {}, + "blockPermissionInformation": { + "actionTypes": {}, + "linkTypes": {}, + "objectTypes": {}, + }, "interfaceTypes": { "FooInterface": { - "apiName": "FooInterface", - "description": "Foo Interface", - "displayName": "Foo Interface", - "extendsInterfaces": [], - "links": {}, - "properties": { - "fooSpt": { - "apiName": "fooSpt", - "dataType": { - "type": "string", + "interfaceType": { + "allExtendsInterfaces": [], + "allLinks": [], + "allProperties": [], + "apiName": "FooInterface", + "displayMetadata": { + "description": "Foo Interface", + "displayName": "Foo Interface", + "icon": undefined, + }, + "extendsInterfaces": [], + "links": [], + "properties": [ + { + "aliases": [], + "apiName": "fooSpt", + "baseFormatter": undefined, + "dataConstraints": undefined, + "displayMetadata": { + "description": undefined, + "displayName": "fooSpt", + "visibility": "NORMAL", + }, + "gothamMapping": undefined, + "indexedForSearch": true, + "provenance": undefined, + "type": { + "string": { + "analyzerOverride": undefined, + "enableAsciiFolding": undefined, + "isLongText": false, + "supportsExactMatching": true, + }, + "type": "string", + }, + "typeClasses": [], + "valueType": undefined, }, - "description": undefined, - "displayName": "fooSpt", - "rid": "ri.ontology.main.generated-object.foo", + ], + "status": { + "active": true, + "type": "active", }, }, - "rid": "ri.ontology.main.generated-object.foo", }, }, - "objectTypes": {}, - "ontology": { - "apiName": "IDK", - "description": "IDK", - "displayName": "IDK", - "rid": "ri.ontology.main.generated-object.foo", - }, - "queryTypes": {}, "sharedPropertyTypes": { "fooSpt": { - "apiName": "fooSpt", - "dataType": { - "type": "string", + "sharedPropertyType": { + "aliases": [], + "apiName": "fooSpt", + "baseFormatter": undefined, + "dataConstraints": undefined, + "displayMetadata": { + "description": undefined, + "displayName": "fooSpt", + "visibility": "NORMAL", + }, + "gothamMapping": undefined, + "indexedForSearch": true, + "provenance": undefined, + "type": { + "string": { + "analyzerOverride": undefined, + "enableAsciiFolding": undefined, + "isLongText": false, + "supportsExactMatching": true, + }, + "type": "string", + }, + "typeClasses": [], + "valueType": undefined, }, - "description": undefined, - "displayName": "fooSpt", - "rid": "ri.ontology.main.generated-object.foo", }, }, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7728d49be..5645698ce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1389,6 +1389,9 @@ importers: specifier: ^17.7.2 version: 17.7.2 devDependencies: + '@osdk/client.unstable': + specifier: workspace:^ + version: link:../client.unstable '@types/yargs': specifier: ^17.0.32 version: 17.0.32 diff --git a/scripts/getConjureIr.sh b/scripts/getConjureIr.sh index a58f477b9..3c9298de3 100755 --- a/scripts/getConjureIr.sh +++ b/scripts/getConjureIr.sh @@ -17,5 +17,7 @@ fi CURRENT_VERSION=$( wget -q -O - "${MAVEN_CONJURE_BASE_PATH}/$(echo "$MAVEN_CONJURE_GROUP_ID" | sed 's/\./\//g')/${MAVEN_CONJURE_ARTIFACT_ID}/maven-metadata.xml" | \ yq -p xml -r '.metadata.versioning.release' ) +echo "getting ${MAVEN_CONJURE_BASE_PATH}/$(echo "$MAVEN_CONJURE_GROUP_ID" | sed 's/\./\//g')/${MAVEN_CONJURE_ARTIFACT_ID}/${CURRENT_VERSION}/${MAVEN_CONJURE_ARTIFACT_ID}-${CURRENT_VERSION}.conjure.json" + wget -q -O - "${MAVEN_CONJURE_BASE_PATH}/$(echo "$MAVEN_CONJURE_GROUP_ID" | sed 's/\./\//g')/${MAVEN_CONJURE_ARTIFACT_ID}/${CURRENT_VERSION}/${MAVEN_CONJURE_ARTIFACT_ID}-${CURRENT_VERSION}.conjure.json" > "${SCRIPT_DIR}/../tmp/${MAVEN_CONJURE_ARTIFACT_ID}.conjure.json"