-
Notifications
You must be signed in to change notification settings - Fork 26
WIP: Let validators opt in to run on clean data #1473
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4634c80
Let validators opt in to run on clean data
ivarne 86cb255
Follow coderabit suggestions
ivarne d4592c0
More coderabit advice and add more tests
ivarne d4f0639
Fix broken tests
ivarne 9168536
Another batch of coderabbit suggestions
ivarne e2d601c
Fix validation of unique ValidationSource after codeRabbit suggestion
ivarne 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
148 changes: 148 additions & 0 deletions
148
src/Altinn.App.Core/Internal/Data/CleanInstanceDataAccessor.cs
This file contains hidden or 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,148 @@ | ||
| using Altinn.App.Core.Configuration; | ||
| using Altinn.App.Core.Features; | ||
| using Altinn.App.Core.Helpers; | ||
| using Altinn.App.Core.Internal.App; | ||
| using Altinn.App.Core.Internal.Expressions; | ||
| using Altinn.App.Core.Internal.Texts; | ||
| using Altinn.App.Core.Models; | ||
| using Altinn.App.Core.Models.Layout; | ||
| using Altinn.Platform.Storage.Interface.Models; | ||
|
|
||
| namespace Altinn.App.Core.Internal.Data; | ||
|
|
||
| internal class CleanInstanceDataAccessor : IInstanceDataAccessor | ||
| { | ||
| private readonly IInstanceDataAccessor _dataAccessor; | ||
| private readonly string? _taskId; | ||
| private readonly IAppResources _appResources; | ||
| private readonly FrontEndSettings _frontEndSettings; | ||
| private readonly RowRemovalOption _rowRemovalOption; | ||
| private readonly string? _language; | ||
| private readonly ITranslationService _translationService; | ||
| private readonly Telemetry? _telemetry; | ||
|
|
||
| public CleanInstanceDataAccessor( | ||
| IInstanceDataAccessor dataAccessor, | ||
| string? taskId, | ||
| IAppResources appResources, | ||
| ITranslationService translationService, | ||
| FrontEndSettings frontEndSettings, | ||
| RowRemovalOption rowRemovalOption, | ||
| string? language, | ||
| Telemetry? telemetry | ||
| ) | ||
| { | ||
| _dataAccessor = dataAccessor; | ||
| _taskId = taskId; | ||
| _appResources = appResources; | ||
| _frontEndSettings = frontEndSettings; | ||
| _rowRemovalOption = rowRemovalOption; | ||
| _language = language; | ||
| _telemetry = telemetry; | ||
| _translationService = translationService; | ||
|
|
||
| LayoutModel? layouts = taskId is not null ? appResources.GetLayoutModelForTask(taskId) : null; | ||
| if (layouts is null) | ||
| { | ||
| _hiddenFieldsTask = new(() => Task.FromResult(new List<DataReference>())); | ||
| } | ||
| else | ||
| { | ||
| var state = new LayoutEvaluatorState( | ||
| dataAccessor, | ||
| layouts, | ||
| translationService, | ||
| frontEndSettings, | ||
| gatewayAction: null, | ||
| language | ||
| ); | ||
| _hiddenFieldsTask = new(() => | ||
| { | ||
| using var activity = telemetry?.StartRemoveHiddenDataForValidation(); | ||
| return LayoutEvaluator.GetHiddenFieldsForRemoval(state); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private readonly DataElementCache<IFormDataWrapper> _cleanCache = new(); | ||
|
|
||
| private readonly Lazy<Task<List<DataReference>>> _hiddenFieldsTask; | ||
|
|
||
| public Instance Instance => _dataAccessor.Instance; | ||
|
|
||
| public IReadOnlyCollection<DataType> DataTypes => _dataAccessor.DataTypes; | ||
|
|
||
| public async Task<object> GetFormData(DataElementIdentifier dataElementIdentifier) | ||
| { | ||
| return (await GetFormDataWrapper(dataElementIdentifier)).BackingData<object>(); | ||
| } | ||
|
|
||
| public async Task<IFormDataWrapper> GetFormDataWrapper(DataElementIdentifier dataElementIdentifier) | ||
| { | ||
| var dataWrapper = await _cleanCache.GetOrCreate( | ||
| dataElementIdentifier, | ||
| async () => | ||
| { | ||
| var data = await _dataAccessor.GetFormDataWrapper(dataElementIdentifier).ConfigureAwait(false); | ||
| var hiddenFields = await _hiddenFieldsTask.Value.ConfigureAwait(false); | ||
| return CleanModel(data.Copy(), dataElementIdentifier, hiddenFields, _rowRemovalOption); | ||
| } | ||
| ); | ||
|
|
||
| return dataWrapper.Copy(); | ||
| } | ||
|
|
||
| private static IFormDataWrapper CleanModel( | ||
| IFormDataWrapper data, | ||
| DataElementIdentifier dataElementIdentifier, | ||
| List<DataReference> hiddenFields, | ||
| RowRemovalOption rowRemovalOption | ||
| ) | ||
| { | ||
| foreach (var dataReference in hiddenFields) | ||
| { | ||
| if (dataReference.DataElementIdentifier != dataElementIdentifier) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Note that the paths for lists is in reverse order from GetHiddenFieldsForRemoval, so we can remove them here in order | ||
| data.RemoveField(dataReference.Field, rowRemovalOption); | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| public IInstanceDataAccessor GetCleanAccessor(RowRemovalOption rowRemovalOption = RowRemovalOption.SetToNull) | ||
| { | ||
| if (rowRemovalOption == _rowRemovalOption) | ||
| { | ||
| return this; | ||
| } | ||
| return new CleanInstanceDataAccessor( | ||
| _dataAccessor, | ||
| _taskId, | ||
| _appResources, | ||
| _translationService, | ||
| _frontEndSettings, | ||
| rowRemovalOption, | ||
| _language, | ||
| _telemetry | ||
| ); | ||
| } | ||
|
|
||
| public IInstanceDataAccessor GetPreviousDataAccessor() | ||
| { | ||
| return _dataAccessor.GetPreviousDataAccessor(); | ||
| } | ||
|
|
||
| public async Task<ReadOnlyMemory<byte>> GetBinaryData(DataElementIdentifier dataElementIdentifier) | ||
| { | ||
| return await _dataAccessor.GetBinaryData(dataElementIdentifier); | ||
| } | ||
|
|
||
| public DataElement GetDataElement(DataElementIdentifier dataElementIdentifier) | ||
| { | ||
| return _dataAccessor.GetDataElement(dataElementIdentifier); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.