11import { ParameterOperation , ResourceOperation , StringIndexedObject } from 'codify-schemas' ;
2- import { ParameterConfiguration } from './plan-types.js' ;
2+ import { ParameterOptions } from './plan-types.js' ;
33
44export interface ParameterChange < T extends StringIndexedObject > {
55 name : keyof T & string ;
@@ -61,12 +61,12 @@ export class ChangeSet<T extends StringIndexedObject> {
6161 static calculateParameterChangeSet < T extends StringIndexedObject > (
6262 desired : T | null ,
6363 current : T | null ,
64- options : { statefulMode : boolean , parameterConfigurations ?: Record < keyof T , ParameterConfiguration > } ,
64+ options : { statefulMode : boolean , parameterOptions ?: Record < keyof T , ParameterOptions > } ,
6565 ) : ParameterChange < T > [ ] {
6666 if ( options . statefulMode ) {
67- return ChangeSet . calculateStatefulModeChangeSet ( desired , current , options . parameterConfigurations ) ;
67+ return ChangeSet . calculateStatefulModeChangeSet ( desired , current , options . parameterOptions ) ;
6868 } else {
69- return ChangeSet . calculateStatelessModeChangeSet ( desired , current , options . parameterConfigurations ) ;
69+ return ChangeSet . calculateStatelessModeChangeSet ( desired , current , options . parameterOptions ) ;
7070 }
7171 }
7272
@@ -88,10 +88,10 @@ export class ChangeSet<T extends StringIndexedObject> {
8888 static isSame (
8989 desired : unknown ,
9090 current : unknown ,
91- configuration ?: ParameterConfiguration ,
91+ options ?: ParameterOptions ,
9292 ) : boolean {
93- if ( configuration ?. isEqual ) {
94- return configuration . isEqual ( desired , current ) ;
93+ if ( options ?. isEqual ) {
94+ return options . isEqual ( desired , current ) ;
9595 }
9696
9797 if ( Array . isArray ( desired ) && Array . isArray ( current ) ) {
@@ -102,9 +102,9 @@ export class ChangeSet<T extends StringIndexedObject> {
102102 return false ;
103103 }
104104
105- if ( configuration ?. isElementEqual ) {
105+ if ( options ?. isElementEqual ) {
106106 return sortedDesired . every ( ( value , index ) =>
107- configuration . isElementEqual ! ( value , sortedCurrent [ index ] )
107+ options . isElementEqual ! ( value , sortedCurrent [ index ] )
108108 ) ;
109109 }
110110
@@ -119,13 +119,15 @@ export class ChangeSet<T extends StringIndexedObject> {
119119 private static calculateStatefulModeChangeSet < T extends StringIndexedObject > (
120120 desired : T | null ,
121121 current : T | null ,
122- parameterConfigurations ?: Record < keyof T , ParameterConfiguration > ,
122+ parameterOptions ?: Record < keyof T , ParameterOptions > ,
123123 ) : ParameterChange < T > [ ] {
124124 const parameterChangeSet = new Array < ParameterChange < T > > ( ) ;
125125
126126 const _desired = { ...desired } ;
127127 const _current = { ...current } ;
128128
129+ this . addDefaultValues ( _desired , parameterOptions ) ;
130+
129131 for ( const [ k , v ] of Object . entries ( _current ) ) {
130132 if ( _desired [ k ] == null ) {
131133 parameterChangeSet . push ( {
@@ -139,7 +141,7 @@ export class ChangeSet<T extends StringIndexedObject> {
139141 continue ;
140142 }
141143
142- if ( ! ChangeSet . isSame ( _desired [ k ] , _current [ k ] , parameterConfigurations ?. [ k ] ) ) {
144+ if ( ! ChangeSet . isSame ( _desired [ k ] , _current [ k ] , parameterOptions ?. [ k ] ) ) {
143145 parameterChangeSet . push ( {
144146 name : k ,
145147 previousValue : v ,
@@ -184,13 +186,15 @@ export class ChangeSet<T extends StringIndexedObject> {
184186 private static calculateStatelessModeChangeSet < T extends StringIndexedObject > (
185187 desired : T | null ,
186188 current : T | null ,
187- parameterConfigurations ?: Record < keyof T , ParameterConfiguration > ,
189+ parameterOptions ?: Record < keyof T , ParameterOptions > ,
188190 ) : ParameterChange < T > [ ] {
189191 const parameterChangeSet = new Array < ParameterChange < T > > ( ) ;
190192
191193 const _desired = { ...desired } ;
192194 const _current = { ...current } ;
193195
196+ this . addDefaultValues ( _desired , parameterOptions ) ;
197+
194198 for ( const [ k , v ] of Object . entries ( _desired ) ) {
195199 if ( _current [ k ] == null ) {
196200 parameterChangeSet . push ( {
@@ -203,7 +207,7 @@ export class ChangeSet<T extends StringIndexedObject> {
203207 continue ;
204208 }
205209
206- if ( ! ChangeSet . isSame ( _desired [ k ] , _current [ k ] , parameterConfigurations ?. [ k ] ) ) {
210+ if ( ! ChangeSet . isSame ( _desired [ k ] , _current [ k ] , parameterOptions ?. [ k ] ) ) {
207211 parameterChangeSet . push ( {
208212 name : k ,
209213 previousValue : _current [ k ] ,
@@ -224,5 +228,16 @@ export class ChangeSet<T extends StringIndexedObject> {
224228
225229 return parameterChangeSet ;
226230 }
231+
232+ private static addDefaultValues < T extends StringIndexedObject > ( obj : Record < string , unknown > , options ?: Record < keyof T , ParameterOptions > ) {
233+ Object . entries ( options ?? { } )
234+ . filter ( ( [ , option ] ) => option . default !== undefined )
235+ . map ( ( [ name , option ] ) => [ name , option . default ] as const )
236+ . forEach ( ( [ key , defaultValue ] ) => {
237+ if ( obj [ key ] === undefined ) {
238+ obj [ key ] = defaultValue ;
239+ }
240+ } )
241+ }
227242
228243}
0 commit comments