Ejemplo n.º 1
0
        /// <summary>
        /// Makes this FieldSet immutable, and returns it for convenience. Any
        /// mutable repeated fields are made immutable, as well as the map itself.
        /// </summary>
        internal FieldSet MakeImmutable()
        {
            // First check if we have any repeated values
            bool hasRepeats = false;

            foreach (object value in fields.Values)
            {
                IList <object> list = value as IList <object>;
                if (list != null && !list.IsReadOnly)
                {
                    hasRepeats = true;
                    break;
                }
            }

            if (hasRepeats)
            {
                var tmp = new SortedList <IFieldDescriptorLite, object>();
                foreach (KeyValuePair <IFieldDescriptorLite, object> entry in fields)
                {
                    IList <object> list = entry.Value as IList <object>;
                    tmp[entry.Key] = list == null ? entry.Value : Lists.AsReadOnly(list);
                }
                fields = tmp;
            }

            fields = Dictionaries.AsReadOnly(fields);

            return(this);
        }
Ejemplo n.º 2
0
        public override object GetValue(TMessage message)
        {
            List <EnumValueDescriptor> ret = new List <EnumValueDescriptor>();

            foreach (int rawValue in (IEnumerable)base.GetValue(message))
            {
                ret.Add(enumDescriptor.FindValueByNumber(rawValue));
            }
            return(Lists.AsReadOnly(ret));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Converts the given array into a read-only list, applying the specified conversion to
 /// each input element.
 /// </summary>
 internal static IList <TOutput> ConvertAndMakeReadOnly <TInput, TOutput>(IList <TInput> input,
                                                                          IndexedConverter <TInput, TOutput> converter)
 {
     TOutput[] array = new TOutput[input.Count];
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = converter(input[i], i);
     }
     return(Lists <TOutput> .AsReadOnly(array));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors.
        /// The list returned is in the same order as the protos are listed in the descriptor set.
        /// Note: this method is internal rather than private to allow testing.
        /// </summary>
        /// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
        public static IList <FileDescriptor> ConvertDescriptors(CSharpFileOptions options,
                                                                params FileDescriptorSet[] descriptorProtos)
        {
            // Simple strategy: Keep going through the list of protos to convert, only doing ones where
            // we've already converted all the dependencies, until we get to a stalemate
            List <FileDescriptorProto> fileList = new List <FileDescriptorProto>();

            foreach (FileDescriptorSet set in descriptorProtos)
            {
                fileList.AddRange(set.FileList);
            }

            FileDescriptor[] converted = new FileDescriptor[fileList.Count];

            Dictionary <string, FileDescriptor> convertedMap = new Dictionary <string, FileDescriptor>();

            int totalConverted = 0;

            bool madeProgress = true;

            while (madeProgress && totalConverted < converted.Length)
            {
                madeProgress = false;
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] != null)
                    {
                        // Already done this one
                        continue;
                    }
                    FileDescriptorProto candidate    = fileList[i];
                    FileDescriptor[]    dependencies = new FileDescriptor[candidate.DependencyList.Count];


                    CSharpFileOptions.Builder builder = options.ToBuilder();
                    if (candidate.Options.HasExtension(CSharpOptions.CSharpFileOptions))
                    {
                        builder.MergeFrom(
                            candidate.Options.GetExtension(CSharpOptions.CSharpFileOptions));
                    }
                    CSharpFileOptions localOptions = builder.Build();

                    bool foundAllDependencies = true;
                    for (int j = 0; j < dependencies.Length; j++)
                    {
                        if (!convertedMap.TryGetValue(candidate.DependencyList[j], out dependencies[j]))
                        {
                            // We can auto-magically resolve these since we already have their description
                            // This way if the file is only referencing options it does not need to be built with the
                            // --include_imports definition.
                            if (localOptions.IgnoreGoogleProtobuf &&
                                (candidate.DependencyList[j] == "google/protobuf/csharp_options.proto"))
                            {
                                dependencies[j] = CSharpOptions.Descriptor;
                                continue;
                            }
                            if (localOptions.IgnoreGoogleProtobuf &&
                                (candidate.DependencyList[j] == "google/protobuf/descriptor.proto"))
                            {
                                dependencies[j] = DescriptorProtoFile.Descriptor;
                                continue;
                            }
                            foundAllDependencies = false;
                            break;
                        }
                    }
                    if (!foundAllDependencies)
                    {
                        continue;
                    }
                    madeProgress = true;
                    totalConverted++;
                    converted[i] = FileDescriptor.BuildFrom(candidate, dependencies);
                    convertedMap[candidate.Name] = converted[i];
                }
            }
            if (!madeProgress)
            {
                StringBuilder remaining = new StringBuilder();
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] == null)
                    {
                        if (remaining.Length != 0)
                        {
                            remaining.Append(", ");
                        }
                        FileDescriptorProto failure = fileList[i];
                        remaining.Append(failure.Name);
                        remaining.Append(":");
                        foreach (string dependency in failure.DependencyList)
                        {
                            if (!convertedMap.ContainsKey(dependency))
                            {
                                remaining.Append(" ");
                                remaining.Append(dependency);
                            }
                        }
                        remaining.Append(";");
                    }
                }
                throw new DependencyResolutionException("Unable to resolve all dependencies: " + remaining);
            }
            return(Lists.AsReadOnly(converted));
        }
Ejemplo n.º 5
0
 public InvalidOptionsException(IList <string> reasons)
     : base(BuildMessage(reasons))
 {
     this.reasons = Lists.AsReadOnly(reasons);
 }
Ejemplo n.º 6
0
        internal FieldSet MakeImmutable()
        {
            bool flag = false;

            using (IEnumerator <object> enumerator = this.fields.Values.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    object         current = enumerator.Current;
                    IList <object> list    = current as IList <object>;
                    if (list != null && !list.IsReadOnly)
                    {
                        flag = true;
                        break;
                    }
                }
            }
            if (flag)
            {
                SortedList <IFieldDescriptorLite, object> sortedList = new SortedList <IFieldDescriptorLite, object>();
                using (IEnumerator <KeyValuePair <IFieldDescriptorLite, object> > enumerator2 = this.fields.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        KeyValuePair <IFieldDescriptorLite, object> current2 = enumerator2.Current;
                        IList <object> list2 = current2.Value as IList <object>;
                        sortedList[current2.Key] = ((list2 == null) ? current2.Value : Lists.AsReadOnly <object>(list2));
                    }
                }
                this.fields = sortedList;
            }
            this.fields = Dictionaries.AsReadOnly <IFieldDescriptorLite, object>(this.fields);
            return(this);
        }
 private UninitializedMessageException(IList <string> missingFields)
     : base(BuildDescription(missingFields))
 {
     this.missingFields = Lists.AsReadOnly(missingFields);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors.
        /// The list returned is in the same order as the protos are listed in the descriptor set.
        /// Note: this method is internal rather than private to allow testing.
        /// </summary>
        /// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
        internal static IList <FileDescriptor> ConvertDescriptors(FileDescriptorSet descriptorProtos)
        {
            // Simple strategy: Keep going through the list of protos to convert, only doing ones where
            // we've already converted all the dependencies, until we get to a stalemate
            IList <FileDescriptorProto> fileList = descriptorProtos.FileList;

            FileDescriptor[] converted = new FileDescriptor[fileList.Count];

            Dictionary <string, FileDescriptor> convertedMap = new Dictionary <string, FileDescriptor>();

            int totalConverted = 0;

            bool madeProgress = true;

            while (madeProgress && totalConverted < converted.Length)
            {
                madeProgress = false;
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] != null)
                    {
                        // Already done this one
                        continue;
                    }
                    FileDescriptorProto candidate    = fileList[i];
                    FileDescriptor[]    dependencies = new FileDescriptor[candidate.DependencyList.Count];
                    bool foundAllDependencies        = true;
                    for (int j = 0; j < dependencies.Length; j++)
                    {
                        if (!convertedMap.TryGetValue(candidate.DependencyList[j], out dependencies[j]))
                        {
                            foundAllDependencies = false;
                            break;
                        }
                    }
                    if (!foundAllDependencies)
                    {
                        continue;
                    }
                    madeProgress = true;
                    totalConverted++;
                    converted[i] = FileDescriptor.BuildFrom(candidate, dependencies);
                    convertedMap[candidate.Name] = converted[i];
                }
            }
            if (!madeProgress)
            {
                StringBuilder remaining = new StringBuilder();
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] == null)
                    {
                        if (remaining.Length != 0)
                        {
                            remaining.Append(", ");
                        }
                        FileDescriptorProto failure = fileList[i];
                        remaining.Append(failure.Name);
                        remaining.Append(":");
                        foreach (string dependency in failure.DependencyList)
                        {
                            if (!convertedMap.ContainsKey(dependency))
                            {
                                remaining.Append(" ");
                                remaining.Append(dependency);
                            }
                        }
                        remaining.Append(";");
                    }
                }
                throw new DependencyResolutionException("Unable to resolve all dependencies: " + remaining);
            }
            return(Lists.AsReadOnly(converted));
        }