@@ -7,7 +7,7 @@ import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
77import { ParameterChange } from '../plan/change-set.js' ;
88import { ResourceController } from './resource-controller.js' ;
99import { TestConfig , testPlan , TestResource , TestStatefulParameter } from '../utils/test-utils.test.js' ;
10- import { untildify } from '../utils/utils.js' ;
10+ import { tildify , untildify } from '../utils/utils.js' ;
1111import { ArrayStatefulParameter , StatefulParameter } from '../stateful-parameter/stateful-parameter.js' ;
1212import { Plan } from '../plan/plan.js' ;
1313
@@ -20,9 +20,11 @@ describe('Resource tests', () => {
2020 id : 'type' ,
2121 dependencies : [ 'homebrew' , 'python' ] ,
2222 parameterSettings : {
23- propA : { canModify : true , inputTransformation : ( input ) => untildify ( input ) } ,
23+ propA : {
24+ canModify : true ,
25+ transformation : { to : ( input ) => untildify ( input ) , from : ( input ) => tildify ( input ) }
26+ } ,
2427 } ,
25- inputTransformation : ( config ) => ( { propA : config . propA , propC : config . propB } ) ,
2628 }
2729 }
2830
@@ -544,7 +546,7 @@ describe('Resource tests', () => {
544546 parameterSettings : {
545547 propD : {
546548 type : 'array' ,
547- inputTransformation : {
549+ transformation : {
548550 to : ( hosts : Record < string , unknown > [ ] ) => hosts . map ( ( h ) => Object . fromEntries (
549551 Object . entries ( h )
550552 . map ( ( [ k , v ] ) => [
@@ -619,4 +621,94 @@ describe('Resource tests', () => {
619621 }
620622 } )
621623 } )
624+
625+ it ( 'Applies reverse input transformations for imports (object level)' , async ( ) => {
626+ const resource = new class extends TestResource {
627+ getSettings ( ) : ResourceSettings < TestConfig > {
628+ return {
629+ id : 'resourceType' ,
630+ parameterSettings : {
631+ propD : {
632+ type : 'array' ,
633+ }
634+ } ,
635+ transformation : {
636+ to : ( input : any ) => ( {
637+ ...input ,
638+ propD : input . propD ?. map ( ( h ) => Object . fromEntries (
639+ Object . entries ( h )
640+ . map ( ( [ k , v ] ) => [
641+ k ,
642+ typeof v === 'boolean'
643+ ? ( v ? 'yes' : 'no' ) // The file takes 'yes' or 'no' instead of booleans
644+ : v ,
645+ ] )
646+ )
647+ )
648+ } ) ,
649+ from : ( output : any ) => ( {
650+ ...output ,
651+ propD : output . propD ?. map ( ( h ) => Object . fromEntries (
652+ Object . entries ( h )
653+ . map ( ( [ k , v ] ) => [
654+ k ,
655+ v === 'yes' || v === 'no'
656+ ? ( v === 'yes' )
657+ : v ,
658+ ] )
659+ ) )
660+ } )
661+ }
662+ }
663+ }
664+
665+ async refresh ( parameters : Partial < TestConfig > ) : Promise < Partial < TestConfig > | null > {
666+ return {
667+ propD : [
668+ {
669+ Host : 'new.com' ,
670+ AddKeysToAgent : true ,
671+ IdentityFile : 'id_ed25519'
672+ } ,
673+ {
674+ Host : 'github.com' ,
675+ AddKeysToAgent : true ,
676+ UseKeychain : true ,
677+ } ,
678+ {
679+ Match : 'User bob,joe,phil' ,
680+ PasswordAuthentication : true ,
681+ }
682+ ] ,
683+ }
684+ }
685+ }
686+
687+ const controller = new ResourceController ( resource ) ;
688+ const plan = await controller . import ( { type : 'resourceType' } , { } ) ;
689+
690+ expect ( plan ! [ 0 ] ) . toMatchObject ( {
691+ 'core' : {
692+ 'type' : 'resourceType'
693+ } ,
694+ 'parameters' : {
695+ 'propD' : [
696+ {
697+ 'Host' : 'new.com' ,
698+ 'AddKeysToAgent' : true ,
699+ 'IdentityFile' : 'id_ed25519'
700+ } ,
701+ {
702+ 'Host' : 'github.com' ,
703+ 'AddKeysToAgent' : true ,
704+ 'UseKeychain' : true
705+ } ,
706+ {
707+ 'Match' : 'User bob,joe,phil' ,
708+ 'PasswordAuthentication' : true
709+ }
710+ ]
711+ }
712+ } )
713+ } )
622714} ) ;
0 commit comments