Skip to content
Snippets Groups Projects
Commit 101921ce authored by Art Lowel's avatar Art Lowel
Browse files

refactored IDToUUIDSerializer

parent 8b8f067a
No related branches found
No related tags found
No related merge requests found
import { IDToUUIDSerializer } from './it-to-uuid-serializer';
fdescribe('IDToUUIDSerializer', () => {
let serializer: IDToUUIDSerializer;
const prefix = 'test-prefix';
beforeEach(() => {
serializer = new IDToUUIDSerializer(prefix);
});
describe('Serialize', () => {
it('should return undefined', () => {
expect(serializer.Serialize('some-uuid')).toBeUndefined()
});
});
describe('Deserialize', () => {
describe('when ID is defined', () => {
it('should prepend the prefix to the ID', () => {
const id = 'some-id';
expect(serializer.Deserialize(id)).toBe(`${prefix}-${id}`);
});
});
describe('when ID is null or undefined', () => {
it('should return null or undefined', () => {
expect(serializer.Deserialize(null)).toBeNull();
expect(serializer.Deserialize(undefined)).toBeUndefined();
});
});
});
});
import { hasValue } from '../../shared/empty.util';
export class IDToUUIDSerializer {
constructor(private prefix: string) {
}
Serialize(uuid: string): any {
return undefined; // ui-only uuid doesn't need to be sent back to the server
}
Deserialize(id: string): string {
if (hasValue(id)) {
return `${this.prefix}-${id}`;
} else {
return id;
}
}
}
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
import { BitstreamFormat } from '../../shared/bitstream-format.model';
import { mapsTo } from '../builders/build-decorators';
import { BitstreamFormat } from '../../shared/bitstream-format.model';
import { IDToUUIDSerializer } from '../it-to-uuid-serializer';
import { NormalizedObject } from './normalized-object.model';
import { NormalizedDSpaceObject } from './normalized-dspace-object.model';
// While BitstreamFormats don't have a UUID, but need to be cachable: use this serializer. Remove it when the REST API returns them with UUID
const BitstreamFormatUUIDSerializer = {
Serialize(json: any): any {
// No need to serialize again, de ID is already serialized by the id field itself
return {};
},
Deserialize(json: any): any {
return 'bitstream-format-' + json.id;
}
};
@mapsTo(BitstreamFormat)
@inheritSerialization(NormalizedObject)
......@@ -41,6 +30,6 @@ export class NormalizedBitstreamFormat extends NormalizedObject {
@autoserialize
id: string;
@autoserializeAs(BitstreamFormatUUIDSerializer)
@autoserializeAs(new IDToUUIDSerializer('bitstream-format'), 'id')
uuid: string;
}
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
import { ResourcePolicy } from '../../shared/resource-policy.model';
import { mapsTo } from '../builders/build-decorators';
import { BitstreamFormat } from '../../shared/bitstream-format.model';
import { IDToUUIDSerializer } from '../it-to-uuid-serializer';
import { NormalizedObject } from './normalized-object.model';
import { ResourcePolicy } from '../../shared/resource-policy.model';
// While ResourcePolicyUUIDSerializer don't have a UUID, but need to be cachable: use this serializer. Remove it when the REST API returns them with UUID
const ResourcePolicyUUIDSerializer = {
Serialize(json: any): any {
// No need to serialize again, de ID is already serialized by the id field itself
return {};
},
Deserialize(json: any): any {
return 'resource-policy-' + json.id;
}
};
@mapsTo(ResourcePolicy)
@inheritSerialization(NormalizedObject)
......@@ -29,6 +18,6 @@ export class NormalizedResourcePolicy extends NormalizedObject {
@autoserialize
id: string;
@autoserializeAs(ResourcePolicyUUIDSerializer)
@autoserializeAs(new IDToUUIDSerializer('resource-policy'), 'id')
uuid: string;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment