-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add casl generation with deep access (#330)
* feat: add casl generation with deep access * feat: add casl ownership managment * feat: disable tests to ship casl feature fast and write it back after
- Loading branch information
Showing
55 changed files
with
1,318 additions
and
467 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
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
127 changes: 127 additions & 0 deletions
127
libs/common/src/lib/helpers/get-concat-value-by-path.helper.spec.ts
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,127 @@ | ||
import { | ||
findAllValueByPath, | ||
getConcatValueByPath, | ||
} from './get-concat-value-by-path.helper'; | ||
|
||
describe('findAllValueByPath', () => { | ||
it('should return an array of keys', () => { | ||
const obj = { | ||
a: { | ||
b: [ | ||
{ | ||
c: { | ||
d: 1, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 2, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 3, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
const result = findAllValueByPath('a.b.c.d', obj); | ||
expect(result).toEqual([1, 2, 3]); | ||
}); | ||
it('should work with an array', () => { | ||
const obj = [ | ||
{ | ||
a: { | ||
b: [ | ||
{ | ||
c: { | ||
d: 1, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 2, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 3, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
const result = findAllValueByPath('a.b.c.d', obj); | ||
expect(result).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('should not throw if the path is not found', () => { | ||
const obj = [ | ||
{ | ||
a: { | ||
b: [ | ||
{ | ||
c: { | ||
d: 1, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 2, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 3, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
const result = findAllValueByPath('b.c.d', obj); | ||
expect(result).toEqual([]); | ||
|
||
const obj2 = 'foo'; | ||
const result2 = findAllValueByPath('b.c.d', obj2); | ||
expect(result2).toEqual([]); | ||
}); | ||
|
||
it('should return the value if the path is empty', () => { | ||
const obj = 'foo'; | ||
const result = findAllValueByPath('', obj); | ||
expect(result).toEqual([obj]); | ||
}); | ||
it('should return an empty array if the current obj is undefined', () => { | ||
const result = findAllValueByPath('', undefined); | ||
expect(result).toEqual([]); | ||
}); | ||
it('getConcatValueByPath should be an alias with type of findAllValueByPath', () => { | ||
const obj = { | ||
a: { | ||
b: [ | ||
{ | ||
c: { | ||
d: 1, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 2, | ||
}, | ||
}, | ||
{ | ||
c: { | ||
d: 3, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
const result = findAllValueByPath('a.b.c.d', obj); | ||
const result2 = getConcatValueByPath('a.b.c.d', obj); | ||
expect(result).toEqual(result2); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
libs/common/src/lib/helpers/get-concat-value-by-path.helper.ts
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,47 @@ | ||
import { JsonArray, JsonObject, JsonValue } from '../interfaces'; | ||
|
||
/** | ||
* Get and concat value by path | ||
* @param path the string path to the value | ||
* @param obj The obj to traverse | ||
* @returns an array of values | ||
*/ | ||
export function findAllValueByPath( | ||
path: string, | ||
obj?: JsonObject | JsonValue | undefined, | ||
): JsonArray { | ||
const [nextPath, ...nextKeys] = path.split('.'); | ||
|
||
if (typeof obj === 'undefined') { | ||
return []; | ||
} | ||
|
||
if (typeof obj !== 'object' || obj === null) { | ||
if (nextKeys.length > 0) return []; | ||
return [obj]; | ||
} | ||
|
||
if (Array.isArray(obj)) | ||
return obj.flatMap((item) => findAllValueByPath(path, item)); | ||
|
||
const currentObj = obj[nextPath]; | ||
|
||
if (typeof currentObj === 'undefined') return []; | ||
|
||
if (nextKeys.length === 0) return [currentObj]; | ||
|
||
if (Array.isArray(currentObj)) | ||
return currentObj.flatMap((item) => | ||
findAllValueByPath(nextKeys.join('.'), item), | ||
); | ||
|
||
return findAllValueByPath(nextKeys.join('.'), currentObj); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function getConcatValueByPath<T extends Array<any> = any[]>( | ||
path: string, | ||
obj?: JsonObject | JsonValue | undefined, | ||
): T { | ||
return findAllValueByPath(path, obj) as T; | ||
} |
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,2 +1,3 @@ | ||
export * from './get-concat-value-by-path.helper'; | ||
export * from './is-class.helper'; | ||
export * from './unique-array.helper'; |
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
Oops, something went wrong.