-
Notifications
You must be signed in to change notification settings - Fork 352
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
Fixes #1817: support key alias #3145
Open
xuzhg
wants to merge
1
commit into
main
Choose a base branch
from
issue1817_SupportKeyAlias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
30 changes: 30 additions & 0 deletions
30
src/Microsoft.OData.Edm/Csdl/Semantics/BadElements/UnresolvedPropertyRef.cs
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,30 @@ | ||||||||
//--------------------------------------------------------------------- | ||||||||
// <copyright file="UnresolvedProperty.cs" company="Microsoft"> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be
Suggested change
|
||||||||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||||||||
// </copyright> | ||||||||
//--------------------------------------------------------------------- | ||||||||
|
||||||||
using Microsoft.OData.Edm.Validation; | ||||||||
|
||||||||
namespace Microsoft.OData.Edm.Csdl.CsdlSemantics | ||||||||
{ | ||||||||
internal class UnresolvedPropertyRef : BadElement, IUnresolvedElement, IEdmPropertyRef | ||||||||
{ | ||||||||
public UnresolvedPropertyRef(IEdmStructuredType declaringType, string name, string alias, EdmLocation location) | ||||||||
: base(new EdmError[] | ||||||||
{ | ||||||||
new EdmError(location, EdmErrorCode.BadUnresolvedPropertyRef, | ||||||||
Edm.Strings.Bad_UnresolvedPropertyRef(declaringType.FullTypeName(), name, alias)) | ||||||||
}) | ||||||||
{ | ||||||||
} | ||||||||
|
||||||||
public IEdmStructuralProperty ReferencedProperty => null; | ||||||||
|
||||||||
public string PropertyAlias { get; } | ||||||||
|
||||||||
public string Name { get; } | ||||||||
|
||||||||
public IEdmPathExpression Path { get; } | ||||||||
} | ||||||||
} |
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||||
using System.Collections.Generic; | ||||||||
using System.Linq; | ||||||||
using Microsoft.OData.Edm.Csdl.Parsing.Ast; | ||||||||
using Microsoft.OData.Edm.Vocabularies; | ||||||||
|
||||||||
namespace Microsoft.OData.Edm.Csdl.CsdlSemantics | ||||||||
{ | ||||||||
|
@@ -23,8 +24,8 @@ internal class CsdlSemanticsEntityTypeDefinition : CsdlSemanticsStructuredTypeDe | |||||||
private static readonly Func<CsdlSemanticsEntityTypeDefinition, IEdmEntityType> ComputeBaseTypeFunc = (me) => me.ComputeBaseType(); | ||||||||
private static readonly Func<CsdlSemanticsEntityTypeDefinition, IEdmEntityType> OnCycleBaseTypeFunc = (me) => new CyclicEntityType(me.GetCyclicBaseTypeName(me.entity.BaseTypeName), me.Location); | ||||||||
|
||||||||
private readonly Cache<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmStructuralProperty>> declaredKeyCache = new Cache<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmStructuralProperty>>(); | ||||||||
private static readonly Func<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmStructuralProperty>> ComputeDeclaredKeyFunc = (me) => me.ComputeDeclaredKey(); | ||||||||
private readonly Cache<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmPropertyRef>> declaredKeyCache = new Cache<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmPropertyRef>>(); | ||||||||
private static readonly Func<CsdlSemanticsEntityTypeDefinition, IEnumerable<IEdmPropertyRef>> ComputeDeclaredKeyFunc = (me) => me.ComputeDeclaredKey(); | ||||||||
|
||||||||
public CsdlSemanticsEntityTypeDefinition(CsdlSemanticsSchema context, CsdlEntityType entity) | ||||||||
: base(context, entity) | ||||||||
|
@@ -72,6 +73,14 @@ public bool HasStream | |||||||
} | ||||||||
|
||||||||
public IEnumerable<IEdmStructuralProperty> DeclaredKey | ||||||||
{ | ||||||||
get | ||||||||
{ | ||||||||
return this.DeclaredKeyRef?.Select(x => x.ReferencedProperty); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public IEnumerable<IEdmPropertyRef> DeclaredKeyRef | ||||||||
{ | ||||||||
get | ||||||||
{ | ||||||||
|
@@ -105,17 +114,24 @@ private IEdmEntityType ComputeBaseType() | |||||||
return null; | ||||||||
} | ||||||||
|
||||||||
private IEnumerable<IEdmStructuralProperty> ComputeDeclaredKey() | ||||||||
private IEnumerable<IEdmPropertyRef> ComputeDeclaredKey() | ||||||||
{ | ||||||||
if (this.entity.Key != null) | ||||||||
{ | ||||||||
List<IEdmStructuralProperty> key = new List<IEdmStructuralProperty>(); | ||||||||
List<IEdmPropertyRef> key = new List<IEdmPropertyRef>(); | ||||||||
foreach (CsdlPropertyReference keyProperty in this.entity.Key.Properties) | ||||||||
{ | ||||||||
IEdmStructuralProperty structuralProperty = this.FindProperty(keyProperty.PropertyName) as IEdmStructuralProperty; | ||||||||
IEdmStructuralProperty structuralProperty = FindKeyProperty(keyProperty.PropertyName); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or define |
||||||||
if (structuralProperty != null) | ||||||||
{ | ||||||||
key.Add(structuralProperty); | ||||||||
if (keyProperty.PropertyAlias != null) | ||||||||
{ | ||||||||
key.Add(new EdmPropertyRef(structuralProperty, new EdmPropertyPathExpression(keyProperty.PropertyName), keyProperty.PropertyAlias)); | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
key.Add(new EdmPropertyRef(structuralProperty)); | ||||||||
} | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
|
@@ -125,11 +141,11 @@ private IEnumerable<IEdmStructuralProperty> ComputeDeclaredKey() | |||||||
structuralProperty = this.DeclaredProperties.FirstOrDefault(p => p.Name == keyProperty.PropertyName) as IEdmStructuralProperty; | ||||||||
if (structuralProperty != null) | ||||||||
{ | ||||||||
key.Add(structuralProperty); | ||||||||
key.Add(new EdmPropertyRef(structuralProperty)); | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
key.Add(new UnresolvedProperty(this, keyProperty.PropertyName, this.Location)); | ||||||||
key.Add(new UnresolvedPropertyRef(this, keyProperty.PropertyName, keyProperty.PropertyAlias, this.Location)); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
@@ -139,5 +155,71 @@ private IEnumerable<IEdmStructuralProperty> ComputeDeclaredKey() | |||||||
|
||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
private IEdmStructuralProperty FindKeyProperty(string nameOrPath) | ||||||||
{ | ||||||||
if (string.IsNullOrWhiteSpace(nameOrPath)) | ||||||||
{ | ||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
string[] segments = nameOrPath.Split('/'); | ||||||||
if (segments.Length == 1) | ||||||||
{ | ||||||||
return FindPropertyOnType(this, nameOrPath) as IEdmStructuralProperty; | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
// OData spec says "The value of Name is a path expression leading to a primitive property." | ||||||||
// The segment in a path expression could be single value property name, collection value property name, type cast, ... | ||||||||
// However, for the key property reference path expression: | ||||||||
// 1) Collection value property name segment is invalid, right? | ||||||||
// 2) Type cast? reference a key on the sub type? it's valid but So far, let's skip it. | ||||||||
IEdmStructuredType edmStructuredType = this; | ||||||||
for (int i = 0; i < segments.Length; ++i) | ||||||||
{ | ||||||||
if (edmStructuredType == null) | ||||||||
{ | ||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
string segment = segments[i]; | ||||||||
if (segment.Contains('.', StringComparison.Ordinal)) | ||||||||
{ | ||||||||
// a type cast, let's skip it for key reference now. | ||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
IEdmProperty edmProperty = FindPropertyOnType(edmStructuredType, segment); | ||||||||
if (i == segments.Length - 1) | ||||||||
{ | ||||||||
return edmProperty as IEdmStructuralProperty; | ||||||||
} | ||||||||
else if (edmProperty != null) | ||||||||
{ | ||||||||
// If the property is a collection value, let's move on using the element type of this collection | ||||||||
edmStructuredType = edmProperty.Type.ToStructuredType(); | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
return null; | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
private static IEdmProperty FindPropertyOnType(IEdmStructuredType structuredType, string name) | ||||||||
{ | ||||||||
IEdmProperty property = structuredType.FindProperty(name); | ||||||||
|
||||||||
if (property == null) | ||||||||
{ | ||||||||
property = structuredType.DeclaredProperties.FirstOrDefault(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); | ||||||||
} | ||||||||
|
||||||||
return property; | ||||||||
} | ||||||||
} | ||||||||
} |
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
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
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.
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.
What if the
$Key
is in lowercase:$key