Skip to content

Commit

Permalink
revert adding id to EntitySchemaField
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Nov 28, 2023
1 parent d707458 commit 3df2199
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/app/core/basic-datatypes/array/array.datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ConfigurableEnumService } from "../configurable-enum/configurable-enum.

describe("Schema data type: array", () => {
const schema: EntitySchemaField = {
id: null,
dataType: "array",
innerDataType: "configurable-enum",
additional: "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ describe("Schema data type: configurable-enum", () => {
const actualMapped = dataType.importMapFunction(
input,
{
id: null,
dataType: "configurable-enum",
additional: "genders",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe("EditConfigurableEnumComponent", () => {
expect(component.multi).toBeTrue();
});

function initWithSchema(schema: Omit<EntitySchemaField, "id">) {
function initWithSchema(schema: EntitySchemaField) {
const fromGroup = new FormGroup({ test: new FormControl() });
component.formControl = fromGroup.get("test") as FormControl;
component.formFieldConfig = { id: "test" };
component.propertySchema = { id: "test", ...schema };
component.propertySchema = schema;
component.entity = new Entity();
component.ngOnInit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("EntityFormService", () => {
});

it("should assign default values", () => {
const schema: EntitySchemaField = { id: "test", defaultValue: 1 };
const schema: EntitySchemaField = { defaultValue: 1 };
Entity.schema.set("test", schema);

let form = service.createFormGroup([{ id: "test" }], new Entity());
Expand All @@ -191,7 +191,7 @@ describe("EntityFormService", () => {
});

it("should not assign default values to existing entities", () => {
Entity.schema.set("test", { id: "test", defaultValue: 1 });
Entity.schema.set("test", { defaultValue: 1 });

const entity = new Entity();
entity._rev = "1-existing_entity";
Expand All @@ -202,7 +202,7 @@ describe("EntityFormService", () => {
});

it("should not overwrite existing values with default value", () => {
Entity.schema.set("test", { id: "test", defaultValue: 1 });
Entity.schema.set("test", { defaultValue: 1 });

const entity = new Entity();
entity["test"] = 2;
Expand Down
12 changes: 5 additions & 7 deletions src/app/core/entity/database-field.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { Entity } from "./model/entity";
import { DatabaseField } from "./database-field.decorator";
import { EntitySchemaField } from "./schema/entity-schema-field";

class TestClass extends Entity {
@DatabaseField()
Expand All @@ -39,13 +40,10 @@ describe("@DatabaseField Decorator", () => {

it("results in full schema", async () => {
expect(TestClass.schema).toEqual(
new Map([
["fieldUndefined", { id: "fieldUndefined", dataType: "string" }],
["fieldWithDefault", { id: "fieldWithDefault", dataType: "string" }],
[
"fieldDate",
{ id: "fieldDate", dataType: "date", generateIndex: true },
],
new Map<string, EntitySchemaField>([
["fieldUndefined", { dataType: "string" }],
["fieldWithDefault", { dataType: "string" }],
["fieldDate", { dataType: "date", generateIndex: true }],
]),
);
});
Expand Down
24 changes: 11 additions & 13 deletions src/app/core/entity/database-field.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,32 @@ import { EntitySchemaField } from "./schema/entity-schema-field";
*
* @param propertySchema (optional) SchemaField definition that configures additional options regarding this field
*/
export function DatabaseField(
propertySchema: Omit<EntitySchemaField, "id"> = {},
) {
export function DatabaseField(propertySchema: EntitySchemaField = {}) {
return (target, propertyName: string) => {
const schemaField: EntitySchemaField = {
id: propertyName,
...propertySchema,
};
// Retrieve datatype from TypeScript type definition
if (schemaField.dataType === undefined) {
if (propertySchema.dataType === undefined) {
const type = Reflect.getMetadata("design:type", target, propertyName);
const typeName = type.DATA_TYPE ?? type.name.toLowerCase();
// 'object' type is ignored
if (typeName !== "object") {
schemaField.dataType = typeName;
propertySchema.dataType = typeName;
}
}
addPropertySchema(target, schemaField);
addPropertySchema(target, propertyName, propertySchema);
};
}

export function addPropertySchema(target, propertySchema: EntitySchemaField) {
target[propertySchema.id] = undefined; // This ensures that the field is not read only
export function addPropertySchema(
target,
propertyName: string,
propertySchema: EntitySchemaField,
) {
target[propertyName] = undefined; // This ensures that the field is not read only

if (Object.getOwnPropertyDescriptor(target.constructor, "schema") == null) {
target.constructor.schema = new Map<string, EntitySchemaField>();
}
target.constructor.schema.set(propertySchema.id, propertySchema);
target.constructor.schema.set(propertyName, propertySchema);

return target;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function setupEditComponent<T>(
fromGroupConfig[propertyName] = formControl;
const formGroup = new UntypedFormGroup(fromGroupConfig);
component.formControl = formControl;
component.propertySchema = { id: propertyName };
component.propertySchema = {};
component.formFieldConfig = { id: propertyName };
component.entity = new Entity();
return formGroup;
Expand Down
5 changes: 2 additions & 3 deletions src/app/core/entity/entity-config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ describe("EntityConfigService", () => {

it("should create a new subclass with the schema of the extended", () => {
const schema: EntitySchemaField = {
id: "dynamicProperty",
dataType: "string",
label: "Dynamic Property",
};
Expand All @@ -122,8 +121,8 @@ describe("EntityConfigService", () => {
_id: "entity:DynamicTest",
label: "Dynamic Test Entity",
extends: "Test",
attributes: [schema],
},
attributes: { dynamicProperty: schema },
} as EntityConfig,
]);

service.setupEntitiesFromConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/entity/entity-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class EntityConfigService {
) {
const entityConfig = configAttributes || this.getEntityConfig(entityType);
for (const [key, value] of Object.entries(entityConfig?.attributes ?? {})) {
addPropertySchema(entityType.prototype, { id: key, ...value });
addPropertySchema(entityType.prototype, key, value);
}

// TODO: shall we just assign all properties that are present in the config object?
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/entity/entity-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldConfig } from "./schema/entity-schema-field";
import { EntitySchemaField } from "./schema/entity-schema-field";

/**
* Dynamic configuration for a entity.
Expand All @@ -8,7 +8,7 @@ export interface EntityConfig {
/**
* A list of attributes that will be dynamically added/overwritten to the entity.
*/
attributes?: { [key: string]: FieldConfig };
attributes?: { [key: string]: EntitySchemaField };

/**
* A list of attributes which should be shown when calling the `.toString()` method of this entity.
Expand Down
10 changes: 0 additions & 10 deletions src/app/core/entity/schema/entity-schema-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ import { EntityReferenceRole } from "../../basic-datatypes/entity/entity-referen
* `@DatabaseField(fieldConfig: EntitySchemaField)`
*/
export interface EntitySchemaField {
/**
* The key (property name) on the entity object for this field.
*
* Do not change after initial field creation!
*/
readonly id: string;

/**
* The datatype of this field. This will trigger to matching datatype transformer when saving/loading the entity.
*
Expand Down Expand Up @@ -134,9 +127,6 @@ export interface EntitySchemaField {
anonymize?: "retain" | "retain-anonymized";
}

// TODO: align FormFieldConfig and EntitySchemaField, so that they can directly overwrite each other + allow a simplified config version where id is not explicitly set
export type FieldConfig = Omit<EntitySchemaField, "id">;

/**
* Available placeholder variables that can be used to configure a dynamic default value.
* (e.g. "$now" to set to current date)
Expand Down
2 changes: 0 additions & 2 deletions src/app/core/entity/schema/entity-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ describe("EntitySchemaService", () => {

it("should return correct inner data type", () => {
const enumArraySchema: EntitySchemaField = {
id: null,
dataType: "array",
innerDataType: "configurable-enum",
};
Expand All @@ -131,7 +130,6 @@ describe("EntitySchemaService", () => {
);

const entityArraySchema: EntitySchemaField = {
id: null,
dataType: "entity-array",
};
expect(service.getInnermostDatatype(entityArraySchema)).toBeInstanceOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export class FilterGeneratorService {
): Promise<Filter<T>[]> {
const filters: Filter<T>[] = [];
for (const filterConfig of filterConfigs) {
const schema = entityConstructor.schema.get(filterConfig.id) || {
id: filterConfig.id,
};
const schema = entityConstructor.schema.get(filterConfig.id) || {};
let filter: Filter<T>;
const type = filterConfig.type ?? schema.dataType;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ConfigFieldRaw } from "./config-field.raw";
import { EntityListConfig } from "../../core/entity-list/EntityListConfig";
import { EntityDetailsConfig } from "../../core/entity-details/EntityDetailsConfig";
import { ViewConfig } from "../../core/config/dynamic-routing/view-config.interface";
import { FieldConfig } from "../../core/entity/schema/entity-schema-field";
import { EntitySchemaField } from "../../core/entity/schema/entity-schema-field";

describe("ConfigImportParserService", () => {
let service: ConfigImportParserService;
Expand All @@ -26,7 +26,7 @@ describe("ConfigImportParserService", () => {

function expectToBeParsedIntoEntityConfig(
inputs: ConfigFieldRaw[],
expectedOutputs: { [key: string]: FieldConfig },
expectedOutputs: { [key: string]: EntitySchemaField },
) {
const entityName = "test";
const result = service.parseImportDefinition(inputs, entityName, false);
Expand Down
10 changes: 4 additions & 6 deletions src/app/features/config-setup/config-import-parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ export class ConfigImportParserService {
if (!f?.dataType) {
continue;
}
const field = this.parseFieldDefinition(f, entityName);
entity.attributes[field.id] = field;
delete entity.attributes[field.id]["id"]; // do not save redundant id
const parsedField = this.parseFieldDefinition(f, entityName);
entity.attributes[parsedField.id] = parsedField.schema;
}

const generatedConfig: GeneratedConfig = {};
Expand Down Expand Up @@ -101,13 +100,12 @@ export class ConfigImportParserService {
private parseFieldDefinition(
fieldDef: ConfigFieldRaw,
entityType: string,
): EntitySchemaField {
): { id: string; schema: EntitySchemaField } {
const fieldId =
fieldDef.id ??
ConfigImportParserService.generateIdFromLabel(fieldDef.label);

const schema: EntitySchemaField = {
id: fieldId,
dataType: fieldDef.dataType,
label: fieldDef.label,
description: fieldDef.description,
Expand Down Expand Up @@ -148,7 +146,7 @@ export class ConfigImportParserService {
this.generateOrUpdateDetailsViewConfig(fieldDef, entityType, fieldId);

deleteEmptyProperties(schema);
return schema;
return { id: fieldId, schema: schema };
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/app/features/file/couchdb-file.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe("CouchdbFileService", () => {
dismiss = jasmine.createSpy();
mockSnackbar.openFromComponent.and.returnValue({ dismiss } as any);
Entity.schema.set("testProp", {
id: "testProp",
dataType: FileDatatype.dataType,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ describe("MapPropertiesPopupComponent", () => {

beforeEach(async () => {
Child.schema.set("address", {
id: "address",
label: "Address",
dataType: "location",
});
Child.schema.set("otherAddress", {
id: "otherAddress",
label: "Other address",
dataType: "location",
});
properties[Child.ENTITY_TYPE] = ["address"];
School.schema.set("address", {
id: "address",
label: "School address",
dataType: "location",
});
Expand Down
5 changes: 2 additions & 3 deletions src/app/features/location/map/map.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe("MapComponent", () => {
});

it("should create markers for entities and emit entity when marker is clicked", (done) => {
Child.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });
const child = new Child();
child["address"] = { lat: 1, lon: 1 };
component.entities = [child];
Expand Down Expand Up @@ -111,9 +111,8 @@ describe("MapComponent", () => {

it("should open a popup that allows to change the properties displayed in the map", () => {
const emitSpy = spyOn(component.displayedPropertiesChange, "emit");
Child.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });
Child.schema.set("otherAddress", {
id: "otherAddress",
dataType: "location",
});
const child = new Child();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe("MatchingEntitiesComponent", () => {
}));

it("should create distance column and publish updates", fakeAsync(() => {
Child.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });
component.entity = new Child();
component.columns = [[undefined, "distance"]];
component.leftSide = { entityType: Child };
Expand Down Expand Up @@ -330,12 +330,11 @@ describe("MatchingEntitiesComponent", () => {

it("should update the distance calculation when the selected map properties change", fakeAsync(() => {
Object.assign(component, testConfig);
Child.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });
Child.schema.set("otherAddress", {
id: "otherAddress",
dataType: "location",
});
School.schema.set("address", { id: "address", dataType: "location" });
School.schema.set("address", { dataType: "location" });
const leftEntity = new Child();
leftEntity["address"] = { lat: 52, lon: 14 };
leftEntity["otherAddress"] = { lat: 53, lon: 14 };
Expand Down Expand Up @@ -466,7 +465,7 @@ describe("MatchingEntitiesComponent", () => {

expect(component.mapVisible).toBeFalse();

Child.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });

component.ngOnInit();
tick();
Expand All @@ -486,8 +485,8 @@ describe("MatchingEntitiesComponent", () => {
leftSide: { entityType: "Child", columns: ["name", "distance"] },
onMatch: testConfig.onMatch,
};
Child.schema.set("address", { id: "address", dataType: "location" });
School.schema.set("address", { id: "address", dataType: "location" });
Child.schema.set("address", { dataType: "location" });
School.schema.set("address", { dataType: "location" });

const configCopy = JSON.parse(JSON.stringify(config));
routeData.next({ config: configCopy });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { EntitySchemaField } from "../../../core/entity/schema/entity-schema-fie
import { importProvidersFrom } from "@angular/core";

const addressSchema: EntitySchemaField = {
id: "address",
label: "Address",
dataType: "location",
};
Expand Down

0 comments on commit 3df2199

Please sign in to comment.