Skip to content

Commit

Permalink
handle default properties (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioUyaguari authored Feb 13, 2024
1 parent a8b4b20 commit a3730a2
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-eels-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': minor
---

Add handling of `defaultValues` for class properties.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { hashArray, type Hashable } from '@finos/legend-shared';
import {
CORE_HASH_STRUCTURE,
hashObjectWithoutSourceInformation,
} from '../../../../../../../graph/Core_HashUtils.js';

export class V1_DefaultValue implements Hashable {
/**
* Studio does not process value specification, they are left in raw JSON form
*
* @discrepancy model
*/
value!: object;

get hashCode(): string {
return hashArray([
CORE_HASH_STRUCTURE.PROPERTY_DEFAULT_VALUE,
hashObjectWithoutSourceInformation(this.value),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import { CORE_HASH_STRUCTURE } from '../../../../../../../graph/Core_HashUtils.j
import type { V1_Multiplicity } from './V1_Multiplicity.js';
import type { V1_StereotypePtr } from '../../../model/packageableElements/domain/V1_StereotypePtr.js';
import type { V1_TaggedValue } from '../../../model/packageableElements/domain/V1_TaggedValue.js';
import type { V1_DefaultValue } from './V1_DefaultValue.js';

export class V1_Property implements Hashable {
name!: string;
type!: string;
multiplicity!: V1_Multiplicity;
aggregation?: string | undefined;
defaultValue: V1_DefaultValue | undefined;
stereotypes: V1_StereotypePtr[] = [];
taggedValues: V1_TaggedValue[] = [];

Expand All @@ -35,6 +37,7 @@ export class V1_Property implements Hashable {
this.multiplicity,
this.type,
this.aggregation ?? '',
this.defaultValue ?? '',
hashArray(this.stereotypes),
hashArray(this.taggedValues),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { UnsupportedOperationError } from '@finos/legend-shared';
import { V1_FunctionStoreTestData } from '../../../model/packageableElements/function/test/V1_FunctionStoreTestData.js';
import { V1_transformEmbeddedData } from './V1_DataElementTransformer.js';
import { V1_transformTestAssertion } from './V1_TestTransformer.js';
import { V1_DefaultValue } from '../../../model/packageableElements/domain/V1_DefaultValue.js';

export const V1_transformProfile = (element: Profile): V1_Profile => {
const profile = new V1_Profile();
Expand Down Expand Up @@ -176,6 +177,11 @@ const transformProperty = (element: Property): V1_Property => {
property.type =
element.genericType.ownerReference.valueForSerialization ?? '';
property.aggregation = element.aggregation;
if (element.defaultValue) {
const defaultVal = new V1_DefaultValue();
defaultVal.value = element.defaultValue.value;
property.defaultValue = defaultVal;
}
return property;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
} from '../../../../../../../../graph/metamodel/pure/packageableElements/function/test/FunctionTest.js';
import { V1_buildTestAssertion } from './V1_TestBuilderHelper.js';
import type { TestSuite } from '../../../../../../../../graph/metamodel/pure/test/Test.js';
import { DefaultValue } from '../../../../../../../../graph/metamodel/pure/packageableElements/domain/DefaultValue.js';

export const V1_buildTaggedValue = (
taggedValue: V1_TaggedValue,
Expand Down Expand Up @@ -219,6 +220,11 @@ export const V1_buildProperty = (
pureProperty.stereotypes = property.stereotypes
.map((stereotype) => context.resolveStereotype(stereotype))
.filter(isNonNullable);
if (property.defaultValue) {
const defautVal = new DefaultValue();
defautVal.value = property.defaultValue.value;
pureProperty.defaultValue = defautVal;
}
pureProperty.taggedValues = property.taggedValues
.map((taggedValue) => V1_buildTaggedValue(taggedValue, context))
.filter(isNonNullable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
} from './V1_TestSerializationHelper.js';
import type { PureProtocolProcessorPlugin } from '../../../../PureProtocolProcessorPlugin.js';
import type { V1_TestSuite } from '../../../model/test/V1_TestSuite.js';
import { V1_DefaultValue } from '../../../model/packageableElements/domain/V1_DefaultValue.js';

export const V1_CLASS_ELEMENT_PROTOCOL_TYPE = 'class';
export const V1_PROFILE_ELEMENT_PROTOCOL_TYPE = 'profile';
Expand Down Expand Up @@ -181,8 +182,12 @@ export const V1_snowflakeAppModelSchema = createModelSchema(V1_SnowflakeApp, {

// ------------------------------------- Class -------------------------------------

export const V1_defaultValueModelSchema = createModelSchema(V1_DefaultValue, {
value: raw(),
});
export const V1_propertyModelSchema = createModelSchema(V1_Property, {
aggregation: optional(primitive()),
defaultValue: optional(usingModelSchema(V1_defaultValueModelSchema)),
multiplicity: usingModelSchema(V1_multiplicityModelSchema),
name: primitive(),
stereotypes: customListWithSchema(V1_stereotypePtrModelSchema, {
Expand Down
1 change: 1 addition & 0 deletions packages/legend-graph/src/graph/Core_HashUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export enum CORE_HASH_STRUCTURE {
ENUM_VALUE = 'ENUM_VALUE',
CLASS = 'CLASS',
PROPERTY = 'PROPERTY',
PROPERTY_DEFAULT_VALUE = 'PROPERTY_DEFAULT_VALUE',
PROPERTY_POINTER = 'PROPERTY_POINTER',
MULTIPLICITY = 'MULTIPLICITY',
CONSTRAINT = 'CONSTRAINT',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { hashArray, type Hashable } from '@finos/legend-shared';
import {
CORE_HASH_STRUCTURE,
hashObjectWithoutSourceInformation,
} from '../../../../Core_HashUtils.js';

export class DefaultValue implements Hashable {
/**
* Studio does not process value specification, they are left in raw JSON form
*
* @discrepancy model
*/
value!: object;

get hashCode(): string {
return hashArray([
CORE_HASH_STRUCTURE.PROPERTY_DEFAULT_VALUE,
hashObjectWithoutSourceInformation(this.value),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class DerivedProperty
*/
parameters?: object | undefined;

defaultValue = undefined;

constructor(
name: string,
multiplicity: Multiplicity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { AnnotatedElement } from './AnnotatedElement.js';
import type { TaggedValue } from './TaggedValue.js';
import type { StereotypeReference } from './StereotypeReference.js';
import type { AggregationKind } from './AggregationKind.js';
import type { DefaultValue } from './DefaultValue.js';

export class Property implements AbstractProperty, AnnotatedElement, Hashable {
readonly _UUID = uuid();
Expand All @@ -32,6 +33,7 @@ export class Property implements AbstractProperty, AnnotatedElement, Hashable {
multiplicity: Multiplicity;
genericType: GenericTypeReference;
aggregation?: AggregationKind | undefined;
defaultValue: DefaultValue | undefined;
stereotypes: StereotypeReference[] = [];
taggedValues: TaggedValue[] = [];

Expand All @@ -54,6 +56,7 @@ export class Property implements AbstractProperty, AnnotatedElement, Hashable {
this.multiplicity,
this.genericType.ownerReference.valueForSerialization ?? '',
this.aggregation ?? '',
this.defaultValue ?? '',
hashArray(this.stereotypes.map((val) => val.pointerHashCode)),
hashArray(this.taggedValues),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class model::Student
{
name: String[1];
age: Integer[1];
name: String[1] = 'Nicole';
age: Integer[1] = 1;
(composite) profile: model::StudentProfile[1];
(composite) theses: model::Thesis[*];
(shared) locker: model::Locker[1];
Expand Down

0 comments on commit a3730a2

Please sign in to comment.