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

SDK types build: calculate required/optional fields from schema #1321

Merged
merged 2 commits into from
Feb 15, 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
35 changes: 15 additions & 20 deletions sdk/types/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import stringifyObject from 'stringify-object';
import { ScryptedInterface, ScryptedInterfaceDescriptor } from "./types.input";
import path from 'path';
import fs from "fs";
import packageJson from "../package.json"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can import package.json directly instead of using JSON.parse(fs.readFileSync(

import { DeclarationReflection, ProjectReflection, SomeType } from 'typedoc';

const schema = JSON.parse(fs.readFileSync(path.join(__dirname, '../gen/schema.json')).toString());
const typesVersion = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json')).toString()).version;
const schema = JSON.parse(fs.readFileSync(path.join(__dirname, '../gen/schema.json')).toString()) as ProjectReflection;
const typesVersion = packageJson.version;
const ScryptedInterfaceDescriptors: { [scryptedInterface: string]: ScryptedInterfaceDescriptor } = {};

const allProperties: { [property: string]: any } = {};
const allProperties: { [property: string]: DeclarationReflection } = {};

function toTypescriptType(type: any): string {
if (type.type === 'literal')
Expand All @@ -20,39 +22,31 @@ function toTypescriptType(type: any): string {
}

for (const name of Object.values(ScryptedInterface)) {
const td = schema.children.find((child: any) => child.name === name);
const td = schema.children.find((child) => child.name === name);
const children = td.children || [];
const properties = children.filter((child: any) => child.kindString === 'Property').map((child: any) => child.name);
const methods = children.filter((child: any) => child.kindString === 'Method').map((child: any) => child.name);
const properties = children.filter((child) => child.kindString === 'Property').map((child) => child.name);
const methods = children.filter((child) => child.kindString === 'Method').map((child) => child.name);
ScryptedInterfaceDescriptors[name] = {
name,
methods,
properties,
};

for (const p of children.filter((child: any) => child.kindString === 'Property')) {
allProperties[p.name] = p.type;
for (const p of children.filter((child) => child.kindString === 'Property')) {
allProperties[p.name] = p;
}
}

const properties = Object.values(ScryptedInterfaceDescriptors).map(d => d.properties).flat();
const methods = Object.values(ScryptedInterfaceDescriptors).map(d => d.methods).flat();

const requiredProperties = [
'id',
'interfaces',
'providedInterfaces',
'pluginId',
'mixins',
];

const deviceStateContents = `
export interface DeviceState {
${Object.entries(allProperties).map(([property, type]) => ` ${property}${!requiredProperties.includes(property) ? '?' : ''}: ${toTypescriptType(type)}`).join('\n')};
${Object.entries(allProperties).map(([property, {type, flags}]) => ` ${property}${flags.isOptional ? '?' : ''}: ${toTypescriptType(type)}`).join('\n')};
}

export class DeviceBase implements DeviceState {
${Object.entries(allProperties).map(([property, type]) => ` ${property}${!requiredProperties.includes(property) ? '?' : ''}: ${toTypescriptType(type)}`).join('\n')};
${Object.entries(allProperties).map(([property, {type, flags}]) => ` ${property}${flags.isOptional ? '?' : ''}: ${toTypescriptType(type)}`).join('\n')};
}
`;

Expand Down Expand Up @@ -237,7 +231,8 @@ class ${e.name}(str, Enum):
${toDocstring(e)}
`
for (const val of e.children) {
pythonEnums += ` ${val.name} = "${val.type.value}"
if ('type' in val && 'value' in val.type)
pythonEnums += ` ${val.name} = "${val.type.value}"
`;
}
}
Expand Down Expand Up @@ -268,7 +263,7 @@ class DeviceState:
pass

`
for (const [val, type] of Object.entries(allProperties)) {
for (const [val, {type}] of Object.entries(allProperties)) {
if (val === 'nativeId')
continue;
python += `
Expand Down
10 changes: 10 additions & 0 deletions sdk/types/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": true
}
}
1 change: 1 addition & 0 deletions sdk/types/tsconfig.typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ESNext",
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"sourceMap": true,
"declaration": true,
},
Expand Down