Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Added parent information on information on visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Apr 9, 2019
1 parent 3cdc3b7 commit 9f59ef7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ConverterOption { type?: Function | Function[], path?: string[], decor
interface ConverterMap { type: Function, converter: Converter }
interface ObjectInfo<T> {
type: T,
parent?: Class ,
path: string[],
converters: ConverterStore,
visitors: Visitor[],
Expand Down Expand Up @@ -59,12 +60,14 @@ class ConversionError extends Error {

abstract class ConverterInvocation implements ObjectInfo<Function | Function[] | undefined> {
type: Function | Function[] | undefined
parent?: Class
path: string[]
converters: Map<string | Function, Converter>
visitors: Visitor[]
decorators: any[]
constructor({ type, path, converters, visitors, decorators, ...opts }: ObjectInfo<Function | Function[] | undefined>) {
constructor({ type, parent, path, converters, visitors, decorators, ...opts }: ObjectInfo<Function | Function[] | undefined>) {
this.type = type
this.parent = parent
this.path = path
this.converters = converters
this.visitors = visitors
Expand Down Expand Up @@ -252,7 +255,7 @@ async function defaultVisitor(value: any, { type, converters, ...restInfo }: Obj
return converters.get(type)!(value, { type, converters, ...restInfo })
//if type of model and has no converter, use DefaultObject converter
else
return converters.get("Class")!(value, { type, converters, ...restInfo })
return converters.get("Class")!(value, { type, converters, ...restInfo, parent: type as Class})
}

async function convert(value: any, { type, ...restInfo }: ObjectInfo<Function | Function[] | undefined>): Promise<ConversionResult> {
Expand Down
56 changes: 55 additions & 1 deletion test/visitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ describe("Visitor", () => {
else return prevResult
}

async function recursiveValue(value: any, info: ConverterInvocation): Promise<ConversionResult> {
const result = await info.proceed()
return new ConversionResult({ parent: info.parent, value: result.value })
}

const convert = createConverter({
visitors: [myVisitor]
})
Expand Down Expand Up @@ -70,6 +75,55 @@ describe("Visitor", () => {
const result = await convert(123)
expect(result).toBe(2000)
})

it("Should provide parent class information", async () => {
const convert = createConverter({ visitors: [recursiveValue] })
@reflect.parameterProperties()
class AnimalClass {
constructor(
public id: number,
public name: string,
) { }
}
const result = await convert({ id: "12", name: "Mimi" }, AnimalClass)
expect(result).toEqual({
parent: undefined,
value: {
id: { parent: AnimalClass, value: 12 },
name: { parent: AnimalClass, value: "Mimi" }
}
})
})

it("Should provide parent class information on nested object", async () => {
const convert = createConverter({ visitors: [recursiveValue] })
@reflect.parameterProperties()
class Tag {
constructor(public age: number) { }
}
@reflect.parameterProperties()
class AnimalClass {
constructor(
public id: number,
public name: string,
public tag: Tag
) { }
}
const result = await convert({ id: "12", name: "Mimi", tag: { age: "12" } }, AnimalClass)
expect(result).toEqual({
parent: undefined,
value: {
id: { parent: AnimalClass, value: 12 },
name: { parent: AnimalClass, value: "Mimi" },
tag: {
parent: AnimalClass,
value: {
age: { parent: Tag, value: 12 }
}
}
}
})
})
})


Expand Down Expand Up @@ -202,7 +256,7 @@ describe("Result Merge", () => {
class Tag {
constructor(public tag: number) { }
}
await expect(convert([{tag: "1"}, {tag: "123"}], [Tag]))
await expect(convert([{ tag: "1" }, { tag: "123" }], [Tag]))
.rejects.toThrow("0.tag Lorem ipsum\n1.tag Lorem ipsum")
})

Expand Down

0 comments on commit 9f59ef7

Please sign in to comment.