Skip to content

Multiple type building improvements/fixes #225

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

Merged
merged 5 commits into from
Jun 12, 2021
Merged
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
71 changes: 50 additions & 21 deletions .travis/build-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as got from 'got';
import * as path from 'path';
import * as prettier from 'prettier';
import * as got from 'got';
import deburr = require('lodash.deburr');

interface RawType {
Expand Down Expand Up @@ -50,14 +50,19 @@ interface ObjectType {
}

interface OutputType {
type: 'object';
type: 'ObsWebSocket.Output';
properties: Tree;
optional: boolean;
}

interface OBSStatsType {
type: 'ObsWebSocket.OBSStats';
optional: boolean;
}

interface ArrayType {
type: 'array';
items: PrimitiveType | ObjectType | OutputType | SceneType | SceneItemType | SceneItemTransformType;
items: PrimitiveType | ObjectType | OutputType | SceneType | SceneItemType | SceneItemTransformType | ScenesCollectionType;
optional: boolean;
}

Expand All @@ -76,8 +81,8 @@ interface SceneItemTransformType {
optional: boolean;
}

interface OBSStatsType {
type: 'ObsWebSocket.OBSStats';
interface ScenesCollectionType {
type: 'ObsWebSocket.ScenesCollection';
optional: boolean;
}

Expand Down Expand Up @@ -133,7 +138,7 @@ function parseApi(raw: RawComments): void {
if (request.params) {
const foo = unflattenAndResolveTypes(request.params);
argsString += '{';
argsString += stringifyTypes(foo, {terminator: ',', finalTerminator: false});
argsString += stringifyTypes(foo, {terminator: ',', finalTerminator: false, name: request.name});
argsString += '}';
} else {
argsString += 'void';
Expand All @@ -142,7 +147,7 @@ function parseApi(raw: RawComments): void {
let returnTypeString = 'void';
if (request.returns) {
const foo = unflattenAndResolveTypes(request.returns);
returnTypeString = `{messageId: string;status: "ok";${stringifyTypes(foo, {terminator: ';', finalTerminator: false})}}`;
returnTypeString = `{messageId: string;status: "ok";${stringifyTypes(foo, {terminator: ';', finalTerminator: false, name: request.name})}}`;
}
responseString += `${returnTypeString};`;

Expand Down Expand Up @@ -200,6 +205,13 @@ declare module 'obs-websocket-js' {
"ConnectionClosed": void;
"AuthenticationSuccess": void;
"AuthenticationFailure": void;
"error": {
error: any;
message: string;
type: string;
// This would require importing all of the WebSocket types so leaving out for now.
// target: WebSocket;
};
${eventOverloads.join('\n\n ')}
}

Expand Down Expand Up @@ -309,11 +321,14 @@ function unflattenAndResolveTypes(inputItems: RawType[]): Tree {

const firstIntermediate = (currentNode as any)[nodeName];
if (firstIntermediate.type === 'array') {
firstIntermediate.items = {
type: 'object',
properties: {},
optional: false
};
// Not sure if needed at all, but was here before and causing issues, so added a check.
if (!firstIntermediate.items.properties) {
firstIntermediate.items = {
type: 'object',
properties: {},
optional: true // Matches the "array<object>" case in "resolveType".
};
}
currentNode = firstIntermediate.items.properties;
} else {
currentNode = firstIntermediate.properties;
Expand Down Expand Up @@ -390,7 +405,7 @@ function resolveType(inType: string): AnyType {
return {
type: 'array',
items: {
type: 'object',
type: 'ObsWebSocket.Output',
properties: {},
optional: true
},
Expand Down Expand Up @@ -423,6 +438,15 @@ function resolveType(inType: string): AnyType {
},
optional: isOptional
};
case 'array<scenescollection>':
return {
type: 'array',
items: {
type: 'ObsWebSocket.ScenesCollection',
optional: true
},
optional: isOptional
};
case 'sceneitemtransform':
return {
type: 'ObsWebSocket.SceneItemTransform',
Expand All @@ -433,25 +457,25 @@ function resolveType(inType: string): AnyType {
type: 'ObsWebSocket.OBSStats',
optional: isOptional
};
case 'output':
return {
type: 'ObsWebSocket.Output',
properties: {},
optional: isOptional
};
case 'string | object':
case 'object':
return {
type: 'object',
properties: {},
optional: isOptional
};
case 'output':
return {
type: 'object',
properties: {},
optional: isOptional
};
default:
throw new Error(`Unknown type: ${inType}`);
}
}

function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = true, includePrefix = true} = {}): string {
function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = true, includePrefix = true, name = ''} = {}): string {
let returnString = '';
Object.entries(inputTypes).forEach(([key, typeDef]) => {
if (includePrefix) {
Expand All @@ -466,7 +490,12 @@ function stringifyTypes(inputTypes: Tree, {terminator = ';', finalTerminator = t
if (typeDef.items) {
if (typeDef.items.type === 'object') {
if (Object.keys(typeDef.items.properties).length > 0) {
returnString += `${stringifyTypes(typeDef.items.properties, {includePrefix: false, terminator: ''})}[]`;
returnString += `{${stringifyTypes(typeDef.items.properties, {name})}`;
// Allows other arbitrary properties inside of "ExecuteBatch".
if (name === 'ExecuteBatch') {
returnString += '[k: string]: any;';
}
returnString += '}[]';
} else {
returnString += 'Array<{[k: string]: any}>';
}
Expand Down