Skip to content
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

EdmLib Vocabulary Annotation Default Values #2173

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Microsoft.OData.Edm/Csdl/Parsing/Ast/CsdlAnnotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public CsdlAnnotation(string term, string qualifier, CsdlExpressionBase expressi
this.term = term;
}

/// <summary>
/// Gets the expression, it could be null if the annotation uses the term's default.
/// </summary>
public CsdlExpressionBase Expression
{
get { return this.expression; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,26 @@ public IEdmExpression Value
get { return this.valueCache.GetValue(this, ComputeValueFunc, null); }
}

/// <summary>
/// Gets whether the annotation uses a default value
/// </summary>
internal bool UseDefault
{
get { return this.Annotation.Expression == null; }
}

protected IEdmTerm ComputeTerm()
{
return this.model.FindTerm(this.Annotation.Term) ?? new UnresolvedVocabularyTerm(this.model.ReplaceAlias(this.Annotation.Term));
}

private IEdmExpression ComputeValue()
{
if (this.UseDefault)
{
return Term.GetDefaultValueExpression();
}

IEdmTypeReference termType = Term is UnresolvedVocabularyTerm ? null : Term.Type;
CsdlExpressionBase adjustedExpression = AdjustStringConstantUsingTermType((this.Annotation).Expression, termType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ internal override void WriteVocabularyAnnotationElementHeader(IEdmVocabularyAnno

if (isInline)
{
// In JSON, we always write the annotation value.
this.WriteInlineExpression(annotation.Value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,23 @@ internal override void WriteCollectionTypeElementHeader(IEdmCollectionType colle
}
}

internal static bool IsUsingDefaultValue(IEdmVocabularyAnnotation annotation)
{
EdmVocabularyAnnotation edmAnnotation = annotation as EdmVocabularyAnnotation;
if (edmAnnotation != null)
{
return edmAnnotation.UseDefault;
}

CsdlSemanticsVocabularyAnnotation csdlAnnotation = annotation as CsdlSemanticsVocabularyAnnotation;
if (csdlAnnotation != null)
{
return csdlAnnotation.UseDefault;
}

return false;
}

internal override void WriteInlineExpression(IEdmExpression expression)
{
IEdmPathExpression pathExpression = expression as IEdmPathExpression;
Expand Down Expand Up @@ -502,8 +519,10 @@ internal override void WriteVocabularyAnnotationElementHeader(IEdmVocabularyAnno
this.xmlWriter.WriteStartElement(CsdlConstants.Element_Annotation);
this.WriteRequiredAttribute(CsdlConstants.Attribute_Term, annotation.Term, this.TermAsXml);
this.WriteOptionalAttribute(CsdlConstants.Attribute_Qualifier, annotation.Qualifier, EdmValueWriter.StringAsXml);
if (isInline)

if (isInline && !IsUsingDefaultValue(annotation))
{
// in xml format, we can (should) skip writing the expression value if it matches the term default value.
this.WriteInlineExpression(annotation.Value);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.OData.Edm/Microsoft.OData.Edm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ internal sealed class EdmRes {
internal const string TimeOfDay_ConvertErrorFromTimeSpan = "TimeOfDay_ConvertErrorFromTimeSpan";
internal const string TimeOfDay_InvalidParsingString = "TimeOfDay_InvalidParsingString";
internal const string TimeOfDay_InvalidCompareToTarget = "TimeOfDay_InvalidCompareToTarget";
internal const string EdmVocabularyAnnotations_DidNotFindDefaultValue = "EdmVocabularyAnnotations_DidNotFindDefaultValue";
internal const string EdmVocabularyAnnotations_TermTypeNotSupported = "EdmVocabularyAnnotations_TermTypeNotSupported";

static EdmRes loader = null;
ResourceManager resources;
Expand Down
8 changes: 5 additions & 3 deletions src/Microsoft.OData.Edm/Microsoft.OData.Edm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ EdmParseException_ErrorsEncounteredInEdmx=Encountered the following errors when
ValueParser_InvalidBoolean=The value '{0}' is not a valid boolean. The value must be 'true' or 'false'.
ValueParser_InvalidInteger=The value '{0}' is not a valid integer. The value must be a valid 32 bit integer.
ValueParser_InvalidLong=The value '{0}' is not a valid integer. The value must be a valid 64 bit integer.
ValueParser_InvalidFloatingPoint=The value '{0}' is not a valid floating point value.
ValueParser_InvalidFloatingPoint=The value '{0}' is not a valid floating point value.
ValueParser_InvalidMaxLength=The value '{0}' is not a valid integer. The value must be a valid 32 bit integer or 'Max'.
ValueParser_InvalidSrid=The value '{0}' is not a valid SRID. The value must either be a 32 bit integer or 'Variable'.
ValueParser_InvalidScale=The value '{0}' is not a valid scale. The value must either be a 32 bit integer or 'Variable'.
ValueParser_InvalidGuid=The value '{0}' is not a valid Guid.
ValueParser_InvalidGuid=The value '{0}' is not a valid Guid.
ValueParser_InvalidDecimal=The value '{0}' is not a valid decimal.
ValueParser_InvalidDateTimeOffset=The value '{0}' is not a valid date time offset value.
ValueParser_InvalidDateTime=The value '{0}' is not a valid date time value.
Expand Down Expand Up @@ -336,4 +336,6 @@ TimeOfDay_InvalidTimeOfDayParameters=The Hour '{0}', Minute '{1}', Second '{2}'
TimeOfDay_TicksOutOfRange=The ticks value '{0}' is out of representable TimeOfDay range.
TimeOfDay_ConvertErrorFromTimeSpan=The TimeSpan value '{0}' is out of representable TimeOfDay range.
TimeOfDay_InvalidParsingString=String '{0}' was not recognized as a valid TimeOfDay.
TimeOfDay_InvalidCompareToTarget=Target object '{0}' is not an instance with type of TimeOfDay.
TimeOfDay_InvalidCompareToTarget=Target object '{0}' is not an instance with type of TimeOfDay.
EdmVocabularyAnnotations_DidNotFindDefaultValue=Annotation expressions must specify a value or use a term with a specified default value. Cannot find a default value for the given annotation term, '{0}'.
EdmVocabularyAnnotations_TermTypeNotSupported=Term type '{0}' is not supported for value retrieval.
53 changes: 40 additions & 13 deletions src/Microsoft.OData.Edm/Parameterized.Microsoft.OData.Edm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,15 +1583,15 @@ internal static string Serializer_SingleFileExpected
/// A string like "Unknown Edm version '{0}'."
/// </summary>
internal static string Serializer_UnknownEdmVersion(object p0)
{
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.Serializer_UnknownEdmVersion, p0);
}

/// <summary>
/// A string like "Unknown Edmx version '{0}'."
/// </summary>
internal static string Serializer_UnknownEdmxVersion(object p0)
{
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.Serializer_UnknownEdmxVersion, p0);
}

Expand Down Expand Up @@ -1955,77 +1955,88 @@ internal static string CsdlSemantics_DuplicateAlias(object p0, object p1)
/// <summary>
/// A string like "An unexpected '{0}' value kind was found when parsing the JSON path '{1}'. A '{2}' value kind was expected."
/// </summary>
internal static string CsdlJsonParser_UnexpectedJsonValueKind(object p0, object p1, object p2) {
internal static string CsdlJsonParser_UnexpectedJsonValueKind(object p0, object p1, object p2)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_UnexpectedJsonValueKind, p0, p1, p2);
}

/// <summary>
/// A string like "A member '{0}' is missing when parsing the JSON path '{1}'."
/// </summary>
internal static string CsdlJsonParser_MissingMemberInObject(object p0, object p1) {
internal static string CsdlJsonParser_MissingMemberInObject(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_MissingMemberInObject, p0, p1);
}

/// <summary>
/// A string like "A member '{0}' with value type '{1}' is unexpected."
/// </summary>
internal static string CsdlJsonParser_UnexpectedJsonMember(object p0, object p1) {
internal static string CsdlJsonParser_UnexpectedJsonMember(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_UnexpectedJsonMember, p0, p1);
}

/// <summary>
/// A string like "Cannot read the value '{0}' at JSON path '{1}' as '{2}' numeric value."
/// </summary>
internal static string CsdlJsonParser_CannotReadValueAsType(object p0, object p1, object p2) {
internal static string CsdlJsonParser_CannotReadValueAsType(object p0, object p1, object p2)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_CannotReadValueAsType, p0, p1, p2);
}

/// <summary>
/// A string like "A schema '{0}' object MUST contain the member '$Kind' with a string value of '{1}'."
/// </summary>
internal static string CsdlJsonParser_MissingKindMember(object p0, object p1) {
internal static string CsdlJsonParser_MissingKindMember(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_MissingKindMember, p0, p1);
}

/// <summary>
/// A string like "A property '{0}' is missing when parsing the JSON path '{1}'."
/// </summary>
internal static string CsdlJsonParser_MissingRequiredPropertyInObject(object p0, object p1) {
internal static string CsdlJsonParser_MissingRequiredPropertyInObject(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_MissingRequiredPropertyInObject, p0, p1);
}

/// <summary>
/// A string like "Found an unknown value kind '{0}' when parsing the JSON path '{1}'."
/// </summary>
internal static string CsdlJsonParser_UnknownJsonElementValueKind(object p0, object p1) {
internal static string CsdlJsonParser_UnknownJsonElementValueKind(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_UnknownJsonElementValueKind, p0, p1);
}

/// <summary>
/// A string like "Cannot parse a JSON number '{0}' when parsing the JSON path '{1}'."
/// </summary>
internal static string CsdlJsonParser_InvalidJsonNumberType(object p0, object p1) {
internal static string CsdlJsonParser_InvalidJsonNumberType(object p0, object p1)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_InvalidJsonNumberType, p0, p1);
}

/// <summary>
/// A string like "A member at JSON path '{0}' is not supported."
/// </summary>
internal static string CsdlJsonParser_UnsupportedJsonMember(object p0) {
internal static string CsdlJsonParser_UnsupportedJsonMember(object p0)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_UnsupportedJsonMember, p0);
}

/// <summary>
/// A string like "The version specified at '{0}' is not valid. It should be a string containing either '4.0' or '4.01'."
/// </summary>
internal static string CsdlJsonParser_InvalidCsdlVersion(object p0) {
internal static string CsdlJsonParser_InvalidCsdlVersion(object p0)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_InvalidCsdlVersion, p0);
}

/// <summary>
/// A string like "The schema object at '{0}' cannot have more than one entity container."
/// </summary>
internal static string CsdlJsonParser_SchemaCannotHaveMoreThanOneEntityContainer(object p0) {
internal static string CsdlJsonParser_SchemaCannotHaveMoreThanOneEntityContainer(object p0)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.CsdlJsonParser_SchemaCannotHaveMoreThanOneEntityContainer, p0);
}

Expand Down Expand Up @@ -2594,6 +2605,22 @@ internal static string TimeOfDay_InvalidCompareToTarget(object p0)
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.TimeOfDay_InvalidCompareToTarget, p0);
}

/// <summary>
/// A string like "Annotation expressions must specify a value or use a term with a specified default value. Cannot find a default value for the given annotation term, '{0}'."
/// </summary>
internal static string EdmVocabularyAnnotations_DidNotFindDefaultValue(object p0)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.EdmVocabularyAnnotations_DidNotFindDefaultValue, p0);
}

/// <summary>
/// A string like "Term type '{0}' is not supported for value retrieval."
/// </summary>
internal static string EdmVocabularyAnnotations_TermTypeNotSupported(object p0)
{
return Microsoft.OData.Edm.EdmRes.GetString(Microsoft.OData.Edm.EdmRes.EdmVocabularyAnnotations_TermTypeNotSupported, p0);
}

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// </copyright>
//---------------------------------------------------------------------

using System;

namespace Microsoft.OData.Edm.Vocabularies
{
/// <summary>
Expand Down Expand Up @@ -33,7 +35,7 @@ public EdmVocabularyAnnotation(IEdmVocabularyAnnotatable target, IEdmTerm term,
/// <param name="target">Element the annotation applies to.</param>
/// <param name="term">Term bound by the annotation.</param>
/// <param name="qualifier">Qualifier used to discriminate between multiple bindings of the same property or type.</param>
/// <param name="value">Expression producing the value of the annotation.</param>
/// <param name="value">Expression producing the value of the annotation. It could be null.</param>
public EdmVocabularyAnnotation(IEdmVocabularyAnnotatable target, IEdmTerm term, string qualifier, IEdmExpression value)
{
EdmUtil.CheckArgumentNull(target, "target");
Expand All @@ -44,6 +46,7 @@ public EdmVocabularyAnnotation(IEdmVocabularyAnnotatable target, IEdmTerm term,
this.term = term;
this.qualifier = qualifier;
this.value = value;
UseDefault = false;
}

/// <summary>
Expand Down Expand Up @@ -77,5 +80,11 @@ public IEdmExpression Value
{
get { return this.value; }
}

/// <summary>
/// Gets whether the annotation uses a default value.
/// In vNext, need refactor this.
/// </summary>
internal bool UseDefault { get; set; }
}
}
Loading