Skip to content

Commit 028edc6

Browse files
committed
Fixed resolving default variables
Implemented defined() function Automatic class invalidation upon fields change
1 parent 6c85302 commit 028edc6

File tree

7 files changed

+451
-71
lines changed

7 files changed

+451
-71
lines changed

src/ipc/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export interface IpcAPI
88
getEnvironmentTree(name: string): Promise<any>;
99
findNode(localPath: string): Promise<any>;
1010
acquireNodeClass(nodePath: string, className: string): Promise<any>;
11+
hasNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<boolean>;
1112
setNodeClassProperty(nodePath: string, className: string, propertyName: string, value: any): Promise<any>;
1213
removeNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<any>;
1314
removeNodeClassProperties(nodePath: string, className: string): Promise<any>;
15+
invalidateNodeClass(nodePath: string, className: string): Promise<void>;
1416
getClassInfo(env: string): Promise<any>;
1517
refreshWorkspace(): Promise<any>;
1618
showOpenDirectoryDialog(defaultPath?: string): Promise<string>;
@@ -26,6 +28,7 @@ export interface IpcAPI
2628
removeResourceFromNode(nodePath: string, definedTypeName: string, title: string): Promise<void>;
2729
renameNodeResource(nodePath: string, definedTypeName: string, title: string, newTitle: string): Promise<boolean>;
2830
removeResourcesFromNode(nodePath: string, definedTypeName: string): Promise<string[]>;
31+
invalidateNodeResource(nodePath: string, definedTypeName: string, title: string): Promise<void>;
2932
chooseDefinedType(nodePath: string): Promise<string>;
3033
createNewResourceToNode(nodePath: string, definedTypeName: string, title: string): Promise<any>;
3134
removeAllResourcesFromNode(nodePath: string): Promise<any[]>;

src/ipc/server.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ export class IpcServer implements IpcAPI
164164

165165
return await node.setClassProperty(className, propertyName, value);
166166
}
167+
168+
public async hasNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<boolean>
169+
{
170+
const workspace: puppet.Workspace = getCurrentWorkspace();
171+
172+
if (workspace == null)
173+
{
174+
return false;
175+
}
176+
177+
const node = await workspace.findNode(nodePath);
178+
179+
if (node == null)
180+
{
181+
return false;
182+
}
183+
184+
return await node.hasClassProperty(className, propertyName);
185+
}
167186

168187
public async removeNodeClassProperty(
169188
nodePath: string, className: string, propertyName: string
@@ -545,6 +564,36 @@ export class IpcServer implements IpcAPI
545564

546565
await node.invalidate();
547566
}
567+
568+
public async invalidateNodeClass(nodePath: string, className: string): Promise<void>
569+
{
570+
const workspace: puppet.Workspace = getCurrentWorkspace();
571+
572+
if (workspace == null)
573+
return;
574+
575+
const node = await workspace.findNode(nodePath);
576+
577+
if (node == null)
578+
return;
579+
580+
await node.invalidateClass(className);
581+
}
582+
583+
public async invalidateNodeResource(nodePath: string, definedTypeName: string, title: string): Promise<void>
584+
{
585+
const workspace: puppet.Workspace = getCurrentWorkspace();
586+
587+
if (workspace == null)
588+
return;
589+
590+
const node = await workspace.findNode(nodePath);
591+
592+
if (node == null)
593+
return;
594+
595+
await node.invalidateDefinedType(definedTypeName, title);
596+
}
548597

549598
public async isNodeClassValid(nodePath: string, className: string): Promise<boolean>
550599
{

src/puppet.ts

Lines changed: 129 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export module puppet
135135
return this.info["file"];
136136
}
137137

138-
public get fields(): Array<string>
138+
public get defaults(): Array<string>
139139
{
140140
return Object.keys(this.info["defaults"] || {});
141141
}
@@ -170,7 +170,7 @@ export module puppet
170170
return {
171171
"name": this.name,
172172
"file": this.info["file"],
173-
"fields": this.fields,
173+
"defaults": this.defaults,
174174
"inherits": this.info["inherits"],
175175
"description": this.description,
176176
"options": this.options,
@@ -241,7 +241,7 @@ export module puppet
241241
return this.info["source"];
242242
}
243243

244-
public get fields(): Array<string>
244+
public get defaults(): Array<string>
245245
{
246246
return Object.keys(this.info["defaults"] || {});
247247
}
@@ -271,7 +271,7 @@ export module puppet
271271
return {
272272
"name": this.name,
273273
"file": this.info["file"],
274-
"fields": this.fields,
274+
"defaults": this.defaults,
275275
"inherits": this.info["inherits"],
276276
"description": this.description,
277277
"options": this.options,
@@ -1098,7 +1098,11 @@ export module puppet
10981098
}
10991099
}
11001100

