示例#1
0
        /// <inheritdoc />
        public override ODataPathSegment Translate(ODataTemplateTranslateContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            // If the function has no parameter, we don't need to do anything and just return an operation segment.
            if (ParameterMappings.Count == 0)
            {
                return(new OperationSegment(Function, NavigationSource as IEdmEntitySetBase));
            }

            if (!SegmentTemplateHelpers.TryParseRouteKey(context.RouteValues, context.UpdatedValues, ParameterMappings))
            {
                return(null);
            }

            IList <OperationSegmentParameter> parameters = SegmentTemplateHelpers.Match(context, Function, ParameterMappings);

            if (parameters == null)
            {
                return(null);
            }

            return(new OperationSegment(Function, parameters, NavigationSource as IEdmEntitySetBase));
        }
        /// <inheritdoc />
        public override ODataPathSegment Translate(ODataTemplateTranslateContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            if (!SegmentTemplateHelpers.TryParseRouteKey(context.RouteValues, context.UpdatedValues, KeyMappings))
            {
                return(null);
            }

            RouteValueDictionary updateValues = context.UpdatedValues;

            IDictionary <string, object> keysValues = new Dictionary <string, object>();

            foreach (var key in KeyMappings)
            {
                string keyName      = key.Key;
                string templateName = key.Value;

                IEdmProperty keyProperty = EntityType.Key().FirstOrDefault(k => k.Name == keyName);
                Contract.Assert(keyProperty != null);

                IEdmTypeReference edmType = keyProperty.Type;
                if (updateValues.TryGetValue(templateName, out object rawValue))
                {
                    string strValue    = rawValue as string;
                    string newStrValue = context.GetParameterAliasOrSelf(strValue);
                    if (newStrValue != strValue)
                    {
                        updateValues[templateName] = newStrValue;
                        strValue = newStrValue;
                    }

                    object newValue = ODataUriUtils.ConvertFromUriLiteral(strValue, ODataVersion.V4, context.Model, edmType);

                    // for non FromODataUri, so update it, for example, remove the single quote for string value.
                    updateValues[templateName] = newValue;

                    // For FromODataUri, let's refactor it later.
                    string prefixName = ODataParameterValue.ParameterValuePrefix + templateName;
                    updateValues[prefixName] = new ODataParameterValue(newValue, edmType);

                    keysValues[keyName] = newValue;
                }
            }

            return(new KeySegment(keysValues, EntityType, NavigationSource));
        }
示例#3
0
        /// <inheritdoc />
        public override bool TryTranslate(ODataTemplateTranslateContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            // If the function has no parameter, we don't need to do anything and just return an operation import segment.
            if (ParameterMappings.Count == 0)
            {
                context.Segments.Add(new OperationImportSegment(FunctionImport, NavigationSource as IEdmEntitySetBase));
                return(true);
            }

            if (HasOptionalMissing())
            {
                // If this function template has the optional parameter missing,
                // for example: ~/GetSalary(min={min},max={max}), without ave={ave}
                // We should avoid this template matching with "~/GetSalary(min=1,max=2,ave=3)"
                // In this request, the comming route data has:
                // min = 1
                // max = 2,ave=3
                // so, let's combine the route data together and separate them using "," again.
                if (!SegmentTemplateHelpers.IsMatchParameters(context.RouteValues, ParameterMappings))
                {
                    return(false);
                }
            }

            IList <OperationSegmentParameter> parameters = SegmentTemplateHelpers.Match(context, FunctionImport.Function, ParameterMappings);

            if (parameters == null)
            {
                return(false);
            }

            context.Segments.Add(new OperationImportSegment(FunctionImport, NavigationSource as IEdmEntitySetBase, parameters));
            return(true);
        }