Skip to content

Commit cb39fcb

Browse files
authored
component.PortType is now listed as an enum in external_samples_data.json. (#152)
Updated getPortTypeForArgument in mrc_components.ts.
1 parent 7cb5198 commit cb39fcb

File tree

3 files changed

+37
-58
lines changed

3 files changed

+37
-58
lines changed

src/blocks/mrc_component.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { Order } from 'blockly/python';
2525
import { MRC_STYLE_COMPONENTS } from '../themes/styles'
2626
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
28-
import { getAllowedTypesForSetCheck, getClassData, getSubclassNames } from './utils/python';
28+
import { getAllowedTypesForSetCheck, getClassData, getModuleData, getSubclassNames } from './utils/python';
2929
import * as ToolboxItems from '../toolbox/items';
3030
import * as CommonStorage from '../storage/common_storage';
3131
import { createPortShadow } from './mrc_port';
@@ -231,18 +231,18 @@ function createComponentBlock(
231231
}
232232

233233
function getPortTypeForArgument(argName: string): string | null {
234-
// TODO(lizlooney): Currently the JSON for component.PortType is ClassData
235-
// instead of EnumData. When it is fixed to be EnumData, this code will need
236-
// to be updated.
237-
238234
const argNameLower = argName.toLowerCase();
239-
const classData = getClassData('component.PortType');
240-
if (classData) {
241-
for (const varData of classData.classVariables) {
242-
if (argNameLower === varData.name.toLowerCase()) {
243-
return varData.name;
235+
const moduleData = getModuleData('component');
236+
for (const enumData of moduleData.enums) {
237+
if (enumData.enumClassName === 'component.PortType') {
238+
for (const value of enumData.enumValues) {
239+
if (argNameLower === value.toLowerCase()) {
240+
return value;
244241
}
245242
}
243+
break;
244+
}
246245
}
246+
247247
return null;
248248
}

src/blocks/utils/generated/external_samples_data.json

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -476,53 +476,6 @@
476476
"moduleName": "component",
477477
"staticMethods": []
478478
},
479-
{
480-
"className": "component.PortType",
481-
"classVariables": [
482-
{
483-
"name": "CAN_PORT",
484-
"tooltip": "",
485-
"type": "component.PortType",
486-
"writable": false
487-
},
488-
{
489-
"name": "I2C_PORT",
490-
"tooltip": "",
491-
"type": "component.PortType",
492-
"writable": false
493-
},
494-
{
495-
"name": "SERVO_PORT",
496-
"tooltip": "",
497-
"type": "component.PortType",
498-
"writable": false
499-
},
500-
{
501-
"name": "SMART_IO_PORT",
502-
"tooltip": "",
503-
"type": "component.PortType",
504-
"writable": false
505-
},
506-
{
507-
"name": "SMART_MOTOR_PORT",
508-
"tooltip": "",
509-
"type": "component.PortType",
510-
"writable": false
511-
},
512-
{
513-
"name": "USB_PORT",
514-
"tooltip": "",
515-
"type": "component.PortType",
516-
"writable": false
517-
}
518-
],
519-
"constructors": [],
520-
"enums": [],
521-
"instanceMethods": [],
522-
"instanceVariables": [],
523-
"moduleName": "component",
524-
"staticMethods": []
525-
},
526479
{
527480
"className": "rev_touch_sensor.RevTouchSensor",
528481
"classVariables": [],
@@ -1846,7 +1799,21 @@
18461799
"moduleVariables": []
18471800
},
18481801
{
1849-
"enums": [],
1802+
"enums": [
1803+
{
1804+
"enumClassName": "component.PortType",
1805+
"enumValues": [
1806+
"CAN_PORT",
1807+
"I2C_PORT",
1808+
"SERVO_PORT",
1809+
"SMART_IO_PORT",
1810+
"SMART_MOTOR_PORT",
1811+
"USB_PORT"
1812+
],
1813+
"moduleName": "component",
1814+
"tooltip": null
1815+
}
1816+
],
18501817
"functions": [],
18511818
"moduleName": "component",
18521819
"moduleVariables": []

src/blocks/utils/python.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ export function getClassData(className: string): ClassData | null {
128128
return null;
129129
}
130130

131+
// Returns the ModuleData for the given module name.
132+
export function getModuleData(moduleName: string): ModuleData | null {
133+
for (const pythonData of allPythonData) {
134+
for (const moduleData of pythonData.modules) {
135+
if (moduleData.moduleName === moduleName) {
136+
return moduleData;
137+
}
138+
}
139+
}
140+
return null;
141+
}
142+
131143
// If the given type is a type alias, returns the value of the type alias.
132144
// For example, if type is 'wpimath.units.nanoseconds', this function will return 'float'
133145
export function getAlias(type: string): string | null {

0 commit comments

Comments
 (0)