({
variablePath,
variablesContainer,
}: {|
variablePath: string,
variablesContainer: gd.VariablesContainer,
|})
| 333 | }; |
| 334 | |
| 335 | export const getVariableAtPath = ({ |
| 336 | variablePath, |
| 337 | variablesContainer, |
| 338 | }: {| |
| 339 | variablePath: string, |
| 340 | variablesContainer: gd.VariablesContainer, |
| 341 | |}): gdVariable | null => { |
| 342 | const pathSegments = parseVariablePath(variablePath); |
| 343 | |
| 344 | if (pathSegments.length === 0) { |
| 345 | throw new Error('Invalid variable path'); |
| 346 | } |
| 347 | |
| 348 | const firstSegment = pathSegments[0]; |
| 349 | if (firstSegment.type !== 'property') { |
| 350 | throw new Error('Variable path must start with a property name'); |
| 351 | } |
| 352 | |
| 353 | if (!variablesContainer.has(firstSegment.value)) { |
| 354 | return null; |
| 355 | } |
| 356 | let variable = variablesContainer.get(firstSegment.value); |
| 357 | |
| 358 | for (let i = 1; i < pathSegments.length; i++) { |
| 359 | const segment = pathSegments[i]; |
| 360 | if (segment.type === 'property') { |
| 361 | if ( |
| 362 | variable.getType() !== gd.Variable.Structure || |
| 363 | !variable.hasChild(segment.value) |
| 364 | ) { |
| 365 | return null; |
| 366 | } |
| 367 | variable = variable.getChild(segment.value); |
| 368 | } else { |
| 369 | const index = parseInt(segment.value, 10); |
| 370 | if ( |
| 371 | variable.getType() !== gd.Variable.Array || |
| 372 | index >= variable.getChildrenCount() |
| 373 | ) { |
| 374 | return null; |
| 375 | } |
| 376 | variable = variable.getAtIndex(index); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return variable; |
| 381 | }; |
no test coverage detected