-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jean-Baptiste Bianchi <[email protected]>
- Loading branch information
Showing
7 changed files
with
142 additions
and
25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,58 @@ | ||
/* | ||
* Copyright 2021-Present The Serverless Workflow Specification Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* oUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
/** | ||
* Represents a fluent builder proxy | ||
*/ | ||
export type Builder<T> = { | ||
build: () => T; | ||
} & { | ||
[K in keyof T]-?: (arg: T[K]) => Builder<T>; | ||
}; | ||
|
||
/** | ||
* The default function used to build an object, basically just return the provided object | ||
* @param data The object to "build" | ||
* @returns | ||
*/ | ||
function defaultBuildingFn<T>(data: Partial<T>): () => T { | ||
return () => data as T; | ||
} | ||
|
||
/** | ||
* A factory for fluent builders that proxy properties assignations and can validate against schema on build() | ||
* @param {Function} buildingFn The function used to validate and produce the object on build() | ||
* @returns {Builder} A fluent builder | ||
*/ | ||
export function builder<T>(buildingFn?: (data: Partial<T>) => () => T): Builder<T> { | ||
const data: Partial<T> = {}; | ||
const proxy = new Proxy({} as Builder<T>, { | ||
get: (_, prop) => { | ||
if (prop === 'build') { | ||
return (buildingFn || defaultBuildingFn)(data); | ||
} | ||
return (value: unknown): Builder<T> => { | ||
(data as any)[prop.toString()] = value; | ||
return proxy; | ||
}; | ||
}, | ||
set: () => { | ||
return false; | ||
}, | ||
}); | ||
return proxy; | ||
} |
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,66 @@ | ||
/* | ||
* Copyright 2021-Present The Serverless Workflow Specification Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* oUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { Builder, builder } from '../../src/lib/builder'; | ||
import { type } from 'ts-inference-check'; | ||
|
||
type Person = { | ||
name: string; | ||
age: number; | ||
friends?: Array<Person>; | ||
[k: string]: unknown; | ||
}; | ||
|
||
const darknessMyOldFriend = { name: 'Darkness', age: 999 }; | ||
const isPerson = (data: Partial<Person>): data is Person => !!data.name && !!data.age; | ||
function personBuildingFn(data: Partial<Person>): () => Person { | ||
return () => { | ||
if (!isPerson(data)) { | ||
throw new Error('The provided object is not a person'); | ||
} | ||
return { | ||
...data, | ||
friends: [...(data.friends || []), darknessMyOldFriend], | ||
}; | ||
}; | ||
} | ||
const personBuilder = (): Builder<Person> => builder<Person>(personBuildingFn); | ||
|
||
describe('builder proxy', () => { | ||
it('should infer property types', () => { | ||
const builder = personBuilder(); | ||
expect(type(builder.name).is<(arg: string) => Builder<Person>>(true)).toBe(true); | ||
expect(type(builder.age).is<(arg: number) => Builder<Person>>(true)).toBe(true); | ||
expect(type(builder.friends).is<(arg: Array<Person> | undefined) => Builder<Person>>(true)).toBe(true); | ||
expect(type(builder.lover).is<(arg: unknown) => Builder<Person>>(true)).toBe(true); | ||
}); | ||
|
||
it('should build', () => { | ||
const name = 'John Doe'; | ||
const age = 42; | ||
const friend = { name: 'Cookie Doe', age: 42 }; | ||
const lover = 'Jane Doe'; | ||
const person = personBuilder().name(name).age(age).friends([friend]).lover(lover).build(); | ||
expect(person).toBeDefined(); | ||
expect(person.name).toBe(name); | ||
expect(person.age).toBe(age); | ||
expect(person.friends?.length).toBe(2); | ||
expect(person.friends?.includes(friend)).toBe(true); | ||
expect(person.friends?.includes(darknessMyOldFriend)).toBe(true); | ||
expect(person.lover).toBe(lover); | ||
}); | ||
}); |
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