1101-
export type GlobalVariableResolver = (key: string) => string;
1101+
export interface GlobalVariableResolver
1102+
{
1103+
get (key: string): string;
1104+
has (key: string): boolean;
1105+
}
11021106

11031107
export class ResolvedResource
11041108
{
@@ -1727,6 +1731,31 @@ export module puppet
17271731
this._compiledResources.clear();
17281732
}
17291733

1734+
public async invalidateClass(className: string): Promise<void>
1735+
{
1736+
if (this._compiledClasses.has(className))
1737+
{
1738+
const compiled = this._compiledClasses.get(className);
1739+
this._compiledClasses.remove(className);
1740+
1741+
// invalidate also a direct parent, if any
1742+
if (compiled.parentName != null)
1743+
{
1744+
await this.invalidateClass(compiled.parentName);
1745+
}
1746+
}
1747+
}
1748+
1749+
public async invalidateDefinedType(definedTypeName: string, title: string): Promise<void>
1750+
{
1751+
if (this._compiledResources.has(definedTypeName))
1752+
{
1753+
const titles = this._compiledResources.get(definedTypeName);
1754+
1755+
titles.remove(title);
1756+
}
1757+
}
1758+
17301759
public async remove(): Promise<boolean>
17311760
{
17321761
if (this._parent == null)
@@ -1800,9 +1829,14 @@ export module puppet
18001829
return zis.resolveFunction(name, global);
18011830
}
18021831

1803-
public async resolveGlobalVariable(name: string): Promise<string>
1832+
public getGlobalVariable(name: string): string
1833+
{
1834+
return global.get(name);
1835+
}
1836+
1837+
public hasGlobalVariable(name: string): boolean
18041838
{
1805-
return global(name);
1839+
return global.has(name);
18061840
}
18071841
});
18081842
}
@@ -1911,9 +1945,14 @@ export module puppet
19111945
return zis.resolveFunction(name, global);
19121946
}
19131947

1914-
public async resolveGlobalVariable(name: string): Promise<string>
1948+
public getGlobalVariable(name: string): string
19151949
{
1916-
return global(name);
1950+
return global.get(name);
1951+
}
1952+
1953+
public hasGlobalVariable(name: string): boolean
1954+
{
1955+
return global.has(name);
19171956
}
19181957
});
19191958
}
@@ -2118,14 +2157,45 @@ export module puppet
21182157
return this.configClasses.indexOf(className) >= 0
21192158
}
21202159

2160+
public hasGlobal(key: string): boolean
2161+
{
2162+
if (key == "facts")
2163+
{
2164+
return this.configFacts != null;
2165+
}
2166+
2167+
if (this.configFacts != null && this.configFacts.hasOwnProperty(key))
2168+
return true;
2169+
2170+
if (this._env.global.has(key) || this._env.workspace.global.has(key))
2171+
return true;
2172+
2173+
if (this._config != null && this._config.hasOwnProperty(key))
2174+
return true;
2175+
2176+
return false;
2177+
}
2178+
21212179
public getGlobal(key: string): string
21222180
{
21232181
if (key == "facts")
21242182
{
21252183
return this.configFacts;
21262184
}
21272185

2128-
return this.configFacts[key] || this._env.global.get(key) || this._env.workspace.global.get(key);
2186+
if (this.configFacts != null && this.configFacts.hasOwnProperty(key))
2187+
return this.configFacts[key];
2188+
2189+
if (this._env.global.has(key))
2190+
return this._env.global.get(key);
2191+
2192+
if (this._env.workspace.global.has(key))
2193+
return this._env.workspace.global.get(key);
2194+
2195+
if (this._config != null)
2196+
return this._config[key];
2197+
2198+
return null;
21292199
}
21302200

21312201
public async removeClass(className: string): Promise<void>
@@ -2274,9 +2344,9 @@ export module puppet
22742344
if (!this.hasClass(className))
22752345
throw Error("No such class: " + className);
22762346

2277-
return await this.resolveClass(className, (key: string) =>
2278-
{
2279-
return zis.getGlobal(key);
2347+
return await this.resolveClass(className, {
2348+
get: (key: string) => zis.getGlobal(key),
2349+
has: (key: string) => zis.hasGlobal(key)
22802350
});
22812351
}
22822352

@@ -2303,9 +2373,9 @@ export module puppet
23032373

23042374
values["title"] = title;
23052375

2306-
return await this.resolveResource(definedTypeName, title, values, (key: string) =>
2307-
{
2308-
return zis.getGlobal(key);
2376+
return await this.resolveResource(definedTypeName, title, values, {
2377+
get: (key: string) => zis.getGlobal(key),
2378+
has: (key: string) => zis.hasGlobal(key)
23092379
});
23102380
}
23112381

@@ -2362,6 +2432,23 @@ export module puppet
23622432
await this.save();
23632433
}
23642434

2435+
public async hasClassProperty(className: string, propertyName: string): Promise<boolean>
2436+
{
2437+
const classInfo = this._env.findClassInfo(className);
2438+
2439+
if (classInfo == null)
2440+
return false;
2441+
2442+
const compiled = await this.acquireClass(className);
2443+
2444+
if (!compiled)
2445+
return false;
2446+
2447+
const propertyPath = this.compilePropertyPath(className, propertyName);
2448+
2449+
return this.config != null && this.config.hasOwnProperty(propertyPath);
2450+
}
2451+
23652452
public async removeClassProperty(className: string, propertyName: string): Promise<any>
23662453
{
23672454
const classInfo = this._env.findClassInfo(className);
@@ -2417,7 +2504,7 @@ export module puppet
24172504
if (!compiled)
24182505
return;
24192506

2420-
for (const propertyName of classInfo.fields)
2507+
for (const propertyName of compiled.resolvedFields.getKeys())
24212508
{
24222509
const propertyPath = this.compilePropertyPath(className, propertyName);
24232510
delete this.config[propertyPath];
@@ -2440,6 +2527,8 @@ export module puppet
24402527
const errors: any = {};
24412528
const hints: any = {};
24422529
const fields: string[] = [];
2530+
const definedFields: string[] = [];
2531+
const requiredFields: string[] = [];
24432532
const values: any = {};
24442533
const classHints: any = compiled.hints;
24452534

@@ -2448,6 +2537,11 @@ export module puppet
24482537
const property = compiled.getResolvedProperty(name);
24492538
fields.push(name);
24502539

2540+
if (classInfo.defaults.indexOf(name) < 0)
2541+
{
2542+
requiredFields.push(name);
2543+
}
2544+
24512545
if (property.hasType)
24522546
{
24532547
types[name] = {
@@ -2475,10 +2569,12 @@ export module puppet
24752569
}
24762570

24772571
const propertyPath = this.compilePropertyPath(className, name);
2478-
const configValue = this.config[propertyPath];
2479-
if (configValue != null)
2572+
2573+
if (this.config.hasOwnProperty(propertyPath))
24802574
{
2575+
const configValue = this.config[propertyPath];
24812576
values[name] = configValue;
2577+
definedFields.push(name);
24822578
}
24832579
}
24842580

@@ -2491,7 +2587,9 @@ export module puppet
24912587
"errors": errors,
24922588
"propertyHints": hints,
24932589
"hints": classHints,
2494-
"fields": fields
2590+
"definedFields": definedFields,
2591+
"fields": fields,
2592+
"requiredFields": requiredFields
24952593
}
24962594
}
24972595

@@ -2507,6 +2605,8 @@ export module puppet
25072605
const defaultValues: any = {};
25082606
const types: any = {};
25092607
const fields: string[] = [];
2608+
const definedFields: string[] = [];
2609+
const requiredFields: string[] = [];
25102610
const errors: any = {};
25112611
const hints: any = {};
25122612
const values: any = {};
@@ -2520,6 +2620,7 @@ export module puppet
25202620
for (const k in t)
25212621
{
25222622
values[k] = t[k];
2623+
definedFields.push(k);
25232624
}
25242625
}
25252626
}
@@ -2528,6 +2629,11 @@ export module puppet
25282629
{
25292630
const property = compiled.resource.resolvedFields.get(name);
25302631

2632+
if (classInfo.defaults.indexOf(name) < 0)
2633+
{
2634+
requiredFields.push(name);
2635+
}
2636+
25312637
if (property.hasType)
25322638
{
25332639
types[name] = {
@@ -2565,7 +2671,9 @@ export module puppet
25652671
"types": types,
25662672
"errors": errors,
25672673
"propertyHints": hints,
2568-
"fields": fields
2674+
"definedFields": definedFields,
2675+
"fields": fields,
2676+
"requiredFields": requiredFields
25692677
}
25702678
}
25712679
}

0 commit comments

Comments
 (0)