Skip to content

Commit e712034

Browse files
authored
Add getFunctionConfigMap function (#164)
* Add getFunctionConfigDataOrThrow function * Address review comments * Remove if statement * Remove intermediate variable
1 parent e7187d0 commit e712034

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

ts/kpt-functions/src/types.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ export class Configs {
166166
return this.functionConfig;
167167
}
168168

169+
/**
170+
* Returns the map of data values if functionConfig is of kind ConfigMap.
171+
*
172+
* Throws a FunctionConfigError if functionConfig is undefined OR
173+
* if the kind is not a v1/ConfigMap.
174+
*/
175+
getFunctionConfigMap() {
176+
const cm = this.getFunctionConfig();
177+
if (cm === undefined) {
178+
throw new FunctionConfigError(
179+
`functionConfig expected, instead undefined`
180+
);
181+
}
182+
if (!isConfigMap(cm)) {
183+
throw new FunctionConfigError(
184+
`functionConfig expected to be of kind ConfigMap, instead got: ${cm.kind}`
185+
);
186+
}
187+
const configMap = new Map<string, string>();
188+
for (const key in cm.data) {
189+
if (cm.data.hasOwnProperty(key)) {
190+
configMap.set(key, cm.data[key]);
191+
}
192+
}
193+
return configMap;
194+
}
195+
169196
/**
170197
* Returns the value for the given key if functionConfig is of kind ConfigMap.
171198
*
@@ -177,16 +204,7 @@ export class Configs {
177204
* @key key The key in the 'data' field in the ConfigMap object given as the functionConfig.
178205
*/
179206
getFunctionConfigValue(key: string): string | undefined {
180-
const cm = this.functionConfig;
181-
if (!cm) {
182-
return undefined;
183-
}
184-
if (!isConfigMap(cm)) {
185-
throw new FunctionConfigError(
186-
`functionConfig expected to be of kind ConfigMap, instead got: ${cm.kind}`
187-
);
188-
}
189-
return cm.data && cm.data[key];
207+
return this.getFunctionConfigMap().get(key);
190208
}
191209

192210
/**

ts/kpt-functions/src/types_test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ describe('functionConfig', () => {
248248
k2: 'v2',
249249
},
250250
};
251+
const configMap = new Map<string, string>();
252+
configMap.set('k1', 'v1');
253+
configMap.set('k2', 'v2');
251254
const configs = new Configs(undefined, cm);
252255
expect(configs.getFunctionConfig()).toEqual(cm);
256+
expect(configs.getFunctionConfigMap()).toEqual(configMap);
253257
expect(configs.getFunctionConfigValue('k1')).toEqual('v1');
254258
expect(configs.getFunctionConfigValue('k2')).toEqual('v2');
255259
expect(configs.getFunctionConfigValue('k3')).toBeUndefined();
@@ -260,14 +264,16 @@ describe('functionConfig', () => {
260264
it('no object', () => {
261265
const configs = new Configs(undefined);
262266
expect(configs.getFunctionConfig()).toBeUndefined();
263-
expect(configs.getFunctionConfigValue('k3')).toBeUndefined();
267+
expect(() => configs.getFunctionConfigMap()).toThrow();
268+
expect(() => configs.getFunctionConfigValue('k3')).toThrow();
264269
expect(() => configs.getFunctionConfigValueOrThrow('k3')).toThrow();
265270
});
266271

267272
it('other kinds', () => {
268273
const r1 = new Role('alice');
269274
const configs = new Configs(undefined, r1);
270275
expect(configs.getFunctionConfig()).toEqual(r1);
276+
expect(() => configs.getFunctionConfigMap()).toThrow();
271277
expect(() => configs.getFunctionConfigValue('k1')).toThrow();
272278
expect(() => configs.getFunctionConfigValueOrThrow('k1')).toThrow();
273279
});

0 commit comments

Comments
 (0)