@@ -119,28 +119,48 @@ export function extendsDirective(): ParsingExtension {
119
119
120
120
/** Lookup a property in the same file, and "copy" it */
121
121
export function extendsSelfDirective ( ) : ParsingExtension {
122
- const validate : ValidationFunction < string > = validationFunction ( ( { stringSchema } ) =>
123
- stringSchema ( ) ,
122
+ const validate : ValidationFunction < string > = validationFunction (
123
+ ( { oneOf, stringSchema, emptySchema } ) =>
124
+ oneOf ( stringSchema ( ) , emptySchema ( ) . addString ( 'select' ) . addString ( 'env' , { } , false ) ) ,
124
125
) ;
125
126
126
127
return named (
127
128
'$extendsSelf' ,
128
- forKey ( '$extendsSelf' , ( input , key , ctx ) => async ( parse , _ , __ , ___ , root ) => {
129
+ forKey ( '$extendsSelf' , ( input , key , parentKeys , ctx ) => async ( parse , _ , __ , ___ , root ) => {
130
+ let select : string ;
131
+ let env : string | undefined ;
132
+
129
133
const value = ( await parse ( input ) ) . toJSON ( ) ;
130
- validate ( value , [ ...ctx , key ] ) ;
134
+
135
+ validate ( value , [ ...parentKeys , key ] ) ;
136
+
137
+ if ( typeof value === 'string' ) {
138
+ select = value ;
139
+ } else {
140
+ ( { select, env } = value ) ;
141
+ }
142
+
143
+ const environmentOptions = {
144
+ ...environmentOptionsFromContext ( ctx ) ,
145
+ override : env ,
146
+ } ;
131
147
132
148
// we temporarily use a ParsedValue literal so that we get the same property lookup semantics
133
- const selected = ParsedValue . literal ( root ) . property ( value . split ( '.' ) ) ;
149
+ const selected = ParsedValue . literal ( root ) . property ( select . split ( '.' ) ) ;
134
150
135
151
if ( selected === undefined ) {
136
- throw new AppConfigError ( `$extendsSelf selector was not found (${ value } )` ) ;
152
+ throw new AppConfigError ( `$extendsSelf selector was not found (${ select } )` ) ;
137
153
}
138
154
139
155
if ( selected . asObject ( ) !== undefined ) {
140
- return parse ( selected . toJSON ( ) , { shouldMerge : true } ) ;
156
+ return parse ( selected . toJSON ( ) , { shouldMerge : true } , undefined , undefined , {
157
+ environmentOptions,
158
+ } ) ;
141
159
}
142
160
143
- return parse ( selected . toJSON ( ) , { shouldFlatten : true } ) ;
161
+ return parse ( selected . toJSON ( ) , { shouldFlatten : true } , undefined , undefined , {
162
+ environmentOptions,
163
+ } ) ;
144
164
} ) ,
145
165
) ;
146
166
}
0 commit comments