-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from jordojordo/policy-settings-empty-objects
Remove empty objects completely from policy during creation
- Loading branch information
Showing
24 changed files
with
141 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
import isEmpty from 'lodash/isEmpty'; | ||
import isObject from 'lodash/isObject'; | ||
|
||
export function removeEmptyAttrs(obj: any) { | ||
export function removeEmptyAttrs(obj: any): any { | ||
Object.keys(obj).forEach((key: any) => { | ||
const value = obj[key]; | ||
|
||
if ( value === undefined || value === null || value === '' || (Array.isArray(value) && !value.length) || (isObject(value) && isEmpty(value)) ) { | ||
// Check for value being empty, null, or an empty array | ||
if ( value === undefined || value === null || value === '' || (Array.isArray(value) && !value.length) ) { | ||
delete obj[key]; | ||
} else if ( isObject(value) ) { | ||
// Recursively clean the object | ||
removeEmptyAttrs(value); | ||
|
||
// After cleaning, if the object is empty, delete it | ||
if ( isEmpty(value) ) { | ||
delete obj[key]; | ||
} | ||
} | ||
}); | ||
|
||
// Added check: If the parent object is now empty, return null to signal removal | ||
if ( isEmpty(obj) ) { | ||
return null; | ||
} | ||
|
||
return obj; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { removeEmptyAttrs } from '@kubewarden/utils/object'; | ||
|
||
describe('removeEmptyAttrs', () => { | ||
it('should remove attributes that are undefined, null, or empty', () => { | ||
const obj = { | ||
name: 'Test', | ||
value: null, | ||
details: {}, | ||
list: [], | ||
nested: { | ||
emptyString: '', | ||
emptyObj: {}, | ||
emptyArray: [], | ||
}, | ||
}; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual({ name: 'Test' }); | ||
}); | ||
|
||
it('should remove deeply nested empty objects', () => { | ||
const obj = { level1: { level2: { level3: {} } } }; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual(null); | ||
}); | ||
|
||
it('should handle arrays and non-object values correctly', () => { | ||
const obj = { | ||
arrayNotEmpty: [1, 2, 3], | ||
stringValue: 'hello', | ||
numberValue: 123, | ||
booleanValue: false, | ||
emptyObject: {}, | ||
}; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual({ | ||
arrayNotEmpty: [1, 2, 3], | ||
stringValue: 'hello', | ||
numberValue: 123, | ||
booleanValue: false, | ||
}); | ||
}); | ||
|
||
it('should remove settings entirely if cpu and memory are empty', () => { | ||
const obj = { | ||
apiVersion: 'policies.kubewarden.io.v1', | ||
kind: 'ClusterAdmissionPolicy', | ||
spec: { | ||
backgroundAudit: true, | ||
policyServer: 'default', | ||
settings: { | ||
cpu: {}, | ||
memory: {}, | ||
}, | ||
}, | ||
}; | ||
const expectedObj = { | ||
apiVersion: 'policies.kubewarden.io.v1', | ||
kind: 'ClusterAdmissionPolicy', | ||
spec: { | ||
backgroundAudit: true, | ||
policyServer: 'default' | ||
} | ||
}; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual(expectedObj); | ||
}); | ||
|
||
it('should remove the entire object if it becomes empty after cleanup', () => { | ||
const obj = { | ||
spec: { | ||
settings: { | ||
cpu: {}, | ||
memory: {}, | ||
}, | ||
}, | ||
}; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual(null); | ||
}); | ||
|
||
it('should remove only the empty memory object while preserving non-empty cpu object', () => { | ||
const obj = { | ||
settings: { | ||
cpu: { | ||
defaultLimit: '100m', | ||
defaultRequest: '100m', | ||
maxLimit: '100m', | ||
}, | ||
memory: {}, | ||
}, | ||
}; | ||
const expectedObj = { | ||
settings: { | ||
cpu: { | ||
defaultLimit: '100m', | ||
defaultRequest: '100m', | ||
maxLimit: '100m', | ||
}, | ||
}, | ||
}; | ||
const cleanedObj = removeEmptyAttrs(obj); | ||
|
||
expect(cleanedObj).toEqual(expectedObj); | ||
}); | ||
}); |