Skip to content

Commit

Permalink
fix: don't use relect metadata when serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Dec 2, 2018
1 parent c91779e commit a38f090
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ export default class Processor {
if (typeof value === 'string' && value.length === 0) continue
if (Array.isArray(value) && value.length === 0) continue

let id = Reflect.getMetadata('property:id', thing, key)
let [context, term] = id.split(':')
if (Array.isArray(value)) {
obj[term] = value.map(item => (item instanceof Thing) ? this.exportObject(item) : item)
obj[key] = value.map(item => (item instanceof Thing) ? this.exportObject(item) : item)
} else if (value instanceof Thing) {
obj[term] = this.exportObject(value)
obj[key] = this.exportObject(value)
} else {
obj[term] = value
obj[key] = value
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/types/Thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import { type, property } from './decorators'
*/
@type('schema:Thing')
export default class Thing {

/**
* The JSON-LD [type specifier](https://w3c.github.io/json-ld-syntax/#specifying-the-type) corresponding to
* the `@type` keyword.
*/
get type (): string {
return this.constructor.name
}
type: string

/**
* The JSON-LD [node identifier](https://w3c.github.io/json-ld-syntax/#node-identifiers) corresponding to
Expand All @@ -38,8 +37,9 @@ export default class Thing {
* @param initializer An object with initial property values
*/
constructor (initializer = {}) {
this.type = this.constructor.name
for (let [key, value] of Object.entries(initializer)) {
if (Reflect.hasMetadata('property:id', this, key)) {
if (Reflect.hasMetadata('property:name', this, key)) {
// @ts-ignore
this[key] = value
}
Expand Down
1 change: 1 addition & 0 deletions src/types/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function type (id: string): ClassDecorator {
*/
export function property (id: string, container: string = 'set'): PropertyDecorator {
return function (target: Object, propertyKey: string | symbol) {
Reflect.defineMetadata('property:name', propertyKey, target, propertyKey)
Reflect.defineMetadata('property:id', id, target, propertyKey)
if (container) Reflect.defineMetadata('property:container', container, target, propertyKey)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('exportJsonLd:Thing', () => {
'type': 'Thing',
'name': 'thing2',
'description': 'The second thing',
'identifier': ['thing2', 'thing_two']
'identifiers': ['thing2', 'thing_two']
})
})

Expand Down

0 comments on commit a38f090

Please sign in to comment.