예제 #1
0
        /// <summary>
        /// Formulates the resources part of the generated snippets
        /// </summary>
        /// <param name="snippetModel">Model of the Snippets info <see cref="SnippetModel"/></param>
        /// <returns>String of the resources in Csharp code</returns>
        private static string JavaGenerateResourcesPath(SnippetModel snippetModel)
        {
            var resourcesPath       = new StringBuilder();
            var resourcesPathSuffix = string.Empty;

            // lets append all resources
            foreach (var item in snippetModel.Segments)
            {
                switch (item)
                {
                //handle indexing into collections
                case KeySegment keySegment:
                    resourcesPath.Remove(resourcesPath.Length - 2, 2);    //first remove the preceding curly braces
                    resourcesPath.Append($"(\"{keySegment.Keys.FirstOrDefault().Value}\")");
                    break;

                //handle functions/requestActions and any parameters present into collections
                case OperationSegment operationSegment:
                    var paramList = CommonGenerator.GetParameterListFromOperationSegment(operationSegment, snippetModel, "List", false);
                    resourcesPath.Append($"\n\t.{CommonGenerator.LowerCaseFirstLetter(operationSegment.Identifier)}({CommonGenerator.GetListAsStringForSnippet(paramList, ",")})");
                    break;

                case ReferenceSegment _:
                    resourcesPath.Append(".reference()");
                    break;

                case ValueSegment _:
                    resourcesPath.Append(".content()");
                    break;

                case PropertySegment _:
                    //do nothing we cant access it directly
                    break;

                case NavigationPropertyLinkSegment _:
                    /*
                     * The ODataURIParser may sometimes not create and add a ReferenceSegment object to the end of
                     * the segments collection in the event that there is a valid NavigationPropertySegment in the
                     * collection. It will replace this NavigationPropertySegment object with a NavigationPropertyLinkSegment
                     * object. Therefore we modify the suffix so that it may be appended to show the Reference section since
                     * the $ref should always be last in a valid Odata URI.
                     */
                    if (snippetModel.Path.Contains("$ref") && !(snippetModel.Segments.Last() is ReferenceSegment))
                    {
                        var nextSegmentIndex = snippetModel.Segments.IndexOf(item) + 1;
                        if (nextSegmentIndex >= snippetModel.Segments.Count)
                        {
                            nextSegmentIndex = snippetModel.Segments.Count - 1;
                        }

                        var nextSegment = snippetModel.Segments[nextSegmentIndex];
                        //check if the next segment is a KeySegment to know if we will be accessing a single entity of a collection.
                        resourcesPathSuffix = (item.EdmType is IEdmCollectionType) && !(nextSegment is KeySegment) ? ".references()" : ".reference()";
                    }
                    resourcesPath.Append($".{CommonGenerator.LowerCaseFirstLetter(item.Identifier)}()");
                    break;

                case CountSegment _:
                    resourcesPath.Append(".count()");
                    break;

                default:
                    //its most likely just a resource so append it with lower case first letter and `()` at the end
                    resourcesPath.Append($".{CommonGenerator.LowerCaseFirstLetter(item.Identifier)}()");
                    break;
                }
            }

            if (!string.IsNullOrEmpty(resourcesPathSuffix))
            {
                resourcesPath.Append(resourcesPathSuffix);
            }

            return(resourcesPath.ToString());
        }
        /// <summary>
        /// Formulates the resources part of the generated snippets
        /// </summary>
        /// <param name="snippetModel">Model of the Snippets info <see cref="SnippetModel"/></param>
        /// <returns>String of the resources in Csharp code</returns>
        private static string CSharpGenerateResourcesPath(SnippetModel snippetModel)
        {
            var resourcesPath       = new StringBuilder();
            var resourcesPathSuffix = string.Empty;

            // lets append all resources
            foreach (var item in snippetModel.Segments)
            {
                switch (item)
                {
                //handle indexing into collections
                case KeySegment keySegment:
                    resourcesPath.Append($"[\"{keySegment.Keys.FirstOrDefault().Value}\"]");
                    break;

                // handle special case of indexing on a property through extensions, e.g.
                // IThumbnailSetRequestBuilder has a manually written extension which allows indexing on {size}
                // https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/src/Microsoft.Graph/Requests/Extensions/IThumbnailSetRequestBuilderExtensions.cs
                case DynamicPathSegment pathSegment when pathSegment.Identifier.Contains("{"):
                    resourcesPath.Append($"[\"{pathSegment.Identifier}\"]");

                    break;

                //handle functions/actions and any parameters present into collections
                case OperationSegment operationSegment:
                    var paramList  = CommonGenerator.GetParameterListFromOperationSegment(operationSegment, snippetModel);
                    var parameters = string.Join(",", paramList.Select(x =>
                    {
                        if (x.Contains("'"))
                        {
                            // handle enums, e.g. microsoft.graph.timeZoneStandard'Iana'
                            // do we have other special types that show up in URLs?
                            var split     = x.Split("'");
                            var enumType  = CommonGenerator.UppercaseFirstLetter(split[0].Split(".").Last());    // TimeZoneStandard
                            var enumValue = split[1];
                            return($"{enumType}.{enumValue}");
                        }
                        else
                        {
                            return(x);
                        }
                    }));
                    resourcesPath.Append($"\n\t.{CommonGenerator.UppercaseFirstLetter(operationSegment.Identifier)}({parameters})");
                    break;

                case ValueSegment _:
                    resourcesPath.Append(".Content");
                    break;

                case PropertySegment propertySegment:
                    //don't append anything that is not a stream since this is not accessed directly in C#
                    if (propertySegment.EdmType.IsStream())
                    {
                        resourcesPath.Append($".{CommonGenerator.UppercaseFirstLetter(item.Identifier)}");
                    }
                    break;

                case ReferenceSegment _:
                    resourcesPath.Append(".Reference");
                    break;

                case NavigationPropertyLinkSegment _:
                    /*
                     * The ODataURIParser may sometimes not create and add a ReferenceSegment object to the end of
                     * the segments collection in the event that there is a valid NavigationPropertySegment in the
                     * collection. It will replace this NavigationPropertySegment object with a NavigationPropertyLinkSegment
                     * object. Therefore we modify the suffix so that it may be appended to show the Reference section since
                     * the $ref should always be last in a valid Odata URI.
                     */
                    if (snippetModel.Path.Contains("$ref") && !(snippetModel.Segments.Last() is ReferenceSegment))
                    {
                        var nextSegmentIndex = snippetModel.Segments.IndexOf(item) + 1;
                        if (nextSegmentIndex >= snippetModel.Segments.Count)
                        {
                            nextSegmentIndex = snippetModel.Segments.Count - 1;
                        }

                        var nextSegment = snippetModel.Segments[nextSegmentIndex];
                        //check if the next segment is a KeySegment to know if we will be accessing a single entity of a collection.
                        resourcesPathSuffix = (item.EdmType is IEdmCollectionType) && !(nextSegment is KeySegment) ? ".References" : ".Reference";
                    }
                    resourcesPath.Append($".{CommonGenerator.UppercaseFirstLetter(item.Identifier)}");
                    break;

                case TypeSegment _:
                    break;     // type segment is not part of the resource path.

                default:
                    //its most likely just a resource so append it
                    resourcesPath.Append($".{CommonGenerator.UppercaseFirstLetter(item.Identifier)}");
                    break;
                }
            }

            if (!string.IsNullOrEmpty(resourcesPathSuffix))
            {
                resourcesPath.Append(resourcesPathSuffix);
            }

            return(resourcesPath.ToString());
        }