@@ -3,7 +3,12 @@ import isObjectsEqual from 'lodash.isequal'
33import path from 'node:path' ;
44
55import { ArrayStatefulParameter , StatefulParameter } from '../stateful-parameter/stateful-parameter.js' ;
6- import { areArraysEqual , untildify } from '../utils/utils.js' ;
6+ import { areArraysEqual , tildify , untildify } from '../utils/utils.js' ;
7+
8+ export interface InputTransformation {
9+ to : ( input : any ) => Promise < any > | any ;
10+ from : ( current : any ) => Promise < any > | any ;
11+ }
712
813/**
914 * The configuration and settings for a resource.
@@ -166,12 +171,13 @@ export interface DefaultParameterSetting {
166171 default ?: unknown ;
167172
168173 /**
169- * A transformation of the input value for this parameter. This transformation is only applied to the desired parameter
170- * value supplied by the user.
174+ * A transformation of the input value for this parameter. Two transformations need to be provided: to (from desired to
175+ * the internal type), and from (from the internal type back to desired). All transformations need to be bi-directional
176+ * to support imports properly
171177 *
172178 * @param input The original parameter value from the desired config.
173179 */
174- inputTransformation ?: ( input : any ) => Promise < any > | any ;
180+ inputTransformation ?: InputTransformation ;
175181
176182 /**
177183 * Customize the equality comparison for a parameter. This is used in the diffing algorithm for generating the plan.
@@ -321,22 +327,29 @@ export function resolveFnFromEqualsFnOrString(
321327 return fnOrString as ( ( a : unknown , b : unknown ) => boolean ) | undefined ;
322328}
323329
324- const ParameterTransformationDefaults : Partial < Record < ParameterSettingType , ( input : any , parameter : ParameterSetting ) => Promise < any > | any > > = {
325- 'directory' : ( a : unknown ) => path . resolve ( untildify ( String ( a ) ) ) ,
326- 'stateful' : ( a : unknown , b : ParameterSetting ) => {
327- const sp = b as StatefulParameterSetting ;
328- return ( sp . definition ?. getSettings ( ) ?. inputTransformation )
329- ? ( sp . definition . getSettings ( ) . inputTransformation ! ( a ) )
330- : a ;
330+ const ParameterTransformationDefaults : Partial < Record < ParameterSettingType , InputTransformation > > = {
331+ 'directory' : {
332+ to : ( a : unknown ) => path . resolve ( untildify ( String ( a ) ) ) ,
333+ from : ( a : unknown ) => tildify ( String ( a ) ) ,
331334 } ,
332- 'string' : String ,
333- // TODO: Add a array parameter itemType parameter
334- // 'array': (arr: unknown[]) => arr.map((i) => (parameter as ArrayParameterSetting).itemType ? ParameterTransformationDefaults[])
335+ 'string' : {
336+ to : String ,
337+ from : String ,
338+ }
335339}
336340
337341export function resolveParameterTransformFn (
338342 parameter : ParameterSetting
339- ) : ( ( input : any , parameter : ParameterSetting ) => Promise < any > | any ) | undefined {
343+ ) : InputTransformation | undefined {
344+
345+ if ( parameter . type === 'stateful' && ! parameter . inputTransformation ) {
346+ const sp = ( parameter as StatefulParameterSetting ) . definition . getSettings ( ) ;
347+ if ( sp . inputTransformation ) {
348+ return ( parameter as StatefulParameterSetting ) . definition ?. getSettings ( ) ?. inputTransformation
349+ }
350+
351+ return sp . type ? ParameterTransformationDefaults [ sp . type ] : undefined ;
352+ }
340353
341354 if ( parameter . type === 'array'
342355 && ( parameter as ArrayParameterSetting ) . itemType
@@ -346,8 +359,13 @@ export function resolveParameterTransformFn(
346359 const itemType = ( parameter as ArrayParameterSetting ) . itemType ! ;
347360 const itemTransformation = ParameterTransformationDefaults [ itemType ] ! ;
348361
349- return ( input : unknown [ ] , parameter ) => {
350- return input . map ( ( i ) => itemTransformation ( i , parameter ) )
362+ return {
363+ to ( input : unknown [ ] ) {
364+ return input . map ( ( i ) => itemTransformation . to ( i ) )
365+ } ,
366+ from ( input : unknown [ ] ) {
367+ return input . map ( ( i ) => itemTransformation . from ( i ) )
368+ }
351369 }
352370 }
353371
0 commit comments