-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SALTO-582: custom obj translation reference fields and validation rules #813
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d17a0cc
custom obj translation refrence fields and validation rules
hadard d1b24d2
CR+
hadard 9aac21f
move tests
hadard afb212c
e2e improvments
hadard 9323598
take element id from parent annotation
hadard a79fe11
more improv
hadard e3a7eb4
fix tests
hadard 3c34d47
CR - renames
hadard 4c74c1a
more more CR ;)
hadard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/salesforce-adapter/src/filters/custom_object_translation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* 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 wu from 'wu' | ||
import { | ||
Element, ElemID, findInstances, InstanceElement, INSTANCE_ANNOTATIONS, ReferenceExpression, | ||
} from '@salto-io/adapter-api' | ||
import { collections } from '@salto-io/lowerdash' | ||
import _ from 'lodash' | ||
import { FilterWith } from '../filter' | ||
import { generateApiNameToCustomObject, apiNameParts, getInstancesOfMetadataType } from './utils' | ||
import { SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE, VALIDATION_RULES_METADATA_TYPE } from '../constants' | ||
|
||
const { makeArray } = collections.array | ||
|
||
export const CUSTOM_OBJ_METADATA_TYPE_ID = new ElemID(SALESFORCE, | ||
CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE) | ||
|
||
const FIELDS = 'fields' | ||
const NAME = 'name' | ||
const VALIDATION_RULES = 'validationRules' | ||
|
||
/** | ||
* This filter change CustomObjectTranslation logical references to fields and validation rules to | ||
* Salto referenes | ||
*/ | ||
const filterCreator = (): FilterWith<'onFetch'> => ({ | ||
onFetch: async (elements: Element[]) => { | ||
const ruleObj = (rule: InstanceElement): string => apiNameParts(rule)[0] | ||
const ruleShortName = (rule: InstanceElement): string => apiNameParts(rule)[1] | ||
|
||
const apiNameToCustomObject = generateApiNameToCustomObject(elements) | ||
const apiNameToRules = _.groupBy( | ||
getInstancesOfMetadataType(elements, VALIDATION_RULES_METADATA_TYPE), | ||
ruleObj | ||
) | ||
|
||
wu(findInstances(elements, CUSTOM_OBJ_METADATA_TYPE_ID)) | ||
.forEach(customTranslation => { | ||
const customObjApiName = apiNameParts(customTranslation)[0] | ||
ori-moisis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Add parent annotation | ||
const customObj = apiNameToCustomObject.get(customObjApiName) | ||
if (customObj) { | ||
customTranslation.annotate({ | ||
[INSTANCE_ANNOTATIONS.PARENT]: new ReferenceExpression(customObj.elemID), | ||
}) | ||
} | ||
|
||
// Change fields to reference | ||
makeArray(customTranslation.value[FIELDS]).forEach(field => { | ||
const customField = customObj?.fields[field[NAME]] | ||
if (customField) { | ||
field[NAME] = new ReferenceExpression(customField.elemID) | ||
} | ||
}) | ||
|
||
// Change validation rules to refs | ||
const objRules = apiNameToRules[customObjApiName] | ||
makeArray(customTranslation.value[VALIDATION_RULES]).forEach(rule => { | ||
const ruleInstance = objRules?.find(r => _.isEqual(ruleShortName(r), rule[NAME])) | ||
if (ruleInstance) { | ||
rule[NAME] = new ReferenceExpression(ruleInstance.elemID) | ||
} | ||
}) | ||
}) | ||
}, | ||
}) | ||
|
||
export default filterCreator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/salesforce-adapter/test/filters/custom_object_translation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* 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 _ from 'lodash' | ||
import { | ||
ObjectType, InstanceElement, Field, BuiltinTypes, ElemID, ReferenceExpression, | ||
} from '@salto-io/adapter-api' | ||
import filterCreator from '../../src/filters/custom_object_translation' | ||
import { | ||
SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE, METADATA_TYPE, CUSTOM_OBJECT, API_NAME, | ||
VALIDATION_RULES_METADATA_TYPE, | ||
INSTANCE_FULL_NAME_FIELD, | ||
} from '../../src/constants' | ||
import { FilterWith } from '../../src/filter' | ||
|
||
describe('custom object translation filter', () => { | ||
const customObjName = 'MockCustomObject' | ||
const customFieldName = 'MockField' | ||
const customObjElemID = new ElemID(SALESFORCE, customObjName) | ||
const customObject = new ObjectType( | ||
{ | ||
elemID: customObjElemID, | ||
fields: { | ||
[customFieldName]: new Field(customObjElemID, customFieldName, BuiltinTypes.STRING), | ||
}, | ||
annotations: { | ||
[METADATA_TYPE]: CUSTOM_OBJECT, | ||
[API_NAME]: customObjName, | ||
}, | ||
} | ||
) | ||
const validationRuleName = 'Last_Price' | ||
const validationRuleType = new ObjectType({ | ||
elemID: new ElemID(SALESFORCE, VALIDATION_RULES_METADATA_TYPE), | ||
annotations: { [METADATA_TYPE]: VALIDATION_RULES_METADATA_TYPE }, | ||
}) | ||
const validationRuleInstance = new InstanceElement( | ||
`${customObjName}_${validationRuleName}`, | ||
validationRuleType, | ||
{ [INSTANCE_FULL_NAME_FIELD]: `${customObjName}.${validationRuleName}` } | ||
) | ||
const objTranslationType = new ObjectType({ | ||
elemID: new ElemID(SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE), | ||
}) | ||
const objTranslationInstance = new InstanceElement( | ||
`${customObjName}-en_US`, | ||
objTranslationType, | ||
{ | ||
[INSTANCE_FULL_NAME_FIELD]: `${customObjName}-en_US`, | ||
fields: [{ name: customFieldName }, { name: 'not-exists' }], | ||
validationRules: [{ name: validationRuleName }, { name: 'not-exists' }], | ||
}, | ||
) | ||
|
||
describe('on fetch', () => { | ||
let postFilter: InstanceElement | ||
|
||
beforeAll(async () => { | ||
const filter = filterCreator() as FilterWith<'onFetch'> | ||
const testElements = [ | ||
objTranslationInstance.clone(), | ||
customObject.clone(), | ||
objTranslationType.clone(), | ||
validationRuleType.clone(), | ||
validationRuleInstance.clone(), | ||
] | ||
await filter.onFetch(testElements) | ||
postFilter = testElements[0] as InstanceElement | ||
}) | ||
|
||
describe('fields reference', () => { | ||
it('should transform fields to reference', async () => { | ||
expect(postFilter.value.fields[0].name) | ||
.toEqual(new ReferenceExpression(customObject.fields[customFieldName].elemID)) | ||
}) | ||
it('should keep name as is if no referenced field was found', async () => { | ||
expect(postFilter.value.fields[1].name).toBe('not-exists') | ||
}) | ||
}) | ||
|
||
describe('validation rules reference', () => { | ||
it('should transform validation rules to reference', async () => { | ||
expect(postFilter.value.validationRules[0].name) | ||
.toEqual(new ReferenceExpression(validationRuleInstance.elemID)) | ||
}) | ||
it('should keep name as is if no referenced validation rules was found', async () => { | ||
expect(postFilter.value.validationRules[1].name).toBe('not-exists') | ||
}) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems unrelated to this PR.
maybe a separate commit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I wonder if it was actually needed so it should be fixed instead of removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per my understanding, it should be removed as “nohoist” enables workspaces to consume 3rd-party libraries not yet compatible with its hoisting scheme." and AFAIK we don't have such a case.
Notice that practically it's not working for a long time (not sure if it means something as vscode extension is also not working)
@royra @roironn WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's needed for packaging of the vscode extension: microsoft/vscode-vsce#300
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok so I'll change
salto-vscode
->vscode
instead of removing