@@ -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