mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 15:33:04 +00:00
refactored IDToUUIDSerializer
This commit is contained in:
34
src/app/core/cache/it-to-uuid-serializer.spec.ts
vendored
Normal file
34
src/app/core/cache/it-to-uuid-serializer.spec.ts
vendored
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
19
src/app/core/cache/it-to-uuid-serializer.ts
vendored
Normal file
19
src/app/core/cache/it-to-uuid-serializer.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,20 +1,9 @@
|
|||||||
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
|
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
|
||||||
|
import { BitstreamFormat } from '../../shared/bitstream-format.model';
|
||||||
|
|
||||||
import { mapsTo } from '../builders/build-decorators';
|
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 { 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)
|
@mapsTo(BitstreamFormat)
|
||||||
@inheritSerialization(NormalizedObject)
|
@inheritSerialization(NormalizedObject)
|
||||||
@@ -41,6 +30,6 @@ export class NormalizedBitstreamFormat extends NormalizedObject {
|
|||||||
@autoserialize
|
@autoserialize
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@autoserializeAs(BitstreamFormatUUIDSerializer)
|
@autoserializeAs(new IDToUUIDSerializer('bitstream-format'), 'id')
|
||||||
uuid: string;
|
uuid: string;
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +1,9 @@
|
|||||||
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
|
import { autoserialize, autoserializeAs, inheritSerialization } from 'cerialize';
|
||||||
|
|
||||||
import { mapsTo } from '../builders/build-decorators';
|
|
||||||
import { BitstreamFormat } from '../../shared/bitstream-format.model';
|
|
||||||
import { NormalizedObject } from './normalized-object.model';
|
|
||||||
import { ResourcePolicy } from '../../shared/resource-policy.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
|
import { mapsTo } from '../builders/build-decorators';
|
||||||
const ResourcePolicyUUIDSerializer = {
|
import { IDToUUIDSerializer } from '../it-to-uuid-serializer';
|
||||||
Serialize(json: any): any {
|
import { NormalizedObject } from './normalized-object.model';
|
||||||
// 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)
|
@mapsTo(ResourcePolicy)
|
||||||
@inheritSerialization(NormalizedObject)
|
@inheritSerialization(NormalizedObject)
|
||||||
@@ -29,6 +18,6 @@ export class NormalizedResourcePolicy extends NormalizedObject {
|
|||||||
@autoserialize
|
@autoserialize
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@autoserializeAs(ResourcePolicyUUIDSerializer)
|
@autoserializeAs(new IDToUUIDSerializer('resource-policy'), 'id')
|
||||||
uuid: string;
|
uuid: string;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user