Skip to content

Commit

Permalink
sdk: calculate required/optional fields from schema (koush#1321)
Browse files Browse the repository at this point in the history
* Add tsconfig for sdk/types/src

* Get property isOptional from schema

Use typedoc types
  • Loading branch information
longzheng authored and rustyd0g committed Feb 17, 2024
1 parent 8caea43 commit e765beb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
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"
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

0 comments on commit e765beb

Please sign in to comment